diff --git a/rust/hg-core/src/dirstate/parsers.rs b/rust/hg-core/src/dirstate/parsers.rs --- a/rust/hg-core/src/dirstate/parsers.rs +++ b/rust/hg-core/src/dirstate/parsers.rs @@ -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, DirstatePackError> { +) -> Result, 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::(entry.mode)?; - packed.write_i32::(entry.size)?; - packed.write_i32::(new_mtime)?; - packed.write_i32::(new_filename.len() as i32)?; + // Unwrapping because `impl std::io::Write for Vec` never errors + packed.write_u8(entry.state.into()).unwrap(); + packed.write_i32::(entry.mode).unwrap(); + packed.write_i32::(entry.size).unwrap(); + packed.write_i32::(new_mtime).unwrap(); + packed + .write_i32::(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) diff --git a/rust/hg-core/src/lib.rs b/rust/hg-core/src/lib.rs --- a/rust/hg-core/src/lib.rs +++ b/rust/hg-core/src/lib.rs @@ -79,19 +79,6 @@ } #[derive(Debug, PartialEq)] -pub enum DirstatePackError { - CorruptedEntry(String), - CorruptedParent, - BadSize(usize, usize), -} - -impl From 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)] diff --git a/rust/hg-cpython/src/parsers.rs b/rust/hg-cpython/src/parsers.rs --- a/rust/hg-cpython/src/parsers.rs +++ b/rust/hg-cpython/src/parsers.rs @@ -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::( - 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::(py, error.to_string())) + } } }