From 0b775c825644dfbe7eb8ad87ee4f22c9e59bfa5e Mon Sep 17 00:00:00 2001 From: KodrAus Date: Tue, 26 Mar 2024 20:41:39 +1000 Subject: [PATCH] fix warnings and fallback to indexes as tags when labels are missing --- flatten/bench/lib.rs | 1 + json/src/to_fmt.rs | 15 ++----- json/test/lib.rs | 89 +++++++++++++++++++++++++++++++++++++++ nested/src/error.rs | 2 +- nested/src/lib.rs | 48 +++++++++++---------- serde/src/to_serialize.rs | 8 ---- src/value.rs | 2 +- 7 files changed, 121 insertions(+), 44 deletions(-) diff --git a/flatten/bench/lib.rs b/flatten/bench/lib.rs index 0e14b0e1..67af6f2a 100644 --- a/flatten/bench/lib.rs +++ b/flatten/bench/lib.rs @@ -1,3 +1,4 @@ +#![cfg(test)] #![feature(test)] extern crate test; diff --git a/json/src/to_fmt.rs b/json/src/to_fmt.rs index d557cb52..a4659bed 100644 --- a/json/src/to_fmt.rs +++ b/json/src/to_fmt.rs @@ -359,7 +359,7 @@ where &mut self, tag: Option<&sval::Tag>, label: Option<&sval::Label>, - _: Option<&sval::Index>, + index: Option<&sval::Index>, ) -> sval::Result { self.is_internally_tagged = false; self.is_current_depth_empty = false; @@ -369,6 +369,8 @@ where _ => { if let Some(label) = label { self.value(label.as_str()) + } else if let Some(index) = index.and_then(|ix| ix.to_i64()) { + self.value(&index) } else { self.null() } @@ -674,14 +676,3 @@ static ESCAPE: [u8; 256] = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // E 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // F ]; - -struct Escape(W); - -impl Write for Escape -where - W: Write, -{ - fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> { - escape_str(s, &mut self.0) - } -} diff --git a/json/test/lib.rs b/json/test/lib.rs index 3a9e0131..7aa0ec30 100644 --- a/json/test/lib.rs +++ b/json/test/lib.rs @@ -259,6 +259,7 @@ fn stream_untagged_enum() { #[derive(Value)] #[sval(dynamic)] enum Dynamic<'a> { + Constant, Null(sval::Null), Text(&'a str), Number(f64), @@ -266,6 +267,10 @@ fn stream_untagged_enum() { Array(&'a [Dynamic<'a>]), } + assert_eq!( + "\"Constant\"", + sval_json::stream_to_string(Dynamic::Constant).unwrap() + ); assert_eq!( "\"Some text\"", sval_json::stream_to_string(Dynamic::Text("Some text")).unwrap() @@ -328,6 +333,90 @@ fn stream_empty_enum() { assert_eq!("\"Enum\"", sval_json::stream_to_string(Enum).unwrap()); } +#[test] +fn stream_unlabeled_tag() { + struct Tag; + + impl sval::Value for Tag { + fn stream<'sval, S: sval::Stream<'sval> + ?Sized>( + &'sval self, + stream: &mut S, + ) -> sval::Result { + stream.tag(None, None, Some(&sval::Index::new(1))) + } + } + + assert_eq!("1", sval_json::stream_to_string(Tag).unwrap()); +} + +#[test] +fn stream_unlabeled_tag_variant() { + struct Enum; + + impl sval::Value for Enum { + fn stream<'sval, S: sval::Stream<'sval> + ?Sized>( + &'sval self, + stream: &mut S, + ) -> sval::Result { + stream.enum_begin(None, Some(&sval::Label::new("Enum")), None)?; + stream.tag(None, None, Some(&sval::Index::new(1)))?; + stream.enum_end(None, Some(&sval::Label::new("Enum")), None) + } + } + + assert_eq!("1", sval_json::stream_to_string(Enum).unwrap()); +} + +#[test] +fn stream_unlabeled_unindexed_tag() { + struct Tag; + + impl sval::Value for Tag { + fn stream<'sval, S: sval::Stream<'sval> + ?Sized>( + &'sval self, + stream: &mut S, + ) -> sval::Result { + stream.tag(None, None, None) + } + } + + assert_eq!("null", sval_json::stream_to_string(Tag).unwrap()); +} + +#[test] +fn stream_unlabeled_empty_enum() { + struct Enum; + + impl sval::Value for Enum { + fn stream<'sval, S: sval::Stream<'sval> + ?Sized>( + &'sval self, + stream: &mut S, + ) -> sval::Result { + stream.enum_begin(None, None, Some(&sval::Index::new(1)))?; + stream.enum_end(None, None, Some(&sval::Index::new(1))) + } + } + + assert_eq!("1", sval_json::stream_to_string(Enum).unwrap()); +} + +#[test] +fn stream_unlabeled_unindexed_empty_enum() { + struct Enum; + + impl sval::Value for Enum { + fn stream<'sval, S: sval::Stream<'sval> + ?Sized>( + &'sval self, + stream: &mut S, + ) -> sval::Result { + stream.enum_begin(None, None, None)?; + stream.enum_end(None, None, None) + } + } + + assert_eq!("null", sval_json::stream_to_string(Enum).unwrap()); +} + #[test] fn stream_exotic_record() { // { field_0: 42, field_1: true, field_2: "Hello" } diff --git a/nested/src/error.rs b/nested/src/error.rs index 38de81e0..38f89dde 100644 --- a/nested/src/error.rs +++ b/nested/src/error.rs @@ -22,7 +22,7 @@ enum ErrorKind { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self.0 { - ErrorKind::Buffer(_) => { + ErrorKind::Buffer(ref _e) => { write!(f, "failed to buffer a value") } ErrorKind::InvalidValue { reason } => { diff --git a/nested/src/lib.rs b/nested/src/lib.rs index a881e860..e430180d 100644 --- a/nested/src/lib.rs +++ b/nested/src/lib.rs @@ -31,10 +31,7 @@ use self::flat::FlatStream; /** Stream a value through a stream. */ -pub fn stream<'sval, S: Stream<'sval>>( - stream: S, - value: impl ValueRef<'sval>, -) -> Result { +pub fn stream<'sval, S: Stream<'sval>>(stream: S, value: impl ValueRef<'sval>) -> Result { stream.value(value) } @@ -1085,7 +1082,10 @@ pub mod default_stream { } impl<'a> sval::Value for Tag<'a> { - fn stream<'sval, S: sval::Stream<'sval> + ?Sized>(&'sval self, stream: &mut S) -> sval::Result { + fn stream<'sval, S: sval::Stream<'sval> + ?Sized>( + &'sval self, + stream: &mut S, + ) -> sval::Result { self.stream_ref(stream) } } @@ -1111,7 +1111,15 @@ pub mod default_stream { } } - stream.tagged(tag.clone(), label.clone(), index, Tag { tag: tag.as_ref(), label: label.as_ref() }) + stream.tagged( + tag.clone(), + label.clone(), + index, + Tag { + tag: tag.as_ref(), + label: label.as_ref(), + }, + ) } /** @@ -1412,12 +1420,14 @@ mod tests { } assert_eq!( - Value::Tag(Tag::new( - Some(sval::tags::RUST_OPTION_NONE), - Some(sval::Label::new("None")), - Some(sval::Index::new(0)) - ) - .unwrap()), + Value::Tag( + Tag::new( + Some(sval::tags::RUST_OPTION_NONE), + Some(sval::Label::new("None")), + Some(sval::Index::new(0)) + ) + .unwrap() + ), ToValue::default().value_ref(&None::).unwrap() ); @@ -1430,22 +1440,16 @@ mod tests { ) .unwrap(), value: Box::new(Value::Record(Record { - tag: Tag::new( - None, - Some(sval::Label::new("Inner")), - None, - ) - .unwrap(), + tag: Tag::new(None, Some(sval::Label::new("Inner")), None,).unwrap(), entries: vec![ (sval::Label::new("a"), Value::I64(42)), (sval::Label::new("b"), Value::Bool(true)), ] })) }), - ToValue::default().value_ref(&Some(Inner { - a: 42, - b: true, - })).unwrap() + ToValue::default() + .value_ref(&Some(Inner { a: 42, b: true })) + .unwrap() ); } diff --git a/serde/src/to_serialize.rs b/serde/src/to_serialize.rs index 968a32ed..e0f54d69 100644 --- a/serde/src/to_serialize.rs +++ b/serde/src/to_serialize.rs @@ -681,11 +681,3 @@ impl<'sval, S: serde::ser::SerializeTupleVariant> StreamTuple<'sval> } } } - -struct Bytes<'sval>(&'sval [u8]); - -impl<'sval> serde::Serialize for Bytes<'sval> { - fn serialize(&self, serializer: S) -> Result { - serializer.serialize_bytes(self.0) - } -} diff --git a/src/value.rs b/src/value.rs index 8b25b143..928a2634 100644 --- a/src/value.rs +++ b/src/value.rs @@ -1,4 +1,4 @@ -use crate::{std::convert::TryInto, Index, Label, Result, Stream, Tag}; +use crate::{Index, Label, Result, Stream, Tag}; /** A producer of structured data.