use std::fmt;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TreeError {
InvalidName(String),
InvalidStructure(String),
}
impl std::error::Error for TreeError {}
impl fmt::Display for TreeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TreeError::InvalidName(msg) => write!(f, "invalid tree entry name: {}", msg),
TreeError::InvalidStructure(msg) => write!(f, "invalid tree structure: {}", msg),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum FileMode {
Normal,
Executable,
Symlink,
}
impl FileMode {
pub fn to_byte(&self) -> u8 {
match self {
FileMode::Normal => 0,
FileMode::Executable => 1,
FileMode::Symlink => 2,
}
}
pub fn from_byte(b: u8) -> Option<Self> {
match b {
0 => Some(FileMode::Normal),
1 => Some(FileMode::Executable),
2 => Some(FileMode::Symlink),
_ => None,
}
}
pub fn to_unix_mode(&self) -> u32 {
match self {
FileMode::Normal => 0o100644,
FileMode::Executable => 0o100755,
FileMode::Symlink => 0o120000,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum EntryType {
Blob,
Tree,
Symlink,
}
impl EntryType {
pub fn to_byte(&self) -> u8 {
match self {
EntryType::Blob => 0,
EntryType::Tree => 1,
EntryType::Symlink => 2,
}
}
pub fn from_byte(b: u8) -> Option<Self> {
match b {
0 => Some(EntryType::Blob),
1 => Some(EntryType::Tree),
2 => Some(EntryType::Symlink),
_ => None,
}
}
}