From 117d55293950c0e4faebfd536f17d062abb55da6 Mon Sep 17 00:00:00 2001 From: kageru Date: Sat, 13 Jun 2020 09:44:26 +0200 Subject: [PATCH] some cleanup --- src/main.rs | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index d5dd324..810fec1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,8 @@ #[macro_use] extern crate pest_derive; -use pest::iterators::Pair; use pest::Parser; use serde::de; use serde::de::value::MapDeserializer; -use serde::de::DeserializeOwned; use serde::forward_to_deserialize_any; use serde::Deserialize; mod error; @@ -14,11 +12,11 @@ use error::MpdResult; #[grammar = "response.pest"] struct MpdParser; -struct MPDeserializer<'de, Iter: Iterator> { +struct MPDeserializer<'de, Iter: Iterator> { inner: MapDeserializer<'de, Iter, error::Error>, } -impl<'de, Iter: Iterator> de::Deserializer<'de> +impl<'de, Iter: Iterator> de::Deserializer<'de> for MPDeserializer<'de, Iter> { type Error = error::Error; @@ -45,15 +43,12 @@ impl<'de, Iter: Iterator> de::Deserializer<'de> } #[derive(Debug, Deserialize)] +#[serde(rename_all = "PascalCase")] struct Song { - Title: String, - Artist: String, + title: String, + artist: String, } -const input_var: &str = "Title: A song 星 -Artist: A name -OK mpd 0.21.23"; - fn main() { /* let parsed = MpdParser::parse(Rule::response, input) @@ -75,30 +70,32 @@ fn main() { let d = Song::deserialize(&mut deser); println!("{:?}", d); */ + let input_var: &str = "Title: A song 星 +Artist: A name +OK mpd 0.21.23"; + let mut map = std::collections::HashMap::new(); let parser = MpdParser::parse(Rule::response, input_var) .unwrap() .next() .unwrap(); - for pair in parser.into_inner() { - let rule = pair.as_rule(); - match rule { + for line in parser.into_inner() { + match line.as_rule() { Rule::kv => { - let mut fields = pair.into_inner(); + let mut fields = line.into_inner(); map.insert( - fields.next().unwrap().as_str().to_string(), - fields.next().unwrap().as_str().to_string(), + fields.next().unwrap().as_str(), + fields.next().unwrap().as_str(), ); } - Rule::ok => (), + Rule::ok => break, Rule::err => panic!("received error response"), _ => unreachable!(), } } - // ::<'_, _, Song> dbg!(&map); let deser = MPDeserializer { - inner: serde::de::value::MapDeserializer::new(map.into_iter()), + inner: MapDeserializer::new(map.into_iter()), }; let s = Song::deserialize(deser); println!("{:?}", s);