helper.rs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. #![allow(unused_variables)]
  2. use crate::schema;
  3. use microrm::prelude::*;
  4. use super::Config;
  5. struct ValueToStringSerializer {}
  6. impl<'l> serde::Serializer for &'l ValueToStringSerializer {
  7. type Ok = Option<String>;
  8. type Error = ConfigError;
  9. type SerializeSeq = serde::ser::Impossible<Self::Ok, Self::Error>;
  10. type SerializeMap = serde::ser::Impossible<Self::Ok, Self::Error>;
  11. type SerializeTuple = serde::ser::Impossible<Self::Ok, Self::Error>;
  12. type SerializeStruct = serde::ser::Impossible<Self::Ok, Self::Error>;
  13. type SerializeTupleStruct = serde::ser::Impossible<Self::Ok, Self::Error>;
  14. type SerializeTupleVariant = serde::ser::Impossible<Self::Ok, Self::Error>;
  15. type SerializeStructVariant = serde::ser::Impossible<Self::Ok, Self::Error>;
  16. fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
  17. unreachable!()
  18. }
  19. fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
  20. self.serialize_i64(v as i64)
  21. }
  22. fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
  23. self.serialize_i64(v as i64)
  24. }
  25. fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
  26. self.serialize_i64(v as i64)
  27. }
  28. fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
  29. Ok(Some(v.to_string()))
  30. }
  31. fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
  32. unreachable!()
  33. }
  34. fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
  35. self.serialize_u64(v as u64)
  36. }
  37. fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
  38. self.serialize_u64(v as u64)
  39. }
  40. fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
  41. self.serialize_u64(v as u64)
  42. }
  43. fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
  44. Ok(Some(v.to_string()))
  45. }
  46. fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
  47. unreachable!()
  48. }
  49. fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
  50. unreachable!()
  51. }
  52. fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> {
  53. unreachable!()
  54. }
  55. fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> {
  56. unreachable!()
  57. }
  58. fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
  59. unreachable!()
  60. }
  61. fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
  62. Ok(Some(v.into()))
  63. }
  64. fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
  65. Ok(None)
  66. }
  67. fn serialize_some<T: ?Sized + serde::Serialize>(
  68. self,
  69. value: &T,
  70. ) -> Result<Self::Ok, Self::Error> {
  71. value.serialize(self)
  72. }
  73. fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
  74. unreachable!()
  75. }
  76. fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
  77. unreachable!()
  78. }
  79. fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
  80. unreachable!()
  81. }
  82. fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
  83. unreachable!()
  84. }
  85. fn serialize_struct(
  86. self,
  87. _name: &'static str,
  88. _len: usize,
  89. ) -> Result<Self::SerializeStruct, Self::Error> {
  90. unreachable!()
  91. }
  92. fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
  93. unreachable!()
  94. }
  95. fn serialize_unit_variant(
  96. self,
  97. name: &'static str,
  98. variant_index: u32,
  99. variant: &'static str,
  100. ) -> Result<Self::Ok, Self::Error> {
  101. unreachable!()
  102. }
  103. fn serialize_tuple_struct(
  104. self,
  105. name: &'static str,
  106. len: usize,
  107. ) -> Result<Self::SerializeTupleStruct, Self::Error> {
  108. unreachable!()
  109. }
  110. fn serialize_tuple_variant(
  111. self,
  112. name: &'static str,
  113. variant_index: u32,
  114. variant: &'static str,
  115. len: usize,
  116. ) -> Result<Self::SerializeTupleVariant, Self::Error> {
  117. todo!()
  118. }
  119. fn serialize_newtype_struct<T: ?Sized + serde::Serialize>(
  120. self,
  121. name: &'static str,
  122. value: &T,
  123. ) -> Result<Self::Ok, Self::Error> {
  124. todo!()
  125. }
  126. fn serialize_struct_variant(
  127. self,
  128. name: &'static str,
  129. variant_index: u32,
  130. variant: &'static str,
  131. len: usize,
  132. ) -> Result<Self::SerializeStructVariant, Self::Error> {
  133. todo!()
  134. }
  135. fn serialize_newtype_variant<T: ?Sized + serde::Serialize>(
  136. self,
  137. name: &'static str,
  138. variant_index: u32,
  139. variant: &'static str,
  140. value: &T,
  141. ) -> Result<Self::Ok, Self::Error> {
  142. todo!()
  143. }
  144. }
  145. pub struct ConfigSerializer<'r, 's> {
  146. pub config: &'r Config,
  147. pub db: &'s schema::UIDCDatabase,
  148. pub prefix: String,
  149. }
  150. impl<'r, 's> ConfigSerializer<'r, 's> {
  151. fn update(&self, key: &str, value: String) {
  152. // TODO: delete old config value
  153. // self.db.persistent_config.delete(schema::PersistentConfig { key: key.into(), value }).expect("couldn't update config");
  154. self.db
  155. .persistent_config
  156. .insert(schema::PersistentConfig {
  157. key: key.into(),
  158. value,
  159. })
  160. .expect("couldn't update config");
  161. }
  162. }
  163. impl<'r, 's> serde::ser::SerializeStruct for ConfigSerializer<'r, 's> {
  164. type Ok = ();
  165. type Error = ConfigError;
  166. fn serialize_field<T: ?Sized + serde::Serialize>(
  167. &mut self,
  168. key: &'static str,
  169. value: &T,
  170. ) -> Result<(), Self::Error> {
  171. let key = format!("{}{}", self.prefix, key);
  172. let value = value.serialize(&ValueToStringSerializer {})?;
  173. if let Some(value) = value {
  174. log::trace!("saving config {} = {}", key, value);
  175. self.update(key.as_str(), value);
  176. }
  177. Ok(())
  178. }
  179. fn end(self) -> Result<Self::Ok, Self::Error> {
  180. Ok(())
  181. }
  182. }
  183. impl<'r, 's> serde::Serializer for &'r ConfigSerializer<'r, 's> {
  184. type Ok = ();
  185. type Error = ConfigError;
  186. type SerializeSeq = serde::ser::Impossible<Self::Ok, Self::Error>;
  187. type SerializeMap = serde::ser::Impossible<Self::Ok, Self::Error>;
  188. type SerializeTuple = serde::ser::Impossible<Self::Ok, Self::Error>;
  189. type SerializeStruct = ConfigSerializer<'r, 's>;
  190. type SerializeTupleStruct = serde::ser::Impossible<Self::Ok, Self::Error>;
  191. type SerializeTupleVariant = serde::ser::Impossible<Self::Ok, Self::Error>;
  192. type SerializeStructVariant = serde::ser::Impossible<Self::Ok, Self::Error>;
  193. fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
  194. unreachable!()
  195. }
  196. fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
  197. self.serialize_i64(v as i64)
  198. }
  199. fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
  200. self.serialize_i64(v as i64)
  201. }
  202. fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
  203. self.serialize_i64(v as i64)
  204. }
  205. fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
  206. todo!()
  207. }
  208. fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
  209. unreachable!()
  210. }
  211. fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
  212. self.serialize_u64(v as u64)
  213. }
  214. fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
  215. self.serialize_u64(v as u64)
  216. }
  217. fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
  218. self.serialize_u64(v as u64)
  219. }
  220. fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
  221. todo!()
  222. }
  223. fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
  224. unreachable!()
  225. }
  226. fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
  227. unreachable!()
  228. }
  229. fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> {
  230. unreachable!()
  231. }
  232. fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> {
  233. unreachable!()
  234. }
  235. fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
  236. todo!()
  237. }
  238. fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
  239. todo!()
  240. }
  241. fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
  242. Ok(())
  243. }
  244. fn serialize_some<T: ?Sized + serde::Serialize>(
  245. self,
  246. value: &T,
  247. ) -> Result<Self::Ok, Self::Error> {
  248. todo!()
  249. }
  250. fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
  251. unreachable!()
  252. }
  253. fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
  254. todo!()
  255. }
  256. fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
  257. todo!()
  258. }
  259. fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
  260. todo!()
  261. }
  262. fn serialize_struct(
  263. self,
  264. name: &'static str,
  265. _len: usize,
  266. ) -> Result<Self::SerializeStruct, Self::Error> {
  267. log::trace!("name: {name}");
  268. let new_prefix =
  269. // are we at the root?
  270. if name == "Config" {
  271. String::new()
  272. }
  273. else {
  274. format!("{}{}.", self.prefix, name)
  275. };
  276. let subser = ConfigSerializer {
  277. config: self.config,
  278. db: self.db,
  279. prefix: new_prefix,
  280. };
  281. Ok(subser)
  282. }
  283. fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
  284. todo!()
  285. }
  286. fn serialize_unit_variant(
  287. self,
  288. name: &'static str,
  289. variant_index: u32,
  290. variant: &'static str,
  291. ) -> Result<Self::Ok, Self::Error> {
  292. todo!()
  293. }
  294. fn serialize_tuple_struct(
  295. self,
  296. name: &'static str,
  297. len: usize,
  298. ) -> Result<Self::SerializeTupleStruct, Self::Error> {
  299. todo!()
  300. }
  301. fn serialize_tuple_variant(
  302. self,
  303. name: &'static str,
  304. variant_index: u32,
  305. variant: &'static str,
  306. len: usize,
  307. ) -> Result<Self::SerializeTupleVariant, Self::Error> {
  308. todo!()
  309. }
  310. fn serialize_newtype_struct<T: ?Sized + serde::Serialize>(
  311. self,
  312. name: &'static str,
  313. value: &T,
  314. ) -> Result<Self::Ok, Self::Error> {
  315. todo!()
  316. }
  317. fn serialize_struct_variant(
  318. self,
  319. name: &'static str,
  320. variant_index: u32,
  321. variant: &'static str,
  322. len: usize,
  323. ) -> Result<Self::SerializeStructVariant, Self::Error> {
  324. todo!()
  325. }
  326. fn serialize_newtype_variant<T: ?Sized + serde::Serialize>(
  327. self,
  328. name: &'static str,
  329. variant_index: u32,
  330. variant: &'static str,
  331. value: &T,
  332. ) -> Result<Self::Ok, Self::Error> {
  333. todo!()
  334. }
  335. }
  336. pub struct ConfigDeserializer<'de> {
  337. pub config_map: &'de std::collections::HashMap<String, String>,
  338. pub prefix: String,
  339. }
  340. #[derive(Debug)]
  341. pub enum ConfigError {
  342. Missing(String),
  343. InvalidType(String),
  344. CustomError(String),
  345. }
  346. impl std::fmt::Display for ConfigError {
  347. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  348. match self {
  349. Self::Missing(what) => f.write_fmt(format_args!(
  350. "Missing required config entry: {}",
  351. what.as_str()
  352. )),
  353. Self::InvalidType(what) => f.write_fmt(format_args!(
  354. "Could not parse config entry '{}'",
  355. what.as_str()
  356. )),
  357. Self::CustomError(what) => {
  358. f.write_fmt(format_args!("Custom error '{}'", what.as_str()))
  359. }
  360. }
  361. }
  362. }
  363. impl std::error::Error for ConfigError {}
  364. impl serde::ser::Error for ConfigError {
  365. fn custom<T>(msg: T) -> Self
  366. where
  367. T: std::fmt::Display,
  368. {
  369. Self::CustomError(msg.to_string())
  370. }
  371. }
  372. impl serde::de::Error for ConfigError {
  373. fn custom<T>(msg: T) -> Self
  374. where
  375. T: std::fmt::Display,
  376. {
  377. Self::CustomError(msg.to_string())
  378. }
  379. fn invalid_type(_unexp: serde::de::Unexpected, _exp: &dyn serde::de::Expected) -> Self {
  380. Self::InvalidType("".into())
  381. }
  382. fn missing_field(field: &'static str) -> Self {
  383. Self::Missing(field.into())
  384. }
  385. }
  386. impl<'de> serde::Deserializer<'de> for &'de mut ConfigDeserializer<'de> {
  387. type Error = ConfigError;
  388. fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
  389. where
  390. V: serde::de::Visitor<'de>,
  391. {
  392. unreachable!("deserialize_any needs context")
  393. }
  394. fn deserialize_struct<V>(
  395. self,
  396. _name: &'static str,
  397. _fields: &'static [&'static str],
  398. visitor: V,
  399. ) -> Result<V::Value, Self::Error>
  400. where
  401. V: serde::de::Visitor<'de>,
  402. {
  403. self.deserialize_map(visitor)
  404. }
  405. fn deserialize_seq<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
  406. where
  407. V: serde::de::Visitor<'de>,
  408. {
  409. todo!("deserialize_seq")
  410. }
  411. fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
  412. where
  413. V: serde::de::Visitor<'de>,
  414. {
  415. let mut map_access = ConfigDeserializerIterator {
  416. it: self
  417. .config_map
  418. .iter()
  419. .filter(|e| {
  420. e.0.starts_with(&self.prefix) && !e.0[self.prefix.len()..].contains('.')
  421. })
  422. .peekable(),
  423. };
  424. visitor.visit_map(&mut map_access)
  425. }
  426. fn deserialize_enum<V>(
  427. self,
  428. _name: &'static str,
  429. _variants: &'static [&'static str],
  430. _visitor: V,
  431. ) -> Result<V::Value, Self::Error>
  432. where
  433. V: serde::de::Visitor<'de>,
  434. {
  435. todo!("deserialize_enum")
  436. }
  437. fn deserialize_tuple<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error>
  438. where
  439. V: serde::de::Visitor<'de>,
  440. {
  441. todo!("deserialize_tuple")
  442. }
  443. fn deserialize_tuple_struct<V>(
  444. self,
  445. _name: &'static str,
  446. _len: usize,
  447. _visitor: V,
  448. ) -> Result<V::Value, Self::Error>
  449. where
  450. V: serde::de::Visitor<'de>,
  451. {
  452. todo!("deserialize_tuple_struct")
  453. }
  454. serde::forward_to_deserialize_any!(
  455. i8 u8 i16 u16 i32 u32 i64 u64 i128 u128 str string bytes
  456. bool f32 f64 char byte_buf option unit unit_struct
  457. newtype_struct identifier ignored_any
  458. );
  459. }
  460. struct AtomicForwarder<'de> {
  461. to_fwd: &'de str,
  462. }
  463. impl<'de> serde::Deserializer<'de> for AtomicForwarder<'de> {
  464. type Error = ConfigError;
  465. fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
  466. where
  467. V: serde::de::Visitor<'de>,
  468. {
  469. visitor.visit_unit()
  470. }
  471. fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
  472. where
  473. V: serde::de::Visitor<'de>,
  474. {
  475. visitor.visit_u64(
  476. self.to_fwd
  477. .parse()
  478. .map_err(|_| ConfigError::InvalidType(String::new()))?,
  479. )
  480. }
  481. fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
  482. where
  483. V: serde::de::Visitor<'de>,
  484. {
  485. visitor.visit_str(self.to_fwd)
  486. }
  487. fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
  488. where
  489. V: serde::de::Visitor<'de>,
  490. {
  491. visitor.visit_str(self.to_fwd)
  492. }
  493. fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
  494. where
  495. V: serde::de::Visitor<'de>,
  496. {
  497. visitor.visit_str(self.to_fwd)
  498. }
  499. serde::forward_to_deserialize_any!(
  500. i8 u8 i16 u16 i32 u32 i64 i128 u128 bytes
  501. bool f32 f64 char byte_buf unit unit_struct option
  502. newtype_struct ignored_any struct tuple tuple_struct
  503. seq map enum
  504. );
  505. }
  506. struct ConfigDeserializerIterator<'de, I: Iterator<Item = (&'de String, &'de String)>> {
  507. it: std::iter::Peekable<I>,
  508. }
  509. impl<'de, I: Iterator<Item = (&'de String, &'de String)>> serde::de::MapAccess<'de>
  510. for ConfigDeserializerIterator<'de, I>
  511. {
  512. type Error = ConfigError;
  513. fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
  514. where
  515. K: serde::de::DeserializeSeed<'de>,
  516. {
  517. if let Some(e) = self.it.peek() {
  518. let de = AtomicForwarder {
  519. to_fwd: e.0.as_str(),
  520. };
  521. Ok(seed.deserialize(de).ok())
  522. } else {
  523. Ok(None)
  524. }
  525. }
  526. fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
  527. where
  528. V: serde::de::DeserializeSeed<'de>,
  529. {
  530. let value = self.it.next().unwrap();
  531. let de = AtomicForwarder {
  532. to_fwd: value.1.as_str(),
  533. };
  534. seed.deserialize(de)
  535. .map_err(|e| ConfigError::InvalidType(e.to_string()))
  536. }
  537. }