use std::collections::HashMap;
use std::path::PathBuf;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum DraftKey {
NewConversation(Option<PathBuf>),
Message(String),
}
impl DraftKey {
pub fn composer(workspace: Option<PathBuf>, selected: Option<String>) -> Self {
match selected {
Some(agent) => Self::Message(agent),
None => Self::NewConversation(workspace),
}
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Drafts {
by_target: HashMap<DraftKey, String>,
}
impl Drafts {
pub fn text(&self, key: &DraftKey) -> String {
self.by_target.get(key).cloned().unwrap_or_default()
}
pub fn set(&mut self, key: DraftKey, text: String) {
if text.is_empty() {
self.by_target.remove(&key);
} else {
self.by_target.insert(key, text);
}
}
pub fn sent(&mut self, key: &DraftKey, fired: &str) {
let now = self.text(key);
let rest = now.strip_prefix(fired).unwrap_or(&now).to_owned();
self.set(key.clone(), rest);
}
pub fn carry(&mut self, from: &DraftKey, to: &DraftKey) {
let text = self.text(from);
if from == to || text.is_empty() || !self.text(to).is_empty() {
return;
}
self.set(from.clone(), String::new());
self.set(to.clone(), text);
}
pub fn is_empty(&self) -> bool {
self.by_target.is_empty()
}
}
#[cfg(test)]
mod tests;