1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
mod attach; mod blob; mod error; mod query; mod record; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::convert::TryInto; pub use crate::schema::*; pub use attach::Attachment; pub use blob::Blob; pub use error::ChatRecordError; pub use query::Query; pub use record::Record; pub type Attachments = HashMap<String, Vec<u8>>; pub type MetadataMerger<C> = fn(&C, &Attachments, Vec<u8>, Vec<u8>) -> Option<Vec<u8>>; pub trait ChatRecoder<'a> { fn insert_or_update_record<R>( &mut self, record: R, merger: Option<MetadataMerger<Self>>, ) -> ChatRecordResult<bool> where R: Into<RecordType<'a>>; fn remove_record<R: Into<RecordType<'a>>>(&mut self, record: R) -> ChatRecordResult<bool>; fn get_record(&self, query: Query) -> ChatRecordResult<Vec<Record>>; } #[derive(Clone)] pub enum RecordType<'a> { Id(i32), Record(Record), RecordRef(&'a Record), RecordWithAttachs { record: Record, attachs: Attachments, }, RecordRefWithAttachs { record: &'a Record, attachs: Attachments, }, } impl<'a> RecordType<'a> { pub fn get_record(&'a self) -> Option<&'a Record> { match self { RecordType::Record(record) | RecordType::RecordWithAttachs { record, .. } => { Some(&record) } RecordType::RecordRef(record) | RecordType::RecordRefWithAttachs { record, .. } => { Some(record) } _ => None, } } pub fn display(&self) -> String { self.get_record() .map(Record::display) .unwrap_or("[no content]".into()) } } impl<'a> From<i32> for RecordType<'a> { fn from(src: i32) -> Self { Self::Id(src) } } impl From<Record> for RecordType<'_> { fn from(src: Record) -> Self { Self::Record(src) } } impl<'a> From<&'a Record> for RecordType<'a> { fn from(src: &'a Record) -> Self { Self::RecordRef(src) } } impl From<(Record, Attachments)> for RecordType<'_> { fn from(src: (Record, Attachments)) -> Self { Self::RecordWithAttachs { record: src.0, attachs: src.1, } } } impl<'a> From<(&'a Record, Attachments)> for RecordType<'a> { fn from(src: (&'a Record, Attachments)) -> Self { Self::RecordRefWithAttachs { record: src.0, attachs: src.1, } } } pub enum AttachType { Id(i32), Attach(Attachment), } impl From<i32> for AttachType { fn from(src: i32) -> Self { Self::Id(src) } } impl From<Attachment> for AttachType { fn from(src: Attachment) -> Self { Self::Attach(src) } } pub type ChatRecordResult<T> = Result<T, ChatRecordError>;