diff --git a/src/main.rs b/src/main.rs index aa577cdb..b508b7f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ mod cli; mod error; mod runtime; mod module; +mod storage; fn main() { let mut config = Config::new(); diff --git a/src/storage.rs b/src/storage.rs new file mode 100644 index 00000000..12d743ca --- /dev/null +++ b/src/storage.rs @@ -0,0 +1,69 @@ +pub use std::path::Path; +pub use std::fs::File; + +pub use runtime::Runtime; +pub use error::ImagError; +pub use error::ImagErrorBase; + +pub struct StorageError { + base : ImagErrorBase, + backendname : String, +} + +impl StorageError { + + pub fn new(backend : &T, short: String, long: String) -> StorageError { + StorageError { + base: ImagErrorBase { + shortdesc: short, + longdesc: long, + }, + backendname: backend.name() + } + } + +} + +impl<'a> ImagError<'a> for StorageError { + + fn print(&self, rt: &Runtime) { + if self.base.longdesc.is_empty() { + let s = format!("Backend {}: {}\n\n{}\n\n", + self.backendname, + self.base.shortdesc, + self.base.longdesc); + rt.print(&s) + } else { + let s = format!("Backend {}: {}\n", + self.backendname, + self.base.shortdesc); + rt.print(&s) + } + } + + fn print_short(&self, rt : &Runtime) { + let s = format!("Backend {}: {}\n", + self.backendname, + self.base.shortdesc); + rt.print(&s) + } + + fn print_long(&self, rt : &Runtime) { + let s = format!("Backend {}: {}\n\n{}\n\n", + self.backendname, + self.base.shortdesc, + self.base.longdesc); + rt.print(&s) + } +} + +pub trait StorageBackend { + + fn name(&self) -> String; + + fn create(&self, file : File) -> Option; + fn read(&self, path: Path) -> Result; + fn update(&self, file : File) -> Option; + fn destroy(&self, path: Path) -> Option; + +}