Skip to content

Commit

Permalink
fix warnings and fallback to indexes as tags when labels are missing
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Mar 26, 2024
1 parent 7d282d3 commit 0b775c8
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 44 deletions.
1 change: 1 addition & 0 deletions flatten/bench/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(test)]
#![feature(test)]

extern crate test;
Expand Down
15 changes: 3 additions & 12 deletions json/src/to_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
}
Expand Down Expand Up @@ -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>(W);

impl<W> Write for Escape<W>
where
W: Write,
{
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
escape_str(s, &mut self.0)
}
}
89 changes: 89 additions & 0 deletions json/test/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,18 @@ fn stream_untagged_enum() {
#[derive(Value)]
#[sval(dynamic)]
enum Dynamic<'a> {
Constant,
Null(sval::Null),
Text(&'a str),
Number(f64),
Boolean(bool),
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()
Expand Down Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion nested/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 } => {
Expand Down
48 changes: 26 additions & 22 deletions nested/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S::Ok> {
pub fn stream<'sval, S: Stream<'sval>>(stream: S, value: impl ValueRef<'sval>) -> Result<S::Ok> {
stream.value(value)
}

Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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(),
},
)
}

/**
Expand Down Expand Up @@ -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::<Inner>).unwrap()
);

Expand All @@ -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()
);
}

Expand Down
8 changes: 0 additions & 8 deletions serde/src/to_serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_bytes(self.0)
}
}
2 changes: 1 addition & 1 deletion src/value.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit 0b775c8

Please sign in to comment.