You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.4KB

  1. use serde::{Deserialize, Deserializer};
  2. use tera::{Map, Value};
  3. use toml;
  4. /// Used as an attribute when we want to convert from TOML to a string date
  5. pub fn from_toml_datetime<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
  6. where
  7. D: Deserializer<'de>,
  8. {
  9. toml::value::Datetime::deserialize(deserializer).map(|s| Some(s.to_string()))
  10. }
  11. /// Returns key/value for a converted date from TOML.
  12. /// If the table itself is the TOML struct, only return its value without the key
  13. fn convert_toml_date(table: Map<String, Value>) -> Value {
  14. let mut new = Map::new();
  15. for (k, v) in table {
  16. if k == "$__toml_private_datetime" {
  17. return v;
  18. }
  19. match v {
  20. Value::Object(o) => {
  21. new.insert(k, convert_toml_date(o));
  22. }
  23. _ => {
  24. new.insert(k, v);
  25. }
  26. }
  27. }
  28. Value::Object(new)
  29. }
  30. /// TOML datetimes will be serialized as a struct but we want the
  31. /// stringified version for json, otherwise they are going to be weird
  32. pub fn fix_toml_dates(table: Map<String, Value>) -> Value {
  33. let mut new = Map::new();
  34. for (key, value) in table {
  35. match value {
  36. Value::Object(o) => {
  37. new.insert(key, convert_toml_date(o));
  38. }
  39. _ => {
  40. new.insert(key, value);
  41. }
  42. }
  43. }
  44. Value::Object(new)
  45. }