changeset 46507:68a15b5a7e58

rust: Replace DirstatePackError with HgError Differential Revision: https://phab.mercurial-scm.org/D9893
author Simon Sapin <simon.sapin@octobus.net>
date Wed, 27 Jan 2021 13:41:28 +0100
parents 39e9407820ac
children 776b97179c06
files rust/hg-core/src/dirstate/parsers.rs rust/hg-core/src/lib.rs rust/hg-cpython/src/parsers.rs
diffstat 3 files changed, 21 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/dirstate/parsers.rs	Wed Jan 27 13:19:49 2021 +0100
+++ b/rust/hg-core/src/dirstate/parsers.rs	Wed Jan 27 13:41:28 2021 +0100
@@ -3,10 +3,11 @@
 // This software may be used and distributed according to the terms of the
 // GNU General Public License version 2 or any later version.
 
+use crate::errors::HgError;
 use crate::utils::hg_path::HgPath;
 use crate::{
     dirstate::{CopyMap, EntryState, StateMap},
-    DirstateEntry, DirstatePackError, DirstateParents, DirstateParseError,
+    DirstateEntry, DirstateParents, DirstateParseError,
 };
 use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
 use micro_timer::timed;
@@ -90,7 +91,7 @@
     copy_map: &CopyMap,
     parents: DirstateParents,
     now: Duration,
-) -> Result<Vec<u8>, DirstatePackError> {
+) -> Result<Vec<u8>, HgError> {
     // TODO move away from i32 before 2038.
     let now: i32 = now.as_secs().try_into().expect("time overflow");
 
@@ -136,16 +137,23 @@
             new_filename.extend(copy.bytes());
         }
 
-        packed.write_u8(entry.state.into())?;
-        packed.write_i32::<BigEndian>(entry.mode)?;
-        packed.write_i32::<BigEndian>(entry.size)?;
-        packed.write_i32::<BigEndian>(new_mtime)?;
-        packed.write_i32::<BigEndian>(new_filename.len() as i32)?;
+        // Unwrapping because `impl std::io::Write for Vec<u8>` never errors
+        packed.write_u8(entry.state.into()).unwrap();
+        packed.write_i32::<BigEndian>(entry.mode).unwrap();
+        packed.write_i32::<BigEndian>(entry.size).unwrap();
+        packed.write_i32::<BigEndian>(new_mtime).unwrap();
+        packed
+            .write_i32::<BigEndian>(new_filename.len() as i32)
+            .unwrap();
         packed.extend(new_filename)
     }
 
     if packed.len() != expected_size {
-        return Err(DirstatePackError::BadSize(expected_size, packed.len()));
+        return Err(HgError::CorruptedRepository(format!(
+            "bad dirstate size: {} != {}",
+            expected_size,
+            packed.len()
+        )));
     }
 
     Ok(packed)
--- a/rust/hg-core/src/lib.rs	Wed Jan 27 13:19:49 2021 +0100
+++ b/rust/hg-core/src/lib.rs	Wed Jan 27 13:41:28 2021 +0100
@@ -79,19 +79,6 @@
 }
 
 #[derive(Debug, PartialEq)]
-pub enum DirstatePackError {
-    CorruptedEntry(String),
-    CorruptedParent,
-    BadSize(usize, usize),
-}
-
-impl From<std::io::Error> for DirstatePackError {
-    fn from(e: std::io::Error) -> Self {
-        DirstatePackError::CorruptedEntry(e.to_string())
-    }
-}
-
-#[derive(Debug, PartialEq)]
 pub enum DirstateMapError {
     PathNotFound(HgPathBuf),
     EmptyPath,
@@ -113,9 +100,9 @@
 #[derive(Debug, derive_more::From)]
 pub enum DirstateError {
     Parse(DirstateParseError),
-    Pack(DirstatePackError),
     Map(DirstateMapError),
     IO(std::io::Error),
+    Common(errors::HgError),
 }
 
 #[derive(Debug, derive_more::From)]
--- a/rust/hg-cpython/src/parsers.rs	Wed Jan 27 13:19:49 2021 +0100
+++ b/rust/hg-cpython/src/parsers.rs	Wed Jan 27 13:41:28 2021 +0100
@@ -15,8 +15,7 @@
 };
 use hg::{
     pack_dirstate, parse_dirstate, utils::hg_path::HgPathBuf, DirstateEntry,
-    DirstatePackError, DirstateParents, DirstateParseError, FastHashMap,
-    PARENT_SIZE,
+    DirstateParents, DirstateParseError, FastHashMap, PARENT_SIZE,
 };
 use std::convert::TryInto;
 
@@ -128,18 +127,9 @@
             }
             Ok(PyBytes::new(py, &packed))
         }
-        Err(error) => Err(PyErr::new::<exc::ValueError, _>(
-            py,
-            match error {
-                DirstatePackError::CorruptedParent => {
-                    "expected a 20-byte hash".to_string()
-                }
-                DirstatePackError::CorruptedEntry(e) => e,
-                DirstatePackError::BadSize(expected, actual) => {
-                    format!("bad dirstate size: {} != {}", actual, expected)
-                }
-            },
-        )),
+        Err(error) => {
+            Err(PyErr::new::<exc::ValueError, _>(py, error.to_string()))
+        }
     }
 }