helper.rs 17 KB

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