use alloc::{string::String, vec::Vec};
use thiserror::Error;
use crate::{flag::types::M2dirFlags, path::M2dirPath};
#[derive(Clone, Debug, Error)]
pub enum ParseFilenameError {
#[error("path {0} is not a regular file")]
NotFile(M2dirPath),
#[error("path {0} is missing a filename")]
MissingFilename(M2dirPath),
#[error("entry {path} does not match filename spec: {reason}")]
InvalidFilename {
path: M2dirPath,
reason: &'static str,
},
#[error("invalid checksum for {path}: expected {expected:?}, got {got:?}")]
InvalidChecksum {
path: M2dirPath,
expected: String,
got: String,
},
}
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct M2dirEntry {
id: String,
path: M2dirPath,
}
impl M2dirEntry {
pub fn from_parts(id: impl Into<String>, path: impl Into<M2dirPath>) -> Self {
Self {
id: id.into(),
path: path.into(),
}
}
pub fn path(&self) -> &M2dirPath {
&self.path
}
pub fn id(&self) -> &str {
&self.id
}
pub fn checksum(&self) -> &str {
self.id.rsplit_once('.').map(|(c, _)| c).unwrap_or(&self.id)
}
}
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct M2dirFullEntry {
entry: M2dirEntry,
contents: Vec<u8>,
flags: M2dirFlags,
}
impl M2dirFullEntry {
pub fn from_parts(entry: M2dirEntry, contents: Vec<u8>, flags: M2dirFlags) -> Self {
Self {
entry,
contents,
flags,
}
}
pub fn entry(&self) -> &M2dirEntry {
&self.entry
}
pub fn contents(&self) -> &[u8] {
&self.contents
}
pub fn flags(&self) -> &M2dirFlags {
&self.flags
}
}