1use std::cell::{Cell, RefCell};
4use std::collections::BTreeMap;
5
6pub trait AttachmentStore {
9 fn put(&self, name: &str, bytes: &[u8]) -> Result<String, String>;
11 fn get(&self, key: &str) -> Result<Option<Vec<u8>>, String>;
13 fn remove(&self, key: &str) -> Result<(), String>;
15}
16
17#[derive(Debug, Default)]
19#[cfg_attr(feature = "live", ravenrs_live::expose(part = "fields"))]
20pub struct MemoryAttachmentStore {
21 files: RefCell<BTreeMap<String, Vec<u8>>>,
22 next: Cell<u64>,
23}
24
25impl AttachmentStore for MemoryAttachmentStore {
26 fn put(&self, _name: &str, bytes: &[u8]) -> Result<String, String> {
27 let key = self.next.get() + 1;
28 self.next.set(key);
29 self.files.borrow_mut().insert(key.to_string(), bytes.to_vec());
30 Ok(key.to_string())
31 }
32
33 fn get(&self, key: &str) -> Result<Option<Vec<u8>>, String> {
34 Ok(self.files.borrow().get(key).cloned())
35 }
36
37 fn remove(&self, key: &str) -> Result<(), String> {
38 self.files.borrow_mut().remove(key);
39 Ok(())
40 }
41}