Abstract away the output of the IO backend

This patch refactors the output part of the IO backend into an own
backend which is simply used by the IO-backend.

This way we can have a pure-output backend.
This commit is contained in:
Matthias Beyer 2017-06-18 13:31:20 +02:00
parent caf77012d9
commit b572e7e8b9
2 changed files with 167 additions and 54 deletions

View file

@ -20,16 +20,16 @@
use std::rc::Rc; use std::rc::Rc;
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::Debug;
use std::fmt::Error as FmtError;
use std::fmt::Formatter;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use std::sync::Mutex; use std::sync::Mutex;
use std::ops::Deref;
use std::fmt::Debug;
use std::fmt::Error as FmtError;
use std::fmt::Formatter;
use libimagerror::into::IntoError; use libimagerror::into::IntoError;
use libimagerror::trace::*;
use error::StoreErrorKind as SEK; use error::StoreErrorKind as SEK;
use error::StoreError as SE; use error::StoreError as SE;
@ -39,25 +39,14 @@ use super::InMemoryFileAbstraction;
use store::Entry; use store::Entry;
pub mod mapper; pub mod mapper;
pub mod out;
use self::mapper::Mapper; use self::mapper::Mapper;
use self::out::StdoutFileAbstraction;
// Because this is not exported in super::inmemory; // Because this is not exported in super::inmemory;
type Backend = Arc<Mutex<RefCell<HashMap<PathBuf, Entry>>>>; type Backend = Arc<Mutex<RefCell<HashMap<PathBuf, Entry>>>>;
pub struct StdIoFileAbstraction<W: Write, M: Mapper> { pub struct StdIoFileAbstraction<W: Write, M: Mapper>(StdoutFileAbstraction<W, M>);
mapper: M,
mem: InMemoryFileAbstraction,
out: Rc<RefCell<W>>,
}
impl<W, M> Debug for StdIoFileAbstraction<W, M>
where M: Mapper,
W: Write
{
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
write!(f, "StdIoFileAbstraction({:?}", self.mem)
}
}
impl<W, M> StdIoFileAbstraction<W, M> impl<W, M> StdIoFileAbstraction<W, M>
where M: Mapper, where M: Mapper,
@ -65,71 +54,65 @@ impl<W, M> StdIoFileAbstraction<W, M>
{ {
pub fn new<R: Read>(in_stream: &mut R, out_stream: Rc<RefCell<W>>, mapper: M) -> Result<StdIoFileAbstraction<W, M>, SE> { pub fn new<R: Read>(in_stream: &mut R, out_stream: Rc<RefCell<W>>, mapper: M) -> Result<StdIoFileAbstraction<W, M>, SE> {
let mem = InMemoryFileAbstraction::new(); StdoutFileAbstraction::new(out_stream, mapper)
.and_then(|out| {
let fill_res = match out.backend().lock() {
Err(_) => Err(SEK::LockError.into_error()),
Ok(mut mtx) => out.mapper().read_to_fs(in_stream, mtx.get_mut())
};
let _ = try!(fill_res);
{ Ok(StdIoFileAbstraction(out))
let fill_res = match mem.backend().lock() { })
Err(_) => Err(SEK::LockError.into_error()),
Ok(mut mtx) => mapper.read_to_fs(in_stream, mtx.get_mut())
};
let _ = try!(fill_res);
}
Ok(StdIoFileAbstraction {
mapper: mapper,
mem: mem,
out: out_stream,
})
} }
pub fn backend(&self) -> &Backend { pub fn backend(&self) -> &Backend {
self.mem.backend() self.0.backend()
} }
} }
impl<W, M> Drop for StdIoFileAbstraction<W, M> impl<W, M> Debug for StdIoFileAbstraction<W, M>
where M: Mapper, where M: Mapper,
W: Write W: Write
{ {
fn drop(&mut self) { fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
use std::ops::DerefMut; write!(f, "StdIoFileAbstraction({:?}", self.0)
let fill_res = match self.mem.backend().lock() {
Err(_) => Err(SEK::LockError.into_error()),
Ok(mut mtx) => {
self.mapper.fs_to_write(mtx.get_mut(), self.out.borrow_mut().deref_mut())
},
};
// We can do nothing but end this here with a trace.
// As this drop gets called when imag almost exits, there is no point in exit()ing here
// again.
let _ = fill_res.map_err_trace();
} }
} }
impl<W, M> Deref for StdIoFileAbstraction<W, M>
where M: Mapper,
W: Write
{
type Target = StdoutFileAbstraction<W, M>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
// basically #[derive(FileAbstraction)]
impl<W: Write, M: Mapper> FileAbstraction for StdIoFileAbstraction<W, M> { impl<W: Write, M: Mapper> FileAbstraction for StdIoFileAbstraction<W, M> {
fn remove_file(&self, path: &PathBuf) -> Result<(), SE> { fn remove_file(&self, path: &PathBuf) -> Result<(), SE> {
self.mem.remove_file(path) self.0.remove_file(path)
} }
fn copy(&self, from: &PathBuf, to: &PathBuf) -> Result<(), SE> { fn copy(&self, from: &PathBuf, to: &PathBuf) -> Result<(), SE> {
self.mem.copy(from, to) self.0.copy(from, to)
} }
fn rename(&self, from: &PathBuf, to: &PathBuf) -> Result<(), SE> { fn rename(&self, from: &PathBuf, to: &PathBuf) -> Result<(), SE> {
self.mem.rename(from, to) self.0.rename(from, to)
} }
fn create_dir_all(&self, pb: &PathBuf) -> Result<(), SE> { fn create_dir_all(&self, pb: &PathBuf) -> Result<(), SE> {
self.mem.create_dir_all(pb) self.0.create_dir_all(pb)
} }
fn new_instance(&self, p: PathBuf) -> Box<FileAbstractionInstance> { fn new_instance(&self, p: PathBuf) -> Box<FileAbstractionInstance> {
self.mem.new_instance(p) self.0.new_instance(p)
} }
} }

View file

@ -0,0 +1,130 @@
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
//! A StdIoFileAbstraction which does not read from stdin.
use std::rc::Rc;
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt::Debug;
use std::fmt::Error as FmtError;
use std::fmt::Formatter;
use std::io::Write;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
use libimagerror::into::IntoError;
use libimagerror::trace::*;
use error::StoreErrorKind as SEK;
use error::StoreError as SE;
use super::FileAbstraction;
use super::FileAbstractionInstance;
use super::InMemoryFileAbstraction;
use store::Entry;
use super::mapper::Mapper;
// Because this is not exported in super::inmemory;
type Backend = Arc<Mutex<RefCell<HashMap<PathBuf, Entry>>>>;
pub struct StdoutFileAbstraction<W: Write, M: Mapper> {
mapper: M,
mem: InMemoryFileAbstraction,
out: Rc<RefCell<W>>,
}
impl<W, M> StdoutFileAbstraction<W, M>
where M: Mapper,
W: Write
{
pub fn new(out_stream: Rc<RefCell<W>>, mapper: M) -> Result<StdoutFileAbstraction<W, M>, SE> {
Ok(StdoutFileAbstraction {
mapper: mapper,
mem: InMemoryFileAbstraction::new(),
out: out_stream,
})
}
pub fn backend(&self) -> &Backend {
self.mem.backend()
}
pub fn mapper(&self) -> &M {
&self.mapper
}
}
impl<W, M> Debug for StdoutFileAbstraction<W, M>
where M: Mapper,
W: Write
{
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
write!(f, "StdoutFileAbstraction({:?}", self.mem)
}
}
impl<W, M> Drop for StdoutFileAbstraction<W, M>
where M: Mapper,
W: Write
{
fn drop(&mut self) {
use std::ops::DerefMut;
let fill_res = match self.mem.backend().lock() {
Err(_) => Err(SEK::LockError.into_error()),
Ok(mut mtx) => {
self.mapper.fs_to_write(mtx.get_mut(), self.out.borrow_mut().deref_mut())
},
};
// We can do nothing but end this here with a trace.
// As this drop gets called when imag almost exits, there is no point in exit()ing here
// again.
let _ = fill_res.map_err_trace();
}
}
impl<W: Write, M: Mapper> FileAbstraction for StdoutFileAbstraction<W, M> {
fn remove_file(&self, path: &PathBuf) -> Result<(), SE> {
self.mem.remove_file(path)
}
fn copy(&self, from: &PathBuf, to: &PathBuf) -> Result<(), SE> {
self.mem.copy(from, to)
}
fn rename(&self, from: &PathBuf, to: &PathBuf) -> Result<(), SE> {
self.mem.rename(from, to)
}
fn create_dir_all(&self, pb: &PathBuf) -> Result<(), SE> {
self.mem.create_dir_all(pb)
}
fn new_instance(&self, p: PathBuf) -> Box<FileAbstractionInstance> {
self.mem.new_instance(p)
}
}