diff --git a/src/backend.rs b/src/backend.rs new file mode 100644 index 0000000..684d032 --- /dev/null +++ b/src/backend.rs @@ -0,0 +1,81 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use error; + +/// The Backend Trait +/// +/// This trait describes a simple backend, allowing users to swap it, or +/// to implement one themselves +pub trait Backend { + /// This method gets the data from the backend + fn get_data(&mut self) -> error::Result>; + + /// This method + fn put_data(&mut self, data: &[u8]) -> error::Result<()>; +} + +/// A backend using a file +pub struct FileBackend { + file: ::std::fs::File +} + +impl Backend for FileBackend { + fn get_data(&mut self) -> error::Result> { + use ::std::io::{Seek, SeekFrom, Read}; + + let mut buffer = vec![]; + self.file.seek(SeekFrom::Start(0))?; + self.file.read_to_end(&mut buffer)?; + Ok(buffer) + } + + fn put_data(&mut self, data: &[u8]) -> error::Result<()> { + use ::std::io::{Seek, SeekFrom, Write}; + + self.file.seek(SeekFrom::Start(0))?; + self.file.set_len(0)?; + self.file.write_all(data)?; + Ok(()) + } +} + +impl FileBackend { + /// Opens a new FileBackend for a given path + pub fn open>(path: P) -> error::Result { + use ::std::fs::OpenOptions; + + Ok(FileBackend { + file: OpenOptions::new().read(true).write(true).create(true).open(path)?, + }) + } + + /// Uses an already open File + pub fn from_file(file: ::std::fs::File) -> FileBackend { + FileBackend { + file: file + } + } + + /// Return the inner File + pub fn into_inner(self) -> ::std::fs::File { + self.file + } +} + +/// An in memory backend +/// +/// It is backed by a `Vec` +pub struct MemoryBackend(pub(crate) Vec); + +impl Backend for MemoryBackend { + fn get_data(&mut self) -> error::Result> { + Ok(self.0.clone()) + } + + fn put_data(&mut self, data: &[u8]) -> error::Result<()> { + self.0 = data.to_owned(); + Ok(()) + } +} diff --git a/src/lib.rs b/src/lib.rs index 5b558a4..12feb06 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,13 +33,10 @@ //! - Which kind of Serialization is used for persistence //! - Which kind of persistence is used //! -//! Per default these options are used: +//! There are two helper type aliases `MemoryDatabase` and `FileDatabase`, each backed by their +//! respective backend. //! -//! - The Serialization is [Ron][ron], a familiar notation for Rust -//! - The persistence is in-memory, allowing for quick prototyping -//! -//! Later in the development process, the Serialization and the Persistence can be exchanged without -//! breaking the code, allowing you to be flexible. +//! Using the `with_deser` and `with_backend` one can switch between the representations one needs. //! //! If you have any questions feel free to ask at the main [repo][repo]. //! @@ -82,10 +79,11 @@ extern crate bincode; extern crate base64; mod error; +/// Different storage backends +pub mod backend; /// Different serialization and deserialization methods one can use pub mod deser; -use std::io::{Read, Write}; use std::sync::{Mutex, RwLock}; use std::fmt::Debug; @@ -94,66 +92,7 @@ use serde::de::DeserializeOwned; // use error::{BreakResult, BreakError}; use deser::DeSerializer; - -/// The Backend Trait -/// -/// This trait describes a simple backend, allowing users to swap it, or -/// to implement one themselves -pub trait Backend { - /// This method gets the data from the backend - fn get_data(&mut self) -> error::Result>; - - /// This method - fn put_data(&mut self, data: &[u8]) -> error::Result<()>; -} - -/// A backend using a file -pub struct FileBackend { - file: std::fs::File -} - -impl Backend for FileBackend { - fn get_data(&mut self) -> error::Result> { - use std::io::{Seek, SeekFrom}; - - let mut buffer = vec![]; - self.file.seek(SeekFrom::Start(0))?; - self.file.read_to_end(&mut buffer)?; - Ok(buffer) - } - - fn put_data(&mut self, data: &[u8]) -> error::Result<()> { - use std::io::{Seek, SeekFrom}; - - self.file.seek(SeekFrom::Start(0))?; - self.file.set_len(0)?; - self.file.write_all(data)?; - Ok(()) - } -} - -impl FileBackend { - /// Opens a new FileBackend for a given path - pub fn open>(path: P) -> error::Result { - use std::fs::OpenOptions; - - Ok(FileBackend { - file: OpenOptions::new().read(true).write(true).create(true).open(path)?, - }) - } - - /// Uses an already open File - pub fn from_file(file: std::fs::File) -> FileBackend { - FileBackend { - file: file - } - } - - /// Return the inner File - pub fn into_inner(self) -> std::fs::File { - self.file - } -} +use backend::{Backend, MemoryBackend, FileBackend}; /// The Central Database to RustBreak /// @@ -253,23 +192,7 @@ impl Database } } -/// An in memory backend -/// -/// It is backed by a `Vec` -pub struct MemoryBackend(Vec); - -impl Backend for MemoryBackend { - fn get_data(&mut self) -> error::Result> { - Ok(self.0.clone()) - } - - fn put_data(&mut self, data: &[u8]) -> error::Result<()> { - self.0 = data.to_owned(); - Ok(()) - } -} - -/// A database backed by a file +/// A database backed by a `Vec` pub type MemoryDatabase = Database; impl Database