From: Ian Jackson Date: Tue, 17 Nov 2020 19:45:23 +0000 (+0000) Subject: wip toml, doex not work, progressing X-Git-Tag: otter-0.2.0~497 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=e7213411db093b7fdfc10d86ed9f09efc36fbc1a;p=otter.git wip toml, doex not work, progressing Signed-off-by: Ian Jackson --- diff --git a/src/config.rs b/src/config.rs index 4e1027b7..68a0faf2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -117,7 +117,7 @@ impl ServerConfig { let mut buf = String::new(); File::open(&config_filename).with_context(||config_filename.to_string())? .read_to_string(&mut buf)?; - let config : ServerConfigSpec = toml::de::from_str(&buf)?; + let config : ServerConfigSpec = crate::toml::from_str(&buf)?; let config = config.try_into()?; set_config(config); } diff --git a/src/toml.rs b/src/toml.rs index fa19da02..e5684fe2 100644 --- a/src/toml.rs +++ b/src/toml.rs @@ -2,8 +2,7 @@ // SPDX-License-Identifier: AGPL-3.0-or-later // There is NO WARRANTY. -use std::convert::TryInto; -use std::fmt::{self, Debug, Display}; +use std::fmt::{Debug, Display}; use std::iter::Peekable; use std::slice; @@ -15,17 +14,10 @@ use thiserror::Error; #[derive(Error,Debug)] pub enum Error { + #[error("deserialize failed (improper TOML structure?): {0}")] Custom(Box), -} - -impl Display for Error { - #[throws(fmt::Error)] - fn fmt(&self, f: &mut fmt::Formatter) { - type E = Error; - match self { - E::Custom(x) => write!(f, "toml::TomlDe::Error::Custom:{}", &x)?, - } - } + #[error("config file has invalid TOML syntax: {0}")] + TomlSyntax(#[from] toml::de::Error), } impl serde::de::Error for Error { @@ -59,15 +51,18 @@ struct MA<'de> (Peekable>); impl<'de> MapAccess<'de> for MA<'de> { type Error = Error; - #[throws(Error)] fn next_key_seed> - (&mut self, seed: K) -> Option + (&mut self, seed: K) -> Result, Error> { - if let Some((k, _v)) = self.0.peek() { - Some(seed.deserialize(k.as_str().into_deserializer())?) + Ok(if let Some((k, _v)) = self.0.peek() { + Some(seed.deserialize({ + let q : serde::de::value::StrDeserializer<'_, Error> = + k.as_str().into_deserializer(); + q + })?) } else { None - } + }) } #[throws(Error)] fn next_value_seed> @@ -82,11 +77,11 @@ impl<'de> MapAccess<'de> for MA<'de> { fn visit<'de, V: Visitor<'de>>(v: V, tv: &'de toml::Value) -> V::Value { type TV = toml::Value; match tv { - TV::String(s) => v.visit_borrowed_str(s)?, - &TV::Integer(i) => v.visit_i64(i)?, - &TV::Float(f) => v.visit_f64(f)?, - &TV::Boolean(b) => v.visit_bool(b)?, - TV::Datetime(dt) => v.visit_str(&dt.to_string())?, + TV::String(s) => v.visit_borrowed_str::(s)?, + &TV::Integer(i) => v.visit_i64::(i)?, + &TV::Float(f) => v.visit_f64::(f)?, + &TV::Boolean(b) => v.visit_bool::(b)?, + TV::Datetime(dt) => v.visit_str::(&dt.to_string())?, TV::Array(a) => v.visit_seq(SA(a.as_slice().iter()))?, TV::Table(t) => v.visit_map(MA(t.iter().peekable()))?, } @@ -114,8 +109,7 @@ pub fn from_value<'de, T: Deserialize<'de>> (tv: &'de toml::Value) -> T #[throws(Error)] pub fn from_str (s: &str) -> T { - let tv : toml::Value = s.try_into() - .map_err(|e| match e { }) - ?; + let tv : toml::Value = s.parse()?; + eprintln!("TOML FROM STR {:?}", tv); from_value(&tv)? }