Mercurial > public > mercurial-scm > hg
diff rust/hg-core/src/dirstate.rs @ 42749:7ceded4419a3
rust-dirstate: use EntryState enum instead of literals
This improves code readability quite a bit, while also adding a layer of
safety because we're checking the state byte against the enum.
Differential Revision: https://phab.mercurial-scm.org/D6629
author | Rapha?l Gom?s <rgomes@octobus.net> |
---|---|
date | Tue, 09 Jul 2019 12:15:09 +0200 |
parents | 7cae6bc29ff9 |
children | fce6dc93a510 |
line wrap: on
line diff
--- a/rust/hg-core/src/dirstate.rs Tue Jul 09 11:49:49 2019 +0200 +++ b/rust/hg-core/src/dirstate.rs Tue Jul 09 12:15:09 2019 +0200 @@ -5,7 +5,9 @@ // 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::DirstateParseError; use std::collections::HashMap; +use std::convert::TryFrom; pub mod dirs_multiset; pub mod parsers; @@ -21,7 +23,7 @@ /// comes first. #[derive(Debug, PartialEq, Copy, Clone)] pub struct DirstateEntry { - pub state: i8, + pub state: EntryState, pub mode: i32, pub mtime: i32, pub size: i32, @@ -36,3 +38,42 @@ Dirstate(&'a HashMap<Vec<u8>, DirstateEntry>), Manifest(&'a Vec<Vec<u8>>), } + +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub enum EntryState { + Normal, + Added, + Removed, + Merged, + Unknown, +} + +impl TryFrom<u8> for EntryState { + type Error = DirstateParseError; + + fn try_from(value: u8) -> Result<Self, Self::Error> { + match value { + b'n' => Ok(EntryState::Normal), + b'a' => Ok(EntryState::Added), + b'r' => Ok(EntryState::Removed), + b'm' => Ok(EntryState::Merged), + b'?' => Ok(EntryState::Unknown), + _ => Err(DirstateParseError::CorruptedEntry(format!( + "Incorrect entry state {}", + value + ))), + } + } +} + +impl Into<u8> for EntryState { + fn into(self) -> u8 { + match self { + EntryState::Normal => b'n', + EntryState::Added => b'a', + EntryState::Removed => b'r', + EntryState::Merged => b'm', + EntryState::Unknown => b'?', + } + } +}