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