Patchwork D11689: rhg: simplify split_metadata

login
register
mail settings
Submitter phabricator
Date Oct. 19, 2021, 9:53 a.m.
Message ID <differential-rev-PHID-DREV-tksnys3qoubhy3zahx7d-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/50013/
State Superseded
Headers show

Comments

phabricator - Oct. 19, 2021, 9:53 a.m.
aalekseyev created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  It turns out that it's possible to implement `FilelogEntry.into_data`
  on top of `split`, as proposed by @spectral.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D11689

AFFECTED FILES
  rust/hg-core/src/revlog/filelog.rs

CHANGE DETAILS




To: aalekseyev, #hg-reviewers
Cc: mercurial-patches, spectral, mercurial-devel

Patch

diff --git a/rust/hg-core/src/revlog/filelog.rs b/rust/hg-core/src/revlog/filelog.rs
--- a/rust/hg-core/src/revlog/filelog.rs
+++ b/rust/hg-core/src/revlog/filelog.rs
@@ -54,28 +54,18 @@ 
 
 impl FilelogEntry {
     /// Split into metadata and data
-    /// Returns None if there is no metadata, so the entire entry is data.
-    fn split_metadata(&self) -> Result<Option<(&[u8], &[u8])>, HgError> {
+    pub fn split(&self) -> Result<(Option<&[u8]>, &[u8]), HgError> {
         const DELIMITER: &[u8; 2] = &[b'\x01', b'\n'];
 
         if let Some(rest) = self.0.drop_prefix(DELIMITER) {
             if let Some((metadata, data)) = rest.split_2_by_slice(DELIMITER) {
-                Ok(Some((metadata, data)))
+                Ok((Some(metadata), data))
             } else {
                 Err(HgError::corrupted(
                     "Missing metadata end delimiter in filelog entry",
                 ))
             }
         } else {
-            Ok(None)
-        }
-    }
-
-    /// Split into metadata and data
-    pub fn split(&self) -> Result<(Option<&[u8]>, &[u8]), HgError> {
-        if let Some((metadata, data)) = self.split_metadata()? {
-            Ok((Some(metadata), data))
-        } else {
             Ok((None, &self.0))
         }
     }
@@ -89,7 +79,7 @@ 
     /// Consume the entry, and convert it into data, discarding any metadata,
     /// if present.
     pub fn into_data(self) -> Result<Vec<u8>, HgError> {
-        if let Some((_metadata, data)) = self.split_metadata()? {
+        if let (Some(_metadata), data) = self.split()? {
             Ok(data.to_owned())
         } else {
             Ok(self.0)