kitab

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | LICENSE

store.rs (1250B)


      1 use std::io::Write;
      2 use std::path::{
      3     PathBuf,
      4     Path,
      5 };
      6 use std::fs::File;
      7 
      8 use crate::meta::MetaData;
      9 
     10 /// Represents the filesystem storage location for metadata.
     11 pub struct FileStore{
     12     path: PathBuf,
     13 }
     14 
     15 impl FileStore {
     16     /// Create new store.
     17     pub fn new(p: &Path) -> Self {
     18         FileStore{
     19             path: p.to_path_buf(),
     20         }
     21     }
     22 
     23     /// Generate new writer for adding / modifying a metadata entry in the store.
     24     pub fn writer(&self, entry: &MetaData) -> impl Write {
     25         let p = self.path.join(entry.fingerprint());
     26         File::create(&p).unwrap()
     27     }
     28 }
     29 
     30 #[cfg(test)]
     31 mod tests {
     32     use biblatex::EntryType;
     33     use tempfile::tempdir;
     34     use super::{
     35         FileStore,
     36         MetaData,
     37     };
     38     use crate::digest;
     39     use std::io::Write;
     40 
     41     #[test]
     42     fn test_writer() {
     43         let mut digest = Vec::with_capacity(64);
     44         digest.resize(64, 0x2a);
     45         let digest_sha = digest::from_vec(Vec::from(digest)).unwrap();
     46         let m = MetaData::new("foo", "bar", EntryType::Article, digest_sha, None);
     47         let dir = tempdir().unwrap();
     48         let fp = dir.path();
     49         let fs = FileStore::new(&fp);
     50         let mut w = fs.writer(&m);
     51         w.write(m.title().as_bytes());
     52     }
     53 }