draft.rs (1085B)
1 use std::collections::HashMap; 2 3 #[derive(Default)] 4 pub struct Draft { 5 pub buffer: String, 6 } 7 8 #[derive(Default)] 9 pub struct Drafts { 10 replies: HashMap<[u8; 32], Draft>, 11 quotes: HashMap<[u8; 32], Draft>, 12 compose: Draft, 13 } 14 15 impl Drafts { 16 pub fn compose_mut(&mut self) -> &mut Draft { 17 &mut self.compose 18 } 19 20 pub fn reply_mut(&mut self, id: &[u8; 32]) -> &mut Draft { 21 self.replies.entry(*id).or_default() 22 } 23 24 pub fn quote_mut(&mut self, id: &[u8; 32]) -> &mut Draft { 25 self.quotes.entry(*id).or_default() 26 } 27 } 28 29 pub enum DraftSource<'a> { 30 Compose, 31 Reply(&'a [u8; 32]), // note id 32 Quote(&'a [u8; 32]), // note id 33 } 34 35 /* 36 impl<'a> DraftSource<'a> { 37 pub fn draft(&self, drafts: &'a mut Drafts) -> &'a mut Draft { 38 match self { 39 DraftSource::Compose => drafts.compose_mut(), 40 DraftSource::Reply(id) => drafts.reply_mut(id), 41 } 42 } 43 } 44 */ 45 46 impl Draft { 47 pub fn new() -> Self { 48 Draft::default() 49 } 50 51 pub fn clear(&mut self) { 52 self.buffer = "".to_string(); 53 } 54 }