diff --git a/Cargo.toml b/Cargo.toml index 8e569cfe6..dda192bd8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,3 @@ [workspace] -members = ["copper", "copper_log_test", "copper_derive", "copper_derive_test", "copper_log", "examples/config_gen", "examples/pluginload", "examples/simplelogger", "examples/v4lsrc"] +members = ["copper", "copper_log_test", "copper_derive", "copper_derive_test", "copper_log", "examples/config_gen", "examples/pluginload", "examples/simplelogger", "examples/v4lsrc", "copper_log_runtime"] resolver = "2" diff --git a/copper_log/src/index.rs b/copper_log/src/index.rs index 21f7dc67c..2ab0d26cf 100644 --- a/copper_log/src/index.rs +++ b/copper_log/src/index.rs @@ -42,22 +42,16 @@ lazy_static! { )) }; } - -pub fn check_and_insert(filename: &str, line_number: u32, log_string: &str) -> Option { - let (counter_store, index_to_string, string_to_index, index_to_callsite) = - &mut *DBS.lock().unwrap(); +pub fn intern_string(s: &str) -> Option { + let (counter_store, index_to_string, string_to_index, _) = &mut *DBS.lock().unwrap(); let index = { let env = RKV.lock().unwrap(); // If this string already exists in the store, return the index { let reader = env.read().unwrap(); - let entry = string_to_index.get(&reader, log_string); // check if log_string is already in the string_to_index store - if let Ok(Some(Value::U64(index))) = string_to_index.get(&reader, log_string) { - println!( - "CLog: Returning existing index #{} -> {}", - index, log_string - ); + if let Ok(Some(Value::U64(index))) = string_to_index.get(&reader, s) { + println!("CLog: Returning existing index #{} -> {}", index, s); return Some(index as IndexType); }; } @@ -65,26 +59,36 @@ pub fn check_and_insert(filename: &str, line_number: u32, log_string: &str) -> O let next_index = get_next_index(&mut writer, counter_store).unwrap(); // Insert the new string into the store index_to_string - .put( - &mut writer, - next_index.to_le_bytes(), - &Value::Str(log_string), - ) + .put(&mut writer, next_index.to_le_bytes(), &Value::Str(s)) .unwrap(); string_to_index - .put(&mut writer, log_string, &Value::U64(next_index as u64)) + .put(&mut writer, s, &Value::U64(next_index as u64)) .unwrap(); + writer.commit().unwrap(); + Some(next_index) + }; + println!("CLog: Inserted #{} -> {}", index.unwrap(), s); + index +} + +pub fn check_and_insert(filename: &str, line_number: u32, log_string: &str) -> Option { + let index = intern_string(log_string); + { + let (_, _, _, index_to_callsite) = &mut *DBS.lock().unwrap(); + index?; + let lindex = index.unwrap(); + let env = RKV.lock().unwrap(); + let mut writer = env.write().unwrap(); index_to_callsite .put( &mut writer, - next_index.to_le_bytes(), + lindex.to_le_bytes(), &Value::Str(format!("{}:{}", filename, line_number).as_str()), ) .unwrap(); writer.commit().unwrap(); - Some(next_index) - }; - println!("CLog: Inserted #{} -> {}", index.unwrap(), log_string); + println!("CLog: Inserted #{} -> {}", lindex, log_string); + } index } diff --git a/copper_log/src/macros.rs b/copper_log/src/macros.rs index 1b38b6af3..847f44df0 100644 --- a/copper_log/src/macros.rs +++ b/copper_log/src/macros.rs @@ -2,13 +2,12 @@ mod index; extern crate proc_macro; -use crate::index::check_and_insert; +use crate::index::{check_and_insert, intern_string}; use proc_macro::TokenStream; -use proc_macro2::{Span, TokenTree}; use quote::quote; use syn::parse::Parser; use syn::Token; -use syn::{custom_keyword, parse_macro_input, Expr, ExprAssign, ExprLit, Lit, LitStr}; +use syn::{Expr, ExprAssign, ExprLit, Lit}; #[proc_macro] pub fn debug(input: TokenStream) -> TokenStream { @@ -28,7 +27,16 @@ pub fn debug(input: TokenStream) -> TokenStream { } else { panic!("The first parameter of the argument needs to be a string literal."); }; - println!("{} -> [{}]", index, msg); + let prefix = quote! { + use copper_log_runtime::value::Value; + use copper_log_runtime::value::to_value; + use copper_log_runtime::ANONYMOUS; + let msg = #msg; + let index = #index; + println!("{} -> [{}]", index, msg); + let mut params = Vec::<(Value, Value)>::new(); + + }; let mut unnamed_params = vec![]; let mut named_params = vec![]; @@ -43,20 +51,34 @@ pub fn debug(input: TokenStream) -> TokenStream { let unnamed_prints = unnamed_params.iter().map(|param| { quote! { - println!("{:?}", #param); + // 0 means an unnamed parameter + let param = (ANONYMOUS, to_value(#param).expect("Failed to convert a parameter to a Value")); + println!("anon param: {:?}", ¶m); + params.push(param); } }); let named_prints = named_params.iter().map(|(name, value)| { + let index = intern_string(quote!(#name).to_string().as_str()) + .expect("Failed to insert log string."); quote! { - println!("{} = {:?}", stringify!(#name), #value); + let param = (to_value(#index).unwrap(), to_value(#value).expect("Failed to convert a parameter to a Value")); + println!("named param: {:?}", ¶m); + params.push(param); } }); + let postfix = quote! { + println!("Would send {:?}", (index, params)); + }; + let expanded = quote! { - println!("Message: {}", #msg); - #(#unnamed_prints)* - #(#named_prints)* + { + #prefix + #(#unnamed_prints)* + #(#named_prints)* + #postfix + } }; expanded.into() diff --git a/copper_log_runtime/Cargo.toml b/copper_log_runtime/Cargo.toml new file mode 100644 index 000000000..b5e72237d --- /dev/null +++ b/copper_log_runtime/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "copper-log-runtime" +version = "0.1.0" +edition = "2021" + +[dependencies] +copper-value = { path = "../copper_value" } diff --git a/copper_log_runtime/src/lib.rs b/copper_log_runtime/src/lib.rs new file mode 100644 index 000000000..3d3440dd7 --- /dev/null +++ b/copper_log_runtime/src/lib.rs @@ -0,0 +1,3 @@ +pub use copper_value as value; +#[allow(dead_code)] +pub const ANONYMOUS: value::Value = value::Value::U32(0); diff --git a/copper_log_test/Cargo.toml b/copper_log_test/Cargo.toml index df9c395f7..b45d7276b 100644 --- a/copper_log_test/Cargo.toml +++ b/copper_log_test/Cargo.toml @@ -5,3 +5,10 @@ edition = "2021" [dependencies] copper-log = { path = "../copper_log" } +copper-log-runtime = { path = "../copper_log_runtime" } +copper-value = { path = "../copper_value" } +serde = { version = "1.0.202", features = ["derive"] } + +[build-dependencies] +copper-log = { path = "../copper_log" } + diff --git a/copper_log_test/src/main.rs b/copper_log_test/src/main.rs index 7d9bb5154..e5608066f 100644 --- a/copper_log_test/src/main.rs +++ b/copper_log_test/src/main.rs @@ -1,8 +1,25 @@ use copper_log::debug; +use copper_value::to_value; +use serde::Serialize; + fn main() { - let a = 2; - let b = 3; - // debug!("Hello, world! {} {}", a, b); - debug!("Hello, world!"); - debug!("Hello, world2!", a); + #[derive(Serialize)] + struct Test { + a: i32, + b: i32, + } + let t = Test { a: 2, b: 3 }; + + let v = to_value(t).unwrap(); + + println!("{:?}", v); + + let mytuple = (1, "toto", 3.34f64, true, 'a'); + let v = to_value(mytuple).unwrap(); + println!("{:?}", v); + debug!("Just a string"); + debug!("anonymous param constants {} {}", 3u16, 2u8); + debug!("named param constants {} {}", a = 3, b = 2); + debug!("mixed named param constants, {} {} {}", a = 3, 54, b = 2); + debug!("a tuple {}", mytuple); } diff --git a/copper_value/.gitignore b/copper_value/.gitignore new file mode 100644 index 000000000..2c1d75ebb --- /dev/null +++ b/copper_value/.gitignore @@ -0,0 +1,5 @@ +/target/ +/Cargo.lock +/.gitattributes +/.idea/ +*.iml diff --git a/copper_value/.travis.yml b/copper_value/.travis.yml new file mode 100644 index 000000000..6bb049fe6 --- /dev/null +++ b/copper_value/.travis.yml @@ -0,0 +1,37 @@ +language: rust +rust: + - nightly + - beta + - stable +sudo: false +os: linux +cache: + directories: + - $HOME/.cargo + - target +env: + global: + - secure: "RFoJK/j05ZzCrx3zJJjqn8FqL2UwAH0iSTE/pTNsEn4Xl1DFTC/ubb+cu8gFS8wpTY0+zPSmBrHp3R0MNKIpbQqZ2WexEpbzNEdpftcW+eUlEj45HvNWw3LdKn8w0O7txdRxbUVzgDgto+XGsFypMsA5wxjOryYDJWPnpXvt1QL520ro0/IzYToubBj0e6hhlXXvDY0gr4ip/Y/dooWJ0XmPdmHi1atHLBYaH5l0qd5Rc6pfqvj+ctaZQyVV6JEK84ODUeK6IHbthl45/ZwecMZW/egAXX2CCViv2FmCkqoIjGynPd5dVTMMZGYVEYpDMzdkvYze6JoNsj2YZkO3WprpTqUTgwf+7XdRIce0X7rIVm6P5fBiN/8NPcRVgfBR6WthJoRfQYdjkqhrg70wcBrJxRUulPxNxwcCkC9RDEbI8ZlG4ZhNKfAVzV0xMwztcJoz5qqsMSAkWc8HIAZ7CL9zplLe6P7tomaLcWFxkyvGsdOXCHtyBnp8TOwgvG4kGzuZ7myEOrGodWn23F9e5h00Kio9k/j1HDa7+ESQ7UW6MJu4YT+5XTTJi8Dr+kdrUVDwFirab8+A8/tfFmQxZhZdzny48uTjGeDFqmr4hQ+hC3elg+ZDpEl72/A8HMQcpxtpiBkyky0B5sJSCUOeCU8A5DN+UJN/9d6my+10flA=" + - secure: "GNs+Daj2ses715lQWto6a+zFWjBBZ/4Rt/F6j0rpqYjm7NEForoRdDmRWHbN3xGSrxT83G7yuPdGRlAKG1AjM91Dxi2fEw/THvT61QXB5LSW21/2h7WETAJYoBzlFwAPo3RVM0tzkjPQfAm5C3zxbPXjo42D5GQkF50vKixBQZ5QG2FIQHAv80Ed9y2aozf6Z5V6lojjLm2xEfdSNA8Mj8/N3ux2eWESddufk6wdEoRkcpRe2e09TZaeamYQ48yj1paDPKNLFhyRFehtOFGdMQQgbWJ1GsYgtWkHu8f7ohuC4Pdx94f0xTApz+jpeADtDmgX8Cvd2IVCUEB8WLMJv2W+Zp2xrn1Ufr88uCzOUT78c6kiRJfQ9n9IEZSTuie/JtwgjlWuud1raarDDfmRKAGWHRx0WL1TKt4ADuy63pyV78FlLNnMTJ9BwZhUpxQqIbKtw31eIwWxkUiwpvEQ1IYLts4U9rSi8xjnLhA7B97ahIq1pMOWnC4HM2jCggCw2AQtkNNM6vuPli//3i1aF02JrLnlwsXparxnQ70HmyaxX0u38Tgr8zbobtJE8+TsDh2d3uFcI6BNARmsO7I1ifDbPj6aB2JOYK4M8knoP3MG28jO5v3qVXrEmfWm361LMNkmw1cM2MnDBq3uxKWhGZzW4XB72kcSzYgFNzUaaUw=" +before_script: + - curl -L https://github.com/arcnmx/ci/archive/master.tar.gz | tar -xzC $HOME && . $HOME/ci-master/src + +script: + - cargo build + - cargo test + +deploy: + provider: script + script: "true" + on: + tags: true + all_branches: true + condition: "$TRAVIS_RUST_VERSION = stable" + +before_deploy: + - cargo doc + - cargo package + +after_deploy: + - cargo pages-publish + - cargo publish diff --git a/copper_value/COPYING b/copper_value/COPYING new file mode 100644 index 000000000..fe07e2433 --- /dev/null +++ b/copper_value/COPYING @@ -0,0 +1,19 @@ +Copyright (c) 2016 arcnmx + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/copper_value/Cargo.toml b/copper_value/Cargo.toml new file mode 100644 index 000000000..40bd8fc2b --- /dev/null +++ b/copper_value/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "copper-value" +version = "0.7.0" +authors = ["arcnmx", "gbin"] +edition = "2021" + +description = "Serialization value trees" +readme = "README.md" +keywords = ["serde"] +license = "MIT" + +[dependencies] +serde = "1.0.202" +ordered-float = "4.2.0" + +[dev-dependencies] +serde_derive = "1.0.202" diff --git a/copper_value/README.md b/copper_value/README.md new file mode 100644 index 000000000..ba4047d7b --- /dev/null +++ b/copper_value/README.md @@ -0,0 +1,9 @@ +# copper-value + +[![license-badge][]][license] + +`copper-value` provides a way to capture serialization value trees for later processing. +Customizations are made to enable a more compact representation for the structured logging of copper. + +[license-badge]: https://img.shields.io/badge/license-MIT-lightgray.svg?style=flat-square +[license]: https://github.com/arcnmx/serde-value/blob/master/COPYING diff --git a/copper_value/src/de.rs b/copper_value/src/de.rs new file mode 100644 index 000000000..97dc1155e --- /dev/null +++ b/copper_value/src/de.rs @@ -0,0 +1,541 @@ +use serde::{forward_to_deserialize_any, de}; +use std::collections::BTreeMap; +use std::error::Error; +use std::fmt; +use std::marker::PhantomData; + +use crate::Value; + +#[derive(Debug)] +pub enum Unexpected { + Bool(bool), + Unsigned(u64), + Signed(i64), + Float(f64), + Char(char), + Str(String), + Bytes(Vec), + Unit, + Option, + NewtypeStruct, + Seq, + Map, + Enum, + UnitVariant, + NewtypeVariant, + TupleVariant, + StructVariant, + Other(String), +} + +impl<'a> From> for Unexpected { + fn from(unexp: de::Unexpected) -> Unexpected { + match unexp { + de::Unexpected::Bool(v) => Unexpected::Bool(v), + de::Unexpected::Unsigned(v) => Unexpected::Unsigned(v), + de::Unexpected::Signed(v) => Unexpected::Signed(v), + de::Unexpected::Float(v) => Unexpected::Float(v), + de::Unexpected::Char(v) => Unexpected::Char(v), + de::Unexpected::Str(v) => Unexpected::Str(v.to_owned()), + de::Unexpected::Bytes(v) => Unexpected::Bytes(v.to_owned()), + de::Unexpected::Unit => Unexpected::Unit, + de::Unexpected::Option => Unexpected::Option, + de::Unexpected::NewtypeStruct => Unexpected::NewtypeStruct, + de::Unexpected::Seq => Unexpected::Seq, + de::Unexpected::Map => Unexpected::Map, + de::Unexpected::Enum => Unexpected::Enum, + de::Unexpected::UnitVariant => Unexpected::UnitVariant, + de::Unexpected::NewtypeVariant => Unexpected::NewtypeVariant, + de::Unexpected::TupleVariant => Unexpected::TupleVariant, + de::Unexpected::StructVariant => Unexpected::StructVariant, + de::Unexpected::Other(v) => Unexpected::Other(v.to_owned()), + } + } +} + +impl Unexpected { + pub fn to_unexpected<'a>(&'a self) -> de::Unexpected<'a> { + match *self { + Unexpected::Bool(v) => de::Unexpected::Bool(v), + Unexpected::Unsigned(v) => de::Unexpected::Unsigned(v), + Unexpected::Signed(v) => de::Unexpected::Signed(v), + Unexpected::Float(v) => de::Unexpected::Float(v), + Unexpected::Char(v) => de::Unexpected::Char(v), + Unexpected::Str(ref v) => de::Unexpected::Str(v), + Unexpected::Bytes(ref v) => de::Unexpected::Bytes(v), + Unexpected::Unit => de::Unexpected::Unit, + Unexpected::Option => de::Unexpected::Option, + Unexpected::NewtypeStruct => de::Unexpected::NewtypeStruct, + Unexpected::Seq => de::Unexpected::Seq, + Unexpected::Map => de::Unexpected::Map, + Unexpected::Enum => de::Unexpected::Enum, + Unexpected::UnitVariant => de::Unexpected::UnitVariant, + Unexpected::NewtypeVariant => de::Unexpected::NewtypeVariant, + Unexpected::TupleVariant => de::Unexpected::TupleVariant, + Unexpected::StructVariant => de::Unexpected::StructVariant, + Unexpected::Other(ref v) => de::Unexpected::Other(v), + } + } + +} + +#[derive(Debug)] +pub enum DeserializerError { + Custom(String), + InvalidType(Unexpected, String), + InvalidValue(Unexpected, String), + InvalidLength(usize, String), + UnknownVariant(String, &'static [&'static str]), + UnknownField(String, &'static [&'static str]), + MissingField(&'static str), + DuplicateField(&'static str), +} + +impl de::Error for DeserializerError { + fn custom(msg: T) -> Self { + DeserializerError::Custom(msg.to_string()) + } + + fn invalid_type(unexp: de::Unexpected, exp: &dyn de::Expected) -> Self { + DeserializerError::InvalidType(unexp.into(), exp.to_string()) + } + + fn invalid_value(unexp: de::Unexpected, exp: &dyn de::Expected) -> Self { + DeserializerError::InvalidValue(unexp.into(), exp.to_string()) + } + + fn invalid_length(len: usize, exp: &dyn de::Expected) -> Self { + DeserializerError::InvalidLength(len, exp.to_string()) + } + + fn unknown_variant(field: &str, expected: &'static [&'static str]) -> Self { + DeserializerError::UnknownVariant(field.into(), expected) + } + + fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self { + DeserializerError::UnknownField(field.into(), expected) + } + + fn missing_field(field: &'static str) -> Self { + DeserializerError::MissingField(field) + } + + fn duplicate_field(field: &'static str) -> Self { + DeserializerError::DuplicateField(field) + } +} + +impl DeserializerError { + pub fn to_error(&self) -> E { + match *self { + DeserializerError::Custom(ref msg) => E::custom(msg.clone()), + DeserializerError::InvalidType(ref unexp, ref exp) => { + E::invalid_type(unexp.to_unexpected(), &&**exp) + } + DeserializerError::InvalidValue(ref unexp, ref exp) => { + E::invalid_value(unexp.to_unexpected(), &&**exp) + } + DeserializerError::InvalidLength(len, ref exp) => E::invalid_length(len, &&**exp), + DeserializerError::UnknownVariant(ref field, exp) => E::unknown_variant(field, exp), + DeserializerError::UnknownField(ref field, exp) => E::unknown_field(field, exp), + DeserializerError::MissingField(field) => E::missing_field(field), + DeserializerError::DuplicateField(field) => E::missing_field(field), + } + } + + pub fn into_error(self) -> E { + self.to_error() + } +} + +impl Error for DeserializerError { + fn description(&self) -> &str { + "Value deserializer error" + } +} + +impl fmt::Display for DeserializerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + DeserializerError::Custom(ref msg) => write!(f, "{}", msg), + DeserializerError::InvalidType(ref unexp, ref exp) => { + write!(f, "Invalid type {}. Expected {}", unexp.to_unexpected(), exp) + } + DeserializerError::InvalidValue(ref unexp, ref exp) => { + write!(f, "Invalid value {}. Expected {}", unexp.to_unexpected(), exp) + } + DeserializerError::InvalidLength(len, ref exp) => { + write!(f, "Invalid length {}. Expected {}", len, exp) + } + DeserializerError::UnknownVariant(ref field, exp) => { + write!(f, "Unknown variant {}. Expected one of {}", field, exp.join(", ")) + }, + DeserializerError::UnknownField(ref field, exp) => { + write!(f, "Unknown field {}. Expected one of {}", field, exp.join(", ")) + } + DeserializerError::MissingField(field) => write!(f, "Missing field {}", field), + DeserializerError::DuplicateField(field) => write!(f, "Duplicate field {}", field), + } + } +} + +impl From for DeserializerError { + fn from(e: de::value::Error) -> DeserializerError { + DeserializerError::Custom(e.to_string()) + } +} + +pub struct ValueVisitor; + +impl<'de> de::Visitor<'de> for ValueVisitor { + type Value = Value; + + fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.write_str("any value") + } + + fn visit_bool(self, value: bool) -> Result { + Ok(Value::Bool(value)) + } + + fn visit_i8(self, value: i8) -> Result { + Ok(Value::I8(value)) + } + + fn visit_i16(self, value: i16) -> Result { + Ok(Value::I16(value)) + } + + fn visit_i32(self, value: i32) -> Result { + Ok(Value::I32(value)) + } + + fn visit_i64(self, value: i64) -> Result { + Ok(Value::I64(value)) + } + + fn visit_u8(self, value: u8) -> Result { + Ok(Value::U8(value)) + } + + fn visit_u16(self, value: u16) -> Result { + Ok(Value::U16(value)) + } + + fn visit_u32(self, value: u32) -> Result { + Ok(Value::U32(value)) + } + + fn visit_u64(self, value: u64) -> Result { + Ok(Value::U64(value)) + } + + fn visit_f32(self, value: f32) -> Result { + Ok(Value::F32(value)) + } + + fn visit_f64(self, value: f64) -> Result { + Ok(Value::F64(value)) + } + + fn visit_char(self, value: char) -> Result { + Ok(Value::Char(value)) + } + + fn visit_str(self, value: &str) -> Result { + Ok(Value::String(value.into())) + } + + fn visit_string(self, value: String) -> Result { + Ok(Value::String(value)) + } + + fn visit_unit(self) -> Result { + Ok(Value::Unit) + } + + fn visit_none(self) -> Result { + Ok(Value::Option(None)) + } + + fn visit_some>(self, d: D) -> Result { + d.deserialize_any(ValueVisitor).map(|v| Value::Option(Some(Box::new(v)))) + } + + fn visit_newtype_struct>(self, d: D) -> Result { + d.deserialize_any(ValueVisitor).map(|v| Value::Newtype(Box::new(v))) + } + + fn visit_seq>(self, mut visitor: V) -> Result { + let mut values = Vec::new(); + while let Some(elem) = visitor.next_element()? { + values.push(elem); + } + Ok(Value::Seq(values)) + } + + fn visit_map>(self, mut visitor: V) -> Result { + let mut values = BTreeMap::new(); + while let Some((key, value)) = visitor.next_entry()? { + values.insert(key, value); + } + Ok(Value::Map(values)) + } + + fn visit_bytes(self, v: &[u8]) -> Result { + Ok(Value::Bytes(v.into())) + } + + fn visit_byte_buf(self, v: Vec) -> Result { + Ok(Value::Bytes(v)) + } +} + +impl<'de> de::Deserialize<'de> for Value { + fn deserialize>(d: D) -> Result { + d.deserialize_any(ValueVisitor) + } +} + +impl<'de> de::IntoDeserializer<'de, DeserializerError> for Value { + type Deserializer = Value; + + fn into_deserializer(self) -> Value { + self + } +} + +pub struct ValueDeserializer { + value: Value, + error: PhantomData E>, +} + +impl ValueDeserializer { + pub fn new(value: Value) -> Self { + ValueDeserializer { + value: value, + error: Default::default(), + } + } + + pub fn into_value(self) -> Value { + self.value + } +} + +impl<'de, E> de::Deserializer<'de> for ValueDeserializer where E: de::Error { + type Error = E; + + fn deserialize_any>(self, visitor: V) -> Result { + match self.value { + Value::Bool(v) => visitor.visit_bool(v), + Value::U8(v) => visitor.visit_u8(v), + Value::U16(v) => visitor.visit_u16(v), + Value::U32(v) => visitor.visit_u32(v), + Value::U64(v) => visitor.visit_u64(v), + Value::I8(v) => visitor.visit_i8(v), + Value::I16(v) => visitor.visit_i16(v), + Value::I32(v) => visitor.visit_i32(v), + Value::I64(v) => visitor.visit_i64(v), + Value::F32(v) => visitor.visit_f32(v), + Value::F64(v) => visitor.visit_f64(v), + Value::Char(v) => visitor.visit_char(v), + Value::String(v) => visitor.visit_string(v), + Value::Unit => visitor.visit_unit(), + Value::Option(None) => visitor.visit_none(), + Value::Option(Some(v)) => visitor.visit_some(ValueDeserializer::new(*v)), + Value::Newtype(v) => visitor.visit_newtype_struct(ValueDeserializer::new(*v)), + Value::Seq(v) => { + visitor.visit_seq(de::value::SeqDeserializer::new(v.into_iter().map(ValueDeserializer::new))) + }, + Value::Map(v) => { + visitor.visit_map(de::value::MapDeserializer::new(v.into_iter().map(|(k, v)| ( + ValueDeserializer::new(k), + ValueDeserializer::new(v), + )))) + }, + Value::Bytes(v) => visitor.visit_byte_buf(v), + } + } + + fn deserialize_option>(self, visitor: V) -> Result { + match self.value { + Value::Option(..) => self.deserialize_any(visitor), + Value::Unit => visitor.visit_unit(), + _ => visitor.visit_some(self) + } + } + + fn deserialize_enum>(self, + _name: &'static str, + _variants: &'static [&'static str], + visitor: V) + -> Result { + let (variant, value) = match self.value { + Value::Map(value) => { + let mut iter = value.into_iter(); + let (variant, value) = match iter.next() { + Some(v) => v, + None => { + return Err(de::Error::invalid_value(de::Unexpected::Map, + &"map with a single key")); + } + }; + // enums are encoded as maps with a single key:value pair + if iter.next().is_some() { + return Err(de::Error::invalid_value(de::Unexpected::Map, + &"map with a single key")); + } + (variant, Some(value)) + } + Value::String(variant) => (Value::String(variant), None), + other => { + return Err(de::Error::invalid_type(other.unexpected(), &"string or map")); + } + }; + + let d = EnumDeserializer { + variant: variant, + value: value, + error: Default::default(), + }; + visitor.visit_enum(d) + } + + fn deserialize_newtype_struct>(self, + _name: &'static str, + visitor: V) + -> Result { + match self.value { + Value::Newtype(v) => visitor.visit_newtype_struct(ValueDeserializer::new(*v)), + _ => visitor.visit_newtype_struct(self), + } + } + + forward_to_deserialize_any! { + bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit + seq bytes byte_buf map unit_struct + tuple_struct struct tuple ignored_any identifier + } +} + +impl<'de, E> de::IntoDeserializer<'de, E> for ValueDeserializer where E: de::Error { + type Deserializer = Self; + + fn into_deserializer(self) -> Self::Deserializer { + self + } +} + +impl<'de> de::Deserializer<'de> for Value { + type Error = DeserializerError; + + fn deserialize_any>(self, visitor: V) -> Result { + ValueDeserializer::new(self).deserialize_any(visitor) + } + + fn deserialize_option>(self, visitor: V) -> Result { + ValueDeserializer::new(self).deserialize_option(visitor) + } + + fn deserialize_enum>(self, + name: &'static str, + variants: &'static [&'static str], + visitor: V) + -> Result { + ValueDeserializer::new(self).deserialize_enum(name, variants, visitor) + } + + fn deserialize_newtype_struct>(self, + name: &'static str, + visitor: V) + -> Result { + ValueDeserializer::new(self).deserialize_newtype_struct(name, visitor) + } + + forward_to_deserialize_any! { + bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit + seq bytes byte_buf map unit_struct + tuple_struct struct tuple ignored_any identifier + } +} + +struct EnumDeserializer { + variant: Value, + value: Option, + error: PhantomData E>, +} + +impl<'de, E> de::EnumAccess<'de> for EnumDeserializer where E: de::Error { + type Error = E; + type Variant = VariantDeserializer; + + fn variant_seed(self, seed: V) -> Result<(V::Value, VariantDeserializer), Self::Error> + where V: de::DeserializeSeed<'de> + { + let visitor = VariantDeserializer { + value: self.value, + error: Default::default(), + }; + seed.deserialize(ValueDeserializer::new(self.variant)).map(|v| (v, visitor)) + } +} + +struct VariantDeserializer { + value: Option, + error: PhantomData E>, +} + +impl<'de, E> de::VariantAccess<'de> for VariantDeserializer where E: de::Error { + type Error = E; + + fn unit_variant(self) -> Result<(), Self::Error> { + match self.value { + Some(value) => de::Deserialize::deserialize(ValueDeserializer::new(value)), + None => Ok(()), + } + } + + fn newtype_variant_seed(self, seed: T) -> Result + where T: de::DeserializeSeed<'de> + { + match self.value { + Some(value) => seed.deserialize(ValueDeserializer::new(value)), + None => Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"newtype variant")), + } + } + + fn tuple_variant(self, _len: usize, visitor: V) -> Result + where V: de::Visitor<'de> + { + match self.value { + Some(Value::Seq(v)) => { + de::Deserializer::deserialize_any( + de::value::SeqDeserializer::new(v.into_iter().map(ValueDeserializer::new)), + visitor) + } + Some(other) => Err(de::Error::invalid_type(other.unexpected(), &"tuple variant")), + None => Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"tuple variant")), + } + } + + fn struct_variant(self, + _fields: &'static [&'static str], + visitor: V) + -> Result + where V: de::Visitor<'de> + { + match self.value { + Some(Value::Map(v)) => { + de::Deserializer::deserialize_any( + de::value::MapDeserializer::new(v.into_iter().map(|(k, v)| ( + ValueDeserializer::new(k), + ValueDeserializer::new(v), + ))), + visitor) + } + Some(other) => Err(de::Error::invalid_type(other.unexpected(), &"struct variant")), + None => Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"struct variant")), + } + } +} diff --git a/copper_value/src/lib.rs b/copper_value/src/lib.rs new file mode 100644 index 000000000..30fb2ffc0 --- /dev/null +++ b/copper_value/src/lib.rs @@ -0,0 +1,447 @@ +#![doc(html_root_url = "https://docs.rs/serde-value/0.7.0/")] + +use ordered_float::OrderedFloat; +use serde::Deserialize; +use std::cmp::Ordering; +use std::collections::BTreeMap; +use std::hash::{Hash, Hasher}; + +pub use de::*; +pub use ser::*; + +mod de; +mod ser; + +#[derive(Clone, Debug)] +pub enum Value { + Bool(bool), + + U8(u8), + U16(u16), + U32(u32), + U64(u64), + + I8(i8), + I16(i16), + I32(i32), + I64(i64), + + F32(f32), + F64(f64), + + Char(char), + String(String), + // InternedString(u32), + Unit, + Option(Option>), + Newtype(Box), + Seq(Vec), + Map(BTreeMap), + Bytes(Vec), +} + +impl Hash for Value { + fn hash(&self, hasher: &mut H) + where + H: Hasher, + { + self.discriminant().hash(hasher); + match *self { + Value::Bool(v) => v.hash(hasher), + Value::U8(v) => v.hash(hasher), + Value::U16(v) => v.hash(hasher), + Value::U32(v) => v.hash(hasher), + Value::U64(v) => v.hash(hasher), + Value::I8(v) => v.hash(hasher), + Value::I16(v) => v.hash(hasher), + Value::I32(v) => v.hash(hasher), + Value::I64(v) => v.hash(hasher), + Value::F32(v) => OrderedFloat(v).hash(hasher), + Value::F64(v) => OrderedFloat(v).hash(hasher), + Value::Char(v) => v.hash(hasher), + Value::String(ref v) => v.hash(hasher), + Value::Unit => ().hash(hasher), + Value::Option(ref v) => v.hash(hasher), + Value::Newtype(ref v) => v.hash(hasher), + Value::Seq(ref v) => v.hash(hasher), + Value::Map(ref v) => v.hash(hasher), + Value::Bytes(ref v) => v.hash(hasher), + } + } +} + +impl PartialEq for Value { + fn eq(&self, rhs: &Self) -> bool { + match (self, rhs) { + (&Value::Bool(v0), &Value::Bool(v1)) if v0 == v1 => true, + (&Value::U8(v0), &Value::U8(v1)) if v0 == v1 => true, + (&Value::U16(v0), &Value::U16(v1)) if v0 == v1 => true, + (&Value::U32(v0), &Value::U32(v1)) if v0 == v1 => true, + (&Value::U64(v0), &Value::U64(v1)) if v0 == v1 => true, + (&Value::I8(v0), &Value::I8(v1)) if v0 == v1 => true, + (&Value::I16(v0), &Value::I16(v1)) if v0 == v1 => true, + (&Value::I32(v0), &Value::I32(v1)) if v0 == v1 => true, + (&Value::I64(v0), &Value::I64(v1)) if v0 == v1 => true, + (&Value::F32(v0), &Value::F32(v1)) if OrderedFloat(v0) == OrderedFloat(v1) => true, + (&Value::F64(v0), &Value::F64(v1)) if OrderedFloat(v0) == OrderedFloat(v1) => true, + (&Value::Char(v0), &Value::Char(v1)) if v0 == v1 => true, + (&Value::String(ref v0), &Value::String(ref v1)) if v0 == v1 => true, + (&Value::Unit, &Value::Unit) => true, + (&Value::Option(ref v0), &Value::Option(ref v1)) if v0 == v1 => true, + (&Value::Newtype(ref v0), &Value::Newtype(ref v1)) if v0 == v1 => true, + (&Value::Seq(ref v0), &Value::Seq(ref v1)) if v0 == v1 => true, + (&Value::Map(ref v0), &Value::Map(ref v1)) if v0 == v1 => true, + (&Value::Bytes(ref v0), &Value::Bytes(ref v1)) if v0 == v1 => true, + _ => false, + } + } +} + +impl Ord for Value { + fn cmp(&self, rhs: &Self) -> Ordering { + match (self, rhs) { + (&Value::Bool(v0), &Value::Bool(ref v1)) => v0.cmp(v1), + (&Value::U8(v0), &Value::U8(ref v1)) => v0.cmp(v1), + (&Value::U16(v0), &Value::U16(ref v1)) => v0.cmp(v1), + (&Value::U32(v0), &Value::U32(ref v1)) => v0.cmp(v1), + (&Value::U64(v0), &Value::U64(ref v1)) => v0.cmp(v1), + (&Value::I8(v0), &Value::I8(ref v1)) => v0.cmp(v1), + (&Value::I16(v0), &Value::I16(ref v1)) => v0.cmp(v1), + (&Value::I32(v0), &Value::I32(ref v1)) => v0.cmp(v1), + (&Value::I64(v0), &Value::I64(ref v1)) => v0.cmp(v1), + (&Value::F32(v0), &Value::F32(v1)) => OrderedFloat(v0).cmp(&OrderedFloat(v1)), + (&Value::F64(v0), &Value::F64(v1)) => OrderedFloat(v0).cmp(&OrderedFloat(v1)), + (&Value::Char(v0), &Value::Char(ref v1)) => v0.cmp(v1), + (&Value::String(ref v0), &Value::String(ref v1)) => v0.cmp(v1), + (&Value::Unit, &Value::Unit) => Ordering::Equal, + (&Value::Option(ref v0), &Value::Option(ref v1)) => v0.cmp(v1), + (&Value::Newtype(ref v0), &Value::Newtype(ref v1)) => v0.cmp(v1), + (&Value::Seq(ref v0), &Value::Seq(ref v1)) => v0.cmp(v1), + (&Value::Map(ref v0), &Value::Map(ref v1)) => v0.cmp(v1), + (&Value::Bytes(ref v0), &Value::Bytes(ref v1)) => v0.cmp(v1), + (ref v0, ref v1) => v0.discriminant().cmp(&v1.discriminant()), + } + } +} + +impl Value { + fn discriminant(&self) -> usize { + match *self { + Value::Bool(..) => 0, + Value::U8(..) => 1, + Value::U16(..) => 2, + Value::U32(..) => 3, + Value::U64(..) => 4, + Value::I8(..) => 5, + Value::I16(..) => 6, + Value::I32(..) => 7, + Value::I64(..) => 8, + Value::F32(..) => 9, + Value::F64(..) => 10, + Value::Char(..) => 11, + Value::String(..) => 12, + Value::Unit => 13, + Value::Option(..) => 14, + Value::Newtype(..) => 15, + Value::Seq(..) => 16, + Value::Map(..) => 17, + Value::Bytes(..) => 18, + } + } + + fn unexpected(&self) -> serde::de::Unexpected { + match *self { + Value::Bool(b) => serde::de::Unexpected::Bool(b), + Value::U8(n) => serde::de::Unexpected::Unsigned(n as u64), + Value::U16(n) => serde::de::Unexpected::Unsigned(n as u64), + Value::U32(n) => serde::de::Unexpected::Unsigned(n as u64), + Value::U64(n) => serde::de::Unexpected::Unsigned(n), + Value::I8(n) => serde::de::Unexpected::Signed(n as i64), + Value::I16(n) => serde::de::Unexpected::Signed(n as i64), + Value::I32(n) => serde::de::Unexpected::Signed(n as i64), + Value::I64(n) => serde::de::Unexpected::Signed(n), + Value::F32(n) => serde::de::Unexpected::Float(n as f64), + Value::F64(n) => serde::de::Unexpected::Float(n), + Value::Char(c) => serde::de::Unexpected::Char(c), + Value::String(ref s) => serde::de::Unexpected::Str(s), + Value::Unit => serde::de::Unexpected::Unit, + Value::Option(_) => serde::de::Unexpected::Option, + Value::Newtype(_) => serde::de::Unexpected::NewtypeStruct, + Value::Seq(_) => serde::de::Unexpected::Seq, + Value::Map(_) => serde::de::Unexpected::Map, + Value::Bytes(ref b) => serde::de::Unexpected::Bytes(b), + } + } + + pub fn deserialize_into<'de, T: Deserialize<'de>>(self) -> Result { + T::deserialize(self) + } + + //pub fn intern_strings(&self, keygen: fn(&str) -> u32) -> Self { + // match self { + // Value::Map(map) => { + // let new_map: BTreeMap = map + // .iter() + // .map(|(key, value)| { + // let new_key = match key { + // Value::String(orig_key) => Value::InternedString(keygen(orig_key)), + // _ => key.clone(), + // }; + // (new_key, value.intern_strings(keygen)) + // }) + // .collect(); + // Value::Map(new_map) + // } + // Value::Seq(seq) => { + // let new_seq: Vec = seq.iter().map(|v| v.intern_strings(keygen)).collect(); + // Value::Seq(new_seq) + // } + // _ => self.clone(), + // } + //} +} + +impl Eq for Value {} +impl PartialOrd for Value { + fn partial_cmp(&self, rhs: &Self) -> Option { + Some(self.cmp(rhs)) + } +} + +#[cfg(test)] +use serde_derive::{Deserialize, Serialize}; + +#[test] +fn de_smoke_test() { + // some convoluted Value + let value = Value::Option(Some(Box::new(Value::Seq(vec![ + Value::U16(8), + Value::Char('a'), + Value::F32(1.0), + Value::String("hello".into()), + Value::Map( + vec![ + (Value::Bool(false), Value::Unit), + ( + Value::Bool(true), + Value::Newtype(Box::new(Value::Bytes(b"hi".as_ref().into()))), + ), + ] + .into_iter() + .collect(), + ), + ])))); + + // assert that the value remains unchanged through deserialization + let value_de = Value::deserialize(value.clone()).unwrap(); + assert_eq!(value_de, value); +} + +#[test] +fn ser_smoke_test() { + #[derive(Serialize)] + struct Foo { + a: u32, + b: String, + c: Vec, + } + + let foo = Foo { + a: 15, + b: "hello".into(), + c: vec![true, false], + }; + + let expected = Value::Map( + vec![ + (Value::String("a".into()), Value::U32(15)), + (Value::String("b".into()), Value::String("hello".into())), + ( + Value::String("c".into()), + Value::Seq(vec![Value::Bool(true), Value::Bool(false)]), + ), + ] + .into_iter() + .collect(), + ); + + let value = to_value(&foo).unwrap(); + assert_eq!(expected, value); +} + +#[test] +fn deserialize_into_enum() { + #[derive(Deserialize, Debug, PartialEq, Eq)] + enum Foo { + Bar, + Baz(u8), + } + + let value = Value::String("Bar".into()); + assert_eq!(Foo::deserialize(value).unwrap(), Foo::Bar); + + let value = Value::Map( + vec![(Value::String("Baz".into()), Value::U8(1))] + .into_iter() + .collect(), + ); + assert_eq!(Foo::deserialize(value).unwrap(), Foo::Baz(1)); +} + +#[test] +fn serialize_from_enum() { + #[derive(Serialize)] + enum Foo { + Bar, + Baz(u8), + Qux { quux: u8 }, + Corge(u8, u8), + } + + let bar = Foo::Bar; + assert_eq!(to_value(&bar).unwrap(), Value::String("Bar".into())); + + let baz = Foo::Baz(1); + assert_eq!( + to_value(&baz).unwrap(), + Value::Map( + vec![(Value::String("Baz".into()), Value::U8(1))] + .into_iter() + .collect(), + ) + ); + + let qux = Foo::Qux { quux: 2 }; + assert_eq!( + to_value(&qux).unwrap(), + Value::Map( + vec![( + Value::String("Qux".into()), + Value::Map( + vec![(Value::String("quux".into()), Value::U8(2))] + .into_iter() + .collect() + ) + )] + .into_iter() + .collect() + ) + ); + + let corge = Foo::Corge(3, 4); + assert_eq!( + to_value(&corge).unwrap(), + Value::Map( + vec![( + Value::String("Corge".into()), + Value::Seq(vec![Value::U8(3), Value::U8(4)]) + )] + .into_iter() + .collect() + ) + ); +} + +#[test] +fn deserialize_inside_deserialize_impl() { + #[derive(Debug, PartialEq, Eq)] + enum Event { + Added(u32), + Error(u8), + } + + impl<'de> serde::Deserialize<'de> for Event { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + #[derive(Deserialize)] + struct RawEvent { + kind: String, + object: Value, + } + + let raw_event = RawEvent::deserialize(deserializer)?; + + // Cannot directly use Value as Deserializer, since error type needs to be + // generic D::Error rather than specific serde_value::DeserializerError + let object_deserializer = ValueDeserializer::new(raw_event.object); + + Ok(match &*raw_event.kind { + "ADDED" => Event::Added(<_>::deserialize(object_deserializer)?), + "ERROR" => Event::Error(<_>::deserialize(object_deserializer)?), + kind => return Err(serde::de::Error::unknown_variant(kind, &["ADDED", "ERROR"])), + }) + } + } + + let input = Value::Map( + vec![ + ( + Value::String("kind".to_owned()), + Value::String("ADDED".to_owned()), + ), + (Value::String("object".to_owned()), Value::U32(5)), + ] + .into_iter() + .collect(), + ); + let event = Event::deserialize(input).expect("could not deserialize ADDED event"); + assert_eq!(event, Event::Added(5)); + + let input = Value::Map( + vec![ + ( + Value::String("kind".to_owned()), + Value::String("ERROR".to_owned()), + ), + (Value::String("object".to_owned()), Value::U8(5)), + ] + .into_iter() + .collect(), + ); + let event = Event::deserialize(input).expect("could not deserialize ERROR event"); + assert_eq!(event, Event::Error(5)); + + let input = Value::Map( + vec![ + ( + Value::String("kind".to_owned()), + Value::String("ADDED".to_owned()), + ), + (Value::String("object".to_owned()), Value::Unit), + ] + .into_iter() + .collect(), + ); + let _ = Event::deserialize(input).expect_err("expected deserializing bad ADDED event to fail"); +} + +#[test] +fn deserialize_newtype() { + #[derive(Debug, Deserialize, PartialEq)] + struct Foo(i32); + + let input = Value::I32(5); + let foo = Foo::deserialize(input).unwrap(); + assert_eq!(foo, Foo(5)); +} + +#[test] +fn deserialize_newtype2() { + #[derive(Debug, Deserialize, PartialEq)] + struct Foo(i32); + + #[derive(Debug, Deserialize, PartialEq)] + struct Bar { + foo: Foo, + } + + let input = Value::Map( + vec![(Value::String("foo".to_owned()), Value::I32(5))] + .into_iter() + .collect(), + ); + let bar = Bar::deserialize(input).unwrap(); + assert_eq!(bar, Bar { foo: Foo(5) }); +} diff --git a/copper_value/src/ser.rs b/copper_value/src/ser.rs new file mode 100644 index 000000000..896175625 --- /dev/null +++ b/copper_value/src/ser.rs @@ -0,0 +1,416 @@ +use serde::ser; +use std::collections::BTreeMap; +use std::error::Error; +use std::fmt; + +use crate::Value; + +#[derive(Debug)] +pub enum SerializerError { + Custom(String), +} + +impl fmt::Display for SerializerError { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + match *self { + SerializerError::Custom(ref s) => fmt.write_str(s), + } + } +} + +impl Error for SerializerError { + fn description(&self) -> &str { + "Value serializer error" + } +} + +impl ser::Error for SerializerError { + fn custom(msg: T) -> SerializerError { + SerializerError::Custom(msg.to_string()) + } +} + +impl ser::Serialize for Value { + fn serialize(&self, s: S) -> Result { + match *self { + Value::Bool(v) => s.serialize_bool(v), + Value::U8(v) => s.serialize_u8(v), + Value::U16(v) => s.serialize_u16(v), + Value::U32(v) => s.serialize_u32(v), + Value::U64(v) => s.serialize_u64(v), + Value::I8(v) => s.serialize_i8(v), + Value::I16(v) => s.serialize_i16(v), + Value::I32(v) => s.serialize_i32(v), + Value::I64(v) => s.serialize_i64(v), + Value::F32(v) => s.serialize_f32(v), + Value::F64(v) => s.serialize_f64(v), + Value::Char(v) => s.serialize_char(v), + Value::String(ref v) => s.serialize_str(v), + // Value::InternedString(v) => s.serialize_u32(v), + Value::Unit => s.serialize_unit(), + Value::Option(None) => s.serialize_none(), + Value::Option(Some(ref v)) => s.serialize_some(v), + Value::Newtype(ref v) => s.serialize_newtype_struct("", v), + Value::Seq(ref v) => v.serialize(s), + Value::Map(ref v) => v.serialize(s), + Value::Bytes(ref v) => s.serialize_bytes(v), + } + } +} + +pub fn to_value(value: T) -> Result { + value.serialize(Serializer) +} + +struct Serializer; + +impl ser::Serializer for Serializer { + type Ok = Value; + type Error = SerializerError; + type SerializeSeq = SerializeSeq; + type SerializeTuple = SerializeTuple; + type SerializeTupleStruct = SerializeTupleStruct; + type SerializeTupleVariant = SerializeTupleVariant; + type SerializeMap = SerializeMap; + type SerializeStruct = SerializeStruct; + type SerializeStructVariant = SerializeStructVariant; + + fn serialize_bool(self, v: bool) -> Result { + Ok(Value::Bool(v)) + } + + fn serialize_i8(self, v: i8) -> Result { + Ok(Value::I8(v)) + } + + fn serialize_i16(self, v: i16) -> Result { + Ok(Value::I16(v)) + } + + fn serialize_i32(self, v: i32) -> Result { + Ok(Value::I32(v)) + } + + fn serialize_i64(self, v: i64) -> Result { + Ok(Value::I64(v)) + } + + fn serialize_u8(self, v: u8) -> Result { + Ok(Value::U8(v)) + } + + fn serialize_u16(self, v: u16) -> Result { + Ok(Value::U16(v)) + } + + fn serialize_u32(self, v: u32) -> Result { + Ok(Value::U32(v)) + } + + fn serialize_u64(self, v: u64) -> Result { + Ok(Value::U64(v)) + } + + fn serialize_f32(self, v: f32) -> Result { + Ok(Value::F32(v)) + } + + fn serialize_f64(self, v: f64) -> Result { + Ok(Value::F64(v)) + } + + fn serialize_char(self, v: char) -> Result { + Ok(Value::Char(v)) + } + + fn serialize_str(self, v: &str) -> Result { + Ok(Value::String(v.to_string())) + } + + fn serialize_bytes(self, v: &[u8]) -> Result { + Ok(Value::Bytes(v.to_vec())) + } + + fn serialize_none(self) -> Result { + Ok(Value::Option(None)) + } + + fn serialize_some(self, value: &T) -> Result + where + T: ser::Serialize, + { + value + .serialize(Serializer) + .map(|v| Value::Option(Some(Box::new(v)))) + } + + fn serialize_unit(self) -> Result { + Ok(Value::Unit) + } + + fn serialize_unit_struct(self, _name: &'static str) -> Result { + Ok(Value::Unit) + } + + fn serialize_unit_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + ) -> Result { + Ok(Value::String(variant.to_string())) + } + + fn serialize_newtype_struct( + self, + _name: &'static str, + value: &T, + ) -> Result + where + T: ser::Serialize, + { + value + .serialize(Serializer) + .map(|v| Value::Newtype(Box::new(v))) + } + + fn serialize_newtype_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + value: &T, + ) -> Result + where + T: ser::Serialize, + { + value.serialize(Serializer).map(|v| { + let mut map = BTreeMap::new(); + map.insert(Value::String(variant.to_string()), v); + Value::Map(map) + }) + } + + fn serialize_seq(self, _len: Option) -> Result { + Ok(SerializeSeq(vec![])) + } + + fn serialize_tuple(self, _len: usize) -> Result { + Ok(SerializeTuple(vec![])) + } + + fn serialize_tuple_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result { + Ok(SerializeTupleStruct(vec![])) + } + + fn serialize_tuple_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + len: usize, + ) -> Result { + Ok(SerializeTupleVariant( + Value::String(variant.to_string()), + Vec::with_capacity(len), + )) + } + + fn serialize_map(self, _len: Option) -> Result { + Ok(SerializeMap { + map: BTreeMap::new(), + key: None, + }) + } + + fn serialize_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result { + Ok(SerializeStruct(BTreeMap::new())) + } + + fn serialize_struct_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + _len: usize, + ) -> Result { + Ok(SerializeStructVariant( + Value::String(variant.to_string()), + BTreeMap::new(), + )) + } +} + +struct SerializeSeq(Vec); + +impl ser::SerializeSeq for SerializeSeq { + type Ok = Value; + type Error = SerializerError; + + fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> + where + T: ser::Serialize, + { + let value = value.serialize(Serializer)?; + self.0.push(value); + Ok(()) + } + + fn end(self) -> Result { + Ok(Value::Seq(self.0)) + } +} + +struct SerializeTuple(Vec); + +impl ser::SerializeTuple for SerializeTuple { + type Ok = Value; + type Error = SerializerError; + + fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> + where + T: ser::Serialize, + { + let value = value.serialize(Serializer)?; + self.0.push(value); + Ok(()) + } + + fn end(self) -> Result { + Ok(Value::Seq(self.0)) + } +} + +struct SerializeTupleStruct(Vec); + +impl ser::SerializeTupleStruct for SerializeTupleStruct { + type Ok = Value; + type Error = SerializerError; + + fn serialize_field(&mut self, value: &T) -> Result<(), Self::Error> + where + T: ser::Serialize, + { + let value = value.serialize(Serializer)?; + self.0.push(value); + Ok(()) + } + + fn end(self) -> Result { + Ok(Value::Seq(self.0)) + } +} + +struct SerializeTupleVariant(Value, Vec); + +impl ser::SerializeTupleVariant for SerializeTupleVariant { + type Ok = Value; + type Error = SerializerError; + + fn serialize_field(&mut self, value: &T) -> Result<(), Self::Error> + where + T: ser::Serialize, + { + let value = value.serialize(Serializer)?; + self.1.push(value); + Ok(()) + } + + fn end(self) -> Result { + let mut map = BTreeMap::new(); + map.insert(self.0, Value::Seq(self.1)); + Ok(Value::Map(map)) + } +} + +struct SerializeMap { + map: BTreeMap, + key: Option, +} + +impl ser::SerializeMap for SerializeMap { + type Ok = Value; + type Error = SerializerError; + + fn serialize_key(&mut self, key: &T) -> Result<(), Self::Error> + where + T: ser::Serialize, + { + let key = key.serialize(Serializer)?; + self.key = Some(key); + Ok(()) + } + + fn serialize_value(&mut self, value: &T) -> Result<(), Self::Error> + where + T: ser::Serialize, + { + let value = value.serialize(Serializer)?; + self.map.insert(self.key.take().unwrap(), value); + Ok(()) + } + + fn end(self) -> Result { + Ok(Value::Map(self.map)) + } +} + +struct SerializeStruct(BTreeMap); + +impl ser::SerializeStruct for SerializeStruct { + type Ok = Value; + type Error = SerializerError; + + fn serialize_field( + &mut self, + key: &'static str, + value: &T, + ) -> Result<(), Self::Error> + where + T: ser::Serialize, + { + let key = Value::String(key.to_string()); + let value = value.serialize(Serializer)?; + self.0.insert(key, value); + Ok(()) + } + + fn end(self) -> Result { + Ok(Value::Map(self.0)) + } +} + +struct SerializeStructVariant(Value, BTreeMap); + +impl ser::SerializeStructVariant for SerializeStructVariant { + type Ok = Value; + type Error = SerializerError; + + fn serialize_field( + &mut self, + key: &'static str, + value: &T, + ) -> Result<(), Self::Error> + where + T: ser::Serialize, + { + let key = Value::String(key.to_string()); + let value = value.serialize(Serializer)?; + self.1.insert(key, value); + Ok(()) + } + + fn end(self) -> Result { + let mut map = BTreeMap::new(); + map.insert(self.0, Value::Map(self.1)); + Ok(Value::Map(map)) + } +}