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
use crate::{
field_handler::FieldHandler, result::Result, table_mapper::mapped::Mapped,
table_mapper::TableMapper,
};
use heck::MixedCase;
use std::collections::HashMap;
#[derive(Debug)]
pub struct TableMapperRegistry {
pub mappers: HashMap<String, TableMapper>,
}
impl TableMapperRegistry {
pub fn new() -> TableMapperRegistry {
TableMapperRegistry {
mappers: HashMap::new(),
}
}
pub fn get(&self, table_name: &str) -> Option<&TableMapper> {
self.mappers.get(table_name)
}
pub fn insert(&mut self, mapper: TableMapper) {
self.mappers
.insert(mapper.table_name.to_mixed_case(), mapper);
}
pub fn insert_new_mapper<M: Mapped>(&mut self) -> Result<String> {
let m = TableMapper::from_mapped::<M>()?;
self.mappers.insert(M::type_name(), m);
tracing::event!(tracing::Level::INFO, ty = %M::type_name(), "Registered table mapping for type.");
Ok(M::type_name())
}
pub fn insert_new_mapper_with_handler<M: Mapped, H>(&mut self, handler: H) -> Result<String>
where
H: 'static + FieldHandler + Send + Sync,
{
let m = TableMapper::from_mapped_with_handler::<M, _>(handler)?;
self.mappers.insert(M::type_name(), m);
Ok(M::type_name())
}
}
impl Default for TableMapperRegistry {
fn default() -> Self {
Self::new()
}
}