Add trait Storage
This commit is contained in:
parent
7b1f9cfbac
commit
b3508f5dc3
2 changed files with 70 additions and 0 deletions
|
@ -8,6 +8,7 @@ mod cli;
|
|||
mod error;
|
||||
mod runtime;
|
||||
mod module;
|
||||
mod storage;
|
||||
|
||||
fn main() {
|
||||
let mut config = Config::new();
|
||||
|
|
69
src/storage.rs
Normal file
69
src/storage.rs
Normal file
|
@ -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<T : StorageBackend>(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<StorageError>;
|
||||
fn read(&self, path: Path) -> Result<File, StorageError>;
|
||||
fn update(&self, file : File) -> Option<StorageError>;
|
||||
fn destroy(&self, path: Path) -> Option<StorageError>;
|
||||
|
||||
}
|
Loading…
Reference in a new issue