Skip to content

Commit

Permalink
fix version requirement for serde_bytes (#3)
Browse files Browse the repository at this point in the history
also add serde tests
  • Loading branch information
polazarus authored Aug 11, 2023
1 parent 4cb996f commit 0133b88
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ serde_json = "1.0"

[dependencies]
serde = { version = "1.0.69", optional = true }
serde_bytes = { version = "0.11", optional = true }
serde_bytes = { version = "0.11.5", optional = true }
106 changes: 103 additions & 3 deletions src/bytes/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,20 @@ where

#[cfg(test)]
mod tests {
use serde::Deserialize;
use serde_test::{
assert_de_tokens, assert_de_tokens_error, assert_ser_tokens, assert_tokens, Token,
};

use crate::bytes::serde::borrow_deserialize;
use super::borrow_deserialize;
use crate::HipByt;

#[derive(Deserialize, Debug, PartialEq, Eq)]
struct MyStruct<'a> {
#[serde(borrow, deserialize_with = "borrow_deserialize")]
field: HipByt<'a>,
}

#[test]
fn test_serde() {
let ref empty = HipByt::new();
Expand Down Expand Up @@ -100,8 +107,7 @@ mod tests {
}

#[test]
fn test_serde_borrowing() {
use serde::de::Deserialize;
fn test_serde_borrow() {
use serde_json::Value;

use super::super::HipByt;
Expand All @@ -112,5 +118,99 @@ mod tests {
let h2: HipByt<'_, Local> = Deserialize::deserialize(&v).unwrap();
assert!(h1.is_borrowed());
assert!(!h2.is_borrowed());

let s: MyStruct =
serde_json::from_str(r#"{"field": "abcdefghijklmnopqrstuvwxyz"}"#).unwrap();
assert!(s.field.is_borrowed());

assert_de_tokens(
&MyStruct {
field: HipByt::from(b"a"),
},
&[
Token::Struct {
name: "MyStruct",
len: 1,
},
Token::Str("field"),
Token::Bytes(b"a"),
Token::StructEnd,
],
);

assert_de_tokens(
&MyStruct {
field: HipByt::from(b"a"),
},
&[
Token::Struct {
name: "MyStruct",
len: 1,
},
Token::Str("field"),
Token::BorrowedBytes(b"a"),
Token::StructEnd,
],
);

assert_de_tokens(
&MyStruct {
field: HipByt::from(b"a"),
},
&[
Token::Struct {
name: "MyStruct",
len: 1,
},
Token::Str("field"),
Token::Str("a"),
Token::StructEnd,
],
);

assert_de_tokens(
&MyStruct {
field: HipByt::from(b"a"),
},
&[
Token::Struct {
name: "MyStruct",
len: 1,
},
Token::Str("field"),
Token::String("a"),
Token::StructEnd,
],
);
assert_de_tokens(
&MyStruct {
field: HipByt::from(b"a"),
},
&[
Token::Struct {
name: "MyStruct",
len: 1,
},
Token::Str("field"),
Token::BorrowedStr("a"),
Token::StructEnd,
],
);
}

#[test]
fn test_serde_borrow_err() {
assert_de_tokens_error::<MyStruct>(
&[
Token::Struct {
name: "MyStruct",
len: 1,
},
Token::Str("field"),
Token::I32(0),
Token::StructEnd,
],
"invalid type: integer `0`, expected a byte array",
);
}
}
63 changes: 55 additions & 8 deletions src/string/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,18 @@ where

#[cfg(test)]
mod tests {
use serde_test::{assert_de_tokens, assert_tokens, Token};
use serde::Deserialize;
use serde_test::{assert_de_tokens, assert_de_tokens_error, assert_tokens, Token};

use super::borrow_deserialize;
use crate::HipStr;

#[derive(Deserialize, PartialEq, Eq, Debug)]
struct MyStruct<'a> {
#[serde(borrow, deserialize_with = "borrow_deserialize")]
field: HipStr<'a>,
}

#[test]
fn test_serde() {
let empty_str = HipStr::new();
Expand All @@ -113,8 +120,7 @@ mod tests {
}

#[test]
fn test_serde_borrowing() {
use serde::Deserialize;
fn test_serde_borrow() {
use serde_json::Value;

let v = Value::from("abcdefghijklmnopqrstuvwxyz");
Expand All @@ -123,13 +129,54 @@ mod tests {
assert!(h1.is_borrowed());
assert!(!h2.is_borrowed());

#[derive(Deserialize)]
struct MyStruct<'a> {
#[serde(borrow, deserialize_with = "crate::string::serde::borrow_deserialize")]
field: HipStr<'a>,
}
let s: MyStruct =
serde_json::from_str(r#"{"field": "abcdefghijklmnopqrstuvwxyz"}"#).unwrap();
assert!(s.field.is_borrowed());

assert_de_tokens(
&MyStruct {
field: HipStr::from("a"),
},
&[
Token::Struct {
name: "MyStruct",
len: 1,
},
Token::Str("field"),
Token::String("a"),
Token::StructEnd,
],
);

assert_de_tokens(
&MyStruct {
field: HipStr::from("a"),
},
&[
Token::Struct {
name: "MyStruct",
len: 1,
},
Token::String("field"),
Token::String("a"),
Token::StructEnd,
],
);
}

#[test]
fn test_serde_borrow_err() {
assert_de_tokens_error::<MyStruct>(
&[
Token::Struct {
name: "MyStruct",
len: 1,
},
Token::Str("field"),
Token::I32(0),
Token::StructEnd,
],
"invalid type: integer `0`, expected a string",
);
}
}

0 comments on commit 0133b88

Please sign in to comment.