123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621 |
- #![allow(unused_variables)]
- use crate::schema;
- use microrm::prelude::*;
- use super::Config;
- struct ValueToStringSerializer {}
- impl<'l> serde::Serializer for &'l ValueToStringSerializer {
- type Ok = Option<String>;
- type Error = ConfigError;
- type SerializeSeq = serde::ser::Impossible<Self::Ok, Self::Error>;
- type SerializeMap = serde::ser::Impossible<Self::Ok, Self::Error>;
- type SerializeTuple = serde::ser::Impossible<Self::Ok, Self::Error>;
- type SerializeStruct = serde::ser::Impossible<Self::Ok, Self::Error>;
- type SerializeTupleStruct = serde::ser::Impossible<Self::Ok, Self::Error>;
- type SerializeTupleVariant = serde::ser::Impossible<Self::Ok, Self::Error>;
- type SerializeStructVariant = serde::ser::Impossible<Self::Ok, Self::Error>;
- fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
- unreachable!()
- }
- fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
- self.serialize_i64(v as i64)
- }
- fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
- self.serialize_i64(v as i64)
- }
- fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
- self.serialize_i64(v as i64)
- }
- fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
- Ok(Some(v.to_string()))
- }
- fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
- unreachable!()
- }
- fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
- self.serialize_u64(v as u64)
- }
- fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
- self.serialize_u64(v as u64)
- }
- fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
- self.serialize_u64(v as u64)
- }
- fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
- Ok(Some(v.to_string()))
- }
- fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
- unreachable!()
- }
- fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
- unreachable!()
- }
- fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> {
- unreachable!()
- }
- fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> {
- unreachable!()
- }
- fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
- unreachable!()
- }
- fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
- Ok(Some(v.into()))
- }
- fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
- Ok(None)
- }
- fn serialize_some<T: ?Sized + serde::Serialize>(
- self,
- value: &T,
- ) -> Result<Self::Ok, Self::Error> {
- value.serialize(self)
- }
- fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
- unreachable!()
- }
- fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
- unreachable!()
- }
- fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
- unreachable!()
- }
- fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
- unreachable!()
- }
- fn serialize_struct(
- self,
- _name: &'static str,
- _len: usize,
- ) -> Result<Self::SerializeStruct, Self::Error> {
- unreachable!()
- }
- fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
- unreachable!()
- }
- fn serialize_unit_variant(
- self,
- name: &'static str,
- variant_index: u32,
- variant: &'static str,
- ) -> Result<Self::Ok, Self::Error> {
- unreachable!()
- }
- fn serialize_tuple_struct(
- self,
- name: &'static str,
- len: usize,
- ) -> Result<Self::SerializeTupleStruct, Self::Error> {
- unreachable!()
- }
- fn serialize_tuple_variant(
- self,
- name: &'static str,
- variant_index: u32,
- variant: &'static str,
- len: usize,
- ) -> Result<Self::SerializeTupleVariant, Self::Error> {
- todo!()
- }
- fn serialize_newtype_struct<T: ?Sized + serde::Serialize>(
- self,
- name: &'static str,
- value: &T,
- ) -> Result<Self::Ok, Self::Error> {
- todo!()
- }
- fn serialize_struct_variant(
- self,
- name: &'static str,
- variant_index: u32,
- variant: &'static str,
- len: usize,
- ) -> Result<Self::SerializeStructVariant, Self::Error> {
- todo!()
- }
- fn serialize_newtype_variant<T: ?Sized + serde::Serialize>(
- self,
- name: &'static str,
- variant_index: u32,
- variant: &'static str,
- value: &T,
- ) -> Result<Self::Ok, Self::Error> {
- todo!()
- }
- }
- pub struct ConfigSerializer<'r, 's> {
- pub config: &'r Config,
- pub db: &'s schema::UIDCDatabase,
- pub prefix: String,
- }
- impl<'r, 's> ConfigSerializer<'r, 's> {
- fn update(&self, key: &str, value: String) {
- // TODO: delete old config value
- // self.db.persistent_config.delete(schema::PersistentConfig { key: key.into(), value }).expect("couldn't update config");
- self.db
- .persistent_config
- .insert(schema::PersistentConfig {
- key: key.into(),
- value,
- })
- .expect("couldn't update config");
- }
- }
- impl<'r, 's> serde::ser::SerializeStruct for ConfigSerializer<'r, 's> {
- type Ok = ();
- type Error = ConfigError;
- fn serialize_field<T: ?Sized + serde::Serialize>(
- &mut self,
- key: &'static str,
- value: &T,
- ) -> Result<(), Self::Error> {
- let key = format!("{}{}", self.prefix, key);
- let value = value.serialize(&ValueToStringSerializer {})?;
- if let Some(value) = value {
- log::trace!("saving config {} = {}", key, value);
- self.update(key.as_str(), value);
- }
- Ok(())
- }
- fn end(self) -> Result<Self::Ok, Self::Error> {
- Ok(())
- }
- }
- impl<'r, 's> serde::Serializer for &'r ConfigSerializer<'r, 's> {
- type Ok = ();
- type Error = ConfigError;
- type SerializeSeq = serde::ser::Impossible<Self::Ok, Self::Error>;
- type SerializeMap = serde::ser::Impossible<Self::Ok, Self::Error>;
- type SerializeTuple = serde::ser::Impossible<Self::Ok, Self::Error>;
- type SerializeStruct = ConfigSerializer<'r, 's>;
- type SerializeTupleStruct = serde::ser::Impossible<Self::Ok, Self::Error>;
- type SerializeTupleVariant = serde::ser::Impossible<Self::Ok, Self::Error>;
- type SerializeStructVariant = serde::ser::Impossible<Self::Ok, Self::Error>;
- fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
- unreachable!()
- }
- fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
- self.serialize_i64(v as i64)
- }
- fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
- self.serialize_i64(v as i64)
- }
- fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
- self.serialize_i64(v as i64)
- }
- fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
- todo!()
- }
- fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
- unreachable!()
- }
- fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
- self.serialize_u64(v as u64)
- }
- fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
- self.serialize_u64(v as u64)
- }
- fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
- self.serialize_u64(v as u64)
- }
- fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
- todo!()
- }
- fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
- unreachable!()
- }
- fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
- unreachable!()
- }
- fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> {
- unreachable!()
- }
- fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> {
- unreachable!()
- }
- fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
- todo!()
- }
- fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
- todo!()
- }
- fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
- Ok(())
- }
- fn serialize_some<T: ?Sized + serde::Serialize>(
- self,
- value: &T,
- ) -> Result<Self::Ok, Self::Error> {
- todo!()
- }
- fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
- unreachable!()
- }
- fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
- todo!()
- }
- fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
- todo!()
- }
- fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
- todo!()
- }
- fn serialize_struct(
- self,
- name: &'static str,
- _len: usize,
- ) -> Result<Self::SerializeStruct, Self::Error> {
- log::trace!("name: {name}");
- let new_prefix =
- // are we at the root?
- if name == "Config" {
- String::new()
- }
- else {
- format!("{}{}.", self.prefix, name)
- };
- let subser = ConfigSerializer {
- config: self.config,
- db: self.db,
- prefix: new_prefix,
- };
- Ok(subser)
- }
- fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
- todo!()
- }
- fn serialize_unit_variant(
- self,
- name: &'static str,
- variant_index: u32,
- variant: &'static str,
- ) -> Result<Self::Ok, Self::Error> {
- todo!()
- }
- fn serialize_tuple_struct(
- self,
- name: &'static str,
- len: usize,
- ) -> Result<Self::SerializeTupleStruct, Self::Error> {
- todo!()
- }
- fn serialize_tuple_variant(
- self,
- name: &'static str,
- variant_index: u32,
- variant: &'static str,
- len: usize,
- ) -> Result<Self::SerializeTupleVariant, Self::Error> {
- todo!()
- }
- fn serialize_newtype_struct<T: ?Sized + serde::Serialize>(
- self,
- name: &'static str,
- value: &T,
- ) -> Result<Self::Ok, Self::Error> {
- todo!()
- }
- fn serialize_struct_variant(
- self,
- name: &'static str,
- variant_index: u32,
- variant: &'static str,
- len: usize,
- ) -> Result<Self::SerializeStructVariant, Self::Error> {
- todo!()
- }
- fn serialize_newtype_variant<T: ?Sized + serde::Serialize>(
- self,
- name: &'static str,
- variant_index: u32,
- variant: &'static str,
- value: &T,
- ) -> Result<Self::Ok, Self::Error> {
- todo!()
- }
- }
- pub struct ConfigDeserializer<'de> {
- pub config_map: &'de std::collections::HashMap<String, String>,
- pub prefix: String,
- }
- #[derive(Debug)]
- pub enum ConfigError {
- Missing(String),
- InvalidType(String),
- CustomError(String),
- }
- impl std::fmt::Display for ConfigError {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- match self {
- Self::Missing(what) => f.write_fmt(format_args!(
- "Missing required config entry: {}",
- what.as_str()
- )),
- Self::InvalidType(what) => f.write_fmt(format_args!(
- "Could not parse config entry '{}'",
- what.as_str()
- )),
- Self::CustomError(what) => {
- f.write_fmt(format_args!("Custom error '{}'", what.as_str()))
- }
- }
- }
- }
- impl std::error::Error for ConfigError {}
- impl serde::ser::Error for ConfigError {
- fn custom<T>(msg: T) -> Self
- where
- T: std::fmt::Display,
- {
- Self::CustomError(msg.to_string())
- }
- }
- impl serde::de::Error for ConfigError {
- fn custom<T>(msg: T) -> Self
- where
- T: std::fmt::Display,
- {
- Self::CustomError(msg.to_string())
- }
- fn invalid_type(_unexp: serde::de::Unexpected, _exp: &dyn serde::de::Expected) -> Self {
- Self::InvalidType("".into())
- }
- fn missing_field(field: &'static str) -> Self {
- Self::Missing(field.into())
- }
- }
- impl<'de> serde::Deserializer<'de> for &'de mut ConfigDeserializer<'de> {
- type Error = ConfigError;
- fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
- where
- V: serde::de::Visitor<'de>,
- {
- unreachable!("deserialize_any needs context")
- }
- fn deserialize_struct<V>(
- self,
- _name: &'static str,
- _fields: &'static [&'static str],
- visitor: V,
- ) -> Result<V::Value, Self::Error>
- where
- V: serde::de::Visitor<'de>,
- {
- self.deserialize_map(visitor)
- }
- fn deserialize_seq<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
- where
- V: serde::de::Visitor<'de>,
- {
- todo!("deserialize_seq")
- }
- fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
- where
- V: serde::de::Visitor<'de>,
- {
- let mut map_access = ConfigDeserializerIterator {
- it: self
- .config_map
- .iter()
- .filter(|e| {
- e.0.starts_with(&self.prefix) && !e.0[self.prefix.len()..].contains('.')
- })
- .peekable(),
- };
- visitor.visit_map(&mut map_access)
- }
- fn deserialize_enum<V>(
- self,
- _name: &'static str,
- _variants: &'static [&'static str],
- _visitor: V,
- ) -> Result<V::Value, Self::Error>
- where
- V: serde::de::Visitor<'de>,
- {
- todo!("deserialize_enum")
- }
- fn deserialize_tuple<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error>
- where
- V: serde::de::Visitor<'de>,
- {
- todo!("deserialize_tuple")
- }
- fn deserialize_tuple_struct<V>(
- self,
- _name: &'static str,
- _len: usize,
- _visitor: V,
- ) -> Result<V::Value, Self::Error>
- where
- V: serde::de::Visitor<'de>,
- {
- todo!("deserialize_tuple_struct")
- }
- serde::forward_to_deserialize_any!(
- i8 u8 i16 u16 i32 u32 i64 u64 i128 u128 str string bytes
- bool f32 f64 char byte_buf option unit unit_struct
- newtype_struct identifier ignored_any
- );
- }
- struct AtomicForwarder<'de> {
- to_fwd: &'de str,
- }
- impl<'de> serde::Deserializer<'de> for AtomicForwarder<'de> {
- type Error = ConfigError;
- fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
- where
- V: serde::de::Visitor<'de>,
- {
- visitor.visit_unit()
- }
- fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
- where
- V: serde::de::Visitor<'de>,
- {
- visitor.visit_u64(
- self.to_fwd
- .parse()
- .map_err(|_| ConfigError::InvalidType(String::new()))?,
- )
- }
- fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
- where
- V: serde::de::Visitor<'de>,
- {
- visitor.visit_str(self.to_fwd)
- }
- fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
- where
- V: serde::de::Visitor<'de>,
- {
- visitor.visit_str(self.to_fwd)
- }
- fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
- where
- V: serde::de::Visitor<'de>,
- {
- visitor.visit_str(self.to_fwd)
- }
- serde::forward_to_deserialize_any!(
- i8 u8 i16 u16 i32 u32 i64 i128 u128 bytes
- bool f32 f64 char byte_buf unit unit_struct option
- newtype_struct ignored_any struct tuple tuple_struct
- seq map enum
- );
- }
- struct ConfigDeserializerIterator<'de, I: Iterator<Item = (&'de String, &'de String)>> {
- it: std::iter::Peekable<I>,
- }
- impl<'de, I: Iterator<Item = (&'de String, &'de String)>> serde::de::MapAccess<'de>
- for ConfigDeserializerIterator<'de, I>
- {
- type Error = ConfigError;
- fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
- where
- K: serde::de::DeserializeSeed<'de>,
- {
- if let Some(e) = self.it.peek() {
- let de = AtomicForwarder {
- to_fwd: e.0.as_str(),
- };
- Ok(seed.deserialize(de).ok())
- } else {
- Ok(None)
- }
- }
- fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
- where
- V: serde::de::DeserializeSeed<'de>,
- {
- let value = self.it.next().unwrap();
- let de = AtomicForwarder {
- to_fwd: value.1.as_str(),
- };
- seed.deserialize(de)
- .map_err(|e| ConfigError::InvalidType(e.to_string()))
- }
- }
|