use std::fmt; use camino::Utf8PathBuf; use crate::WikPath; #[derive(Debug)] pub enum FileOp { Open, Read, Write, Delete, } impl fmt::Display for FileOp { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let statstr = match self { Self::Open => "Open", Self::Read => "Read", Self::Write => "Write", Self::Delete => "Delete", }; write!(f, "{statstr}") } } #[derive(Debug)] pub enum WikError { PageExists { name: String, }, PageNoexist { name: String, }, SectionExists { name: String, }, SectionNoexist { name: String, }, InvalidName { name: String, reason: &'static str, }, CanonicalizationFailed { path: Utf8PathBuf, }, IllegalPath { wik: WikPath, canon: Utf8PathBuf, }, FileOperation { op: FileOp, path: Utf8PathBuf, io: tokio::io::Error, }, DirectoryCreation { path: Utf8PathBuf, }, JournalError { reason: &'static str, }, GitError { git: git2::Error, }, } impl From for WikError { fn from(value: git2::Error) -> Self { Self::GitError { git: value } } } impl WikError { pub fn page_exists(path: WikPath) -> Self { Self::PageExists { name: path.to_string(), } } pub fn page_noexists(path: WikPath) -> Self { Self::PageNoexist { name: path.to_string(), } } pub fn section_exists(path: WikPath) -> Self { Self::SectionExists { name: path.to_string(), } } pub fn section_noexist(path: WikPath) -> Self { Self::SectionNoexist { name: path.to_string(), } } pub fn invalid_name>(name: P, reason: &'static str) -> Self { Self::InvalidName { name: name.into(), reason, } } pub fn name_has_slashes>(name: P) -> Self { Self::InvalidName { name: name.into(), reason: "names cannot contain slashes (\"/\")", } } pub fn canonicalization_failed(path: Utf8PathBuf) -> Self { Self::CanonicalizationFailed { path } } pub fn illegal_path(wik: WikPath, canon: Utf8PathBuf) -> Self { Self::IllegalPath { wik, canon } } pub fn file_operation( op: FileOp, path: Utf8PathBuf, io: tokio::io::Error, ) -> Result<(), WikError> { Err(Self::FileOperation { op, path, io }) } pub fn journal(reason: &'static str) -> Self { Self::JournalError { reason } } } impl fmt::Display for WikError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::PageExists { name } => { write!(f, "page '{name}' already exists") } Self::PageNoexist { name } => { write!(f, "page '{name}' does not exist") } Self::SectionExists { name } => { write!(f, "section '{name}' already exists") } Self::SectionNoexist { name } => { write!(f, "section '{name}' does not exist") } Self::InvalidName { name, reason } => { write!(f, "the name '{name}' is invalid: {reason}") } Self::CanonicalizationFailed { path } => { write!(f, "failed to get absolute path for '{path}'") } Self::DirectoryCreation { path } => { write!(f, "failed to create directory '{path}'") } Self::FileOperation { op, path, io } => { write!(f, "failed to {op} '{path}': {io}") } Self::GitError { git } => { write!(f, "git error: {git}") } Self::IllegalPath { wik, canon } => { write!(f, "path '{wik}' is invalid. system_path='{canon}'") } Self::JournalError { reason } => { write!(f, "journal error: {reason}") } } } }