rust/hg-core/src/revlog/filelog.rs
changeset 48249 e9faae0f445c
parent 48237 027ebad952ac
child 48540 20d0d896183e
equal deleted inserted replaced
48248:7f23a472068f 48249:e9faae0f445c
    52 
    52 
    53 pub struct FilelogEntry(Vec<u8>);
    53 pub struct FilelogEntry(Vec<u8>);
    54 
    54 
    55 impl FilelogEntry {
    55 impl FilelogEntry {
    56     /// Split into metadata and data
    56     /// Split into metadata and data
    57     /// Returns None if there is no metadata, so the entire entry is data.
    57     pub fn split(&self) -> Result<(Option<&[u8]>, &[u8]), HgError> {
    58     fn split_metadata(&self) -> Result<Option<(&[u8], &[u8])>, HgError> {
       
    59         const DELIMITER: &[u8; 2] = &[b'\x01', b'\n'];
    58         const DELIMITER: &[u8; 2] = &[b'\x01', b'\n'];
    60 
    59 
    61         if let Some(rest) = self.0.drop_prefix(DELIMITER) {
    60         if let Some(rest) = self.0.drop_prefix(DELIMITER) {
    62             if let Some((metadata, data)) = rest.split_2_by_slice(DELIMITER) {
    61             if let Some((metadata, data)) = rest.split_2_by_slice(DELIMITER) {
    63                 Ok(Some((metadata, data)))
    62                 Ok((Some(metadata), data))
    64             } else {
    63             } else {
    65                 Err(HgError::corrupted(
    64                 Err(HgError::corrupted(
    66                     "Missing metadata end delimiter in filelog entry",
    65                     "Missing metadata end delimiter in filelog entry",
    67                 ))
    66                 ))
    68             }
    67             }
    69         } else {
       
    70             Ok(None)
       
    71         }
       
    72     }
       
    73 
       
    74     /// Split into metadata and data
       
    75     pub fn split(&self) -> Result<(Option<&[u8]>, &[u8]), HgError> {
       
    76         if let Some((metadata, data)) = self.split_metadata()? {
       
    77             Ok((Some(metadata), data))
       
    78         } else {
    68         } else {
    79             Ok((None, &self.0))
    69             Ok((None, &self.0))
    80         }
    70         }
    81     }
    71     }
    82 
    72 
    87     }
    77     }
    88 
    78 
    89     /// Consume the entry, and convert it into data, discarding any metadata,
    79     /// Consume the entry, and convert it into data, discarding any metadata,
    90     /// if present.
    80     /// if present.
    91     pub fn into_data(self) -> Result<Vec<u8>, HgError> {
    81     pub fn into_data(self) -> Result<Vec<u8>, HgError> {
    92         if let Some((_metadata, data)) = self.split_metadata()? {
    82         if let (Some(_metadata), data) = self.split()? {
    93             Ok(data.to_owned())
    83             Ok(data.to_owned())
    94         } else {
    84         } else {
    95             Ok(self.0)
    85             Ok(self.0)
    96         }
    86         }
    97     }
    87     }