diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2020-07-06 14:29:51 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-07-06 14:29:51 +0200 |
commit | 8e48b51612e715b3f33489b301bbbf273efbde7e (patch) | |
tree | 0b81c77f4531508cec53897eff6617089d7b2faa /src | |
parent | bb405cc533daaa61c9cdcb01dab2681fb1b08675 (diff) |
Add utility module with Progress type
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src')
-rw-r--r-- | src/mailcache.rs | 102 | ||||
-rw-r--r-- | src/util.rs | 45 |
2 files changed, 45 insertions, 102 deletions
diff --git a/src/mailcache.rs b/src/mailcache.rs deleted file mode 100644 index 40a85d3..0000000 --- a/src/mailcache.rs +++ /dev/null @@ -1,102 +0,0 @@ -use std::path::PathBuf; -use std::path::Path; -use std::sync::Arc; -use std::sync::RwLock; -use anyhow::Result; -use anyhow::Error; -use async_std::sync::{Sender, Receiver}; -use async_std::sync::TryRecvError; -use async_std::sync::channel; -use async_std::task; - -use crate::mailstore::MailStore; - -struct Cache { - cache: Vec<MailStore>, - receiver: Receiver<Result<MailStore>>, -} - -impl Cache { - fn find(&self, path: &Path) -> Option<&MailStore> { - self.cache.iter().find(|e| e.path() == path) - } - - fn update(&mut self) -> Result<()> { - loop { - match self.receiver.try_recv() { - Ok(queued) => { - self.cache.push(queued?); - } - Err(TryRecvError::Empty) => break, - Err(other) => { - return Err(other).map_err(Error::from) - }, - } - } - - Ok(()) - } - - fn names(&self) -> Vec<PathBuf> { - self.cache.iter().map(|store| store.path().to_path_buf()).collect() - } -} - -#[derive(Clone)] -pub struct MailCache { - cache: Arc<RwLock<Cache>>, - sender: Sender<Result<MailStore>>, -} - -impl MailCache { - - pub fn new(buffer_cap: usize) -> Self { - let (s, r) = channel(buffer_cap); - MailCache { - cache: Arc::new(RwLock::new(Cache { - cache: Vec::new(), - receiver: r, - })), - sender: s - } - } - - pub fn load_background(&self, path: PathBuf) { - debug!("Loading {}", path.display()); - let sender = self.sender.clone(); - task::spawn(async move { - let p = path.clone(); - let mut ms = MailStore::from_path(path); - let r = ms.load().map(|_| ms); - - debug!("Finished Loading {}", p.display()); - sender.send(r).await - }); - } - - pub fn exec_on<F, T>(&self, path: &Path, f: F) -> Result<T> - where T: Sized, - F: FnOnce(Option<&MailStore>) -> Result<T>, - { - self.cache - .read() - .map_err(|_| anyhow!("RWLock possibly poisened")) - .and_then(|cache| f(cache.find(path))) - } - - pub fn update_cache(&mut self) -> Result<()> { - self.cache - .write() - .map_err(|_| anyhow!("Possibly poisened RWLock")) - .and_then(|mut cache| cache.update()) - } - - pub fn cached_pathes(&self) -> Result<Vec<PathBuf>> { - self.cache - .read() - .map_err(|_| anyhow!("Possibly poisened RWLock")) - .map(|cache| cache.names()) - } - -} - diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..19b50fe --- /dev/null +++ b/src/util.rs @@ -0,0 +1,45 @@ +use std::fmt::Debug; + +#[derive(Clone, Debug)] +pub struct Progress +{ + current: usize, + complete: usize, + is_complete: bool, +} + +impl Progress +{ + pub fn with_complete(complete: usize) -> Progress { + Progress { current: 0, complete, is_complete: false } + } + + pub fn is(current: usize, complete: usize) -> Progress { + Progress { current, complete, current == complete } + } + + pub fn new_complete() -> Progress { + Progress { current: 0, complete: 0, is_complete: true } + } + + pub fn is_complete(&self) -> bool { + self.is_complete + } + + pub fn set_complete(&mut self, c: bool) { + self.is_complete = c; + } + + pub fn step(&mut self) { + self.current += 1; + } + + pub fn current(&self) -> usize { + self.current + } + + pub fn complete(&self) -> usize { + self.complete + } +} + |