From b3508f5dc3680a77e1535890fbe22446fcebe626 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 18 Oct 2015 21:59:17 +0200 Subject: [PATCH] Add trait Storage --- src/main.rs | 1 + src/storage.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/storage.rs 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; + +}