Skip to content

Commit 99a576d

Browse files
authored
Merge pull request #6 from MidasLamb/better-serde-error-message
Better serde error message
2 parents 86b7001 + f33e5c2 commit 99a576d

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

CHANGELOG.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Removed
1313

14+
## [0.2.1]
15+
### Added
16+
17+
### Changed
18+
* The error message when using `serde` now indicates that the empty string could not be deserialized.
19+
* Bumped rust edition to `2021`
20+
21+
### Removed
1422
## [0.2.0]
1523
### Added
1624
* `serde` support behind the `serde` feature flag.
@@ -22,5 +30,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2230

2331
### Removed
2432

25-
[Unreleased]: https://github.com/MidasLamb/non-empty-string/v0.2.0...HEAD
33+
[Unreleased]: https://github.com/MidasLamb/non-empty-string/v0.2.1...HEAD
34+
[0.2.1]: https://github.com/MidasLamb/non-empty-string/compare/v0.2.0...v0.2.1
2635
[0.2.0]: https://github.com/MidasLamb/non-empty-string/compare/v0.1.0...v0.2.0

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "non-empty-string"
3-
version = "0.2.0"
4-
edition = "2018"
3+
version = "0.2.1"
4+
edition = "2021"
55
authors = ["Midas Lambrichts <[email protected]>"]
66
license = "MIT OR Apache-2.0"
77
description = "A simple type for non empty Strings, similar to NonZeroUsize and friends."

src/serde_support.rs

+34-10
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,11 @@ impl<'de> de::Deserialize<'de> for NonEmptyString {
2727
}
2828
}
2929

30-
pub enum DeserializeError {}
31-
32-
type Result<T, E = DeserializeError> = std::result::Result<T, E>;
33-
3430
impl<'de> Visitor<'de> for NonEmptyStringVisitor {
3531
type Value = NonEmptyString;
3632

3733
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
38-
formatter.write_str("an integer between -2^31 and 2^31")
34+
formatter.write_str("a string with a length of more than 0")
3935
}
4036

4137
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
@@ -55,13 +51,12 @@ impl<'de> Visitor<'de> for NonEmptyStringVisitor {
5551

5652
#[cfg(test)]
5753
mod tests {
58-
use super::*;
5954
use crate::*;
6055
use assert_matches::assert_matches;
6156
use serde_json::json;
6257

6358
#[test]
64-
fn serialize_works() {
59+
fn serialize_non_empty_string_and_normal_string_give_the_same_result() {
6560
let value = NonEmptyString("abc".to_owned());
6661
let result = serde_json::to_string(&value);
6762

@@ -80,11 +75,40 @@ mod tests {
8075
assert_matches!(e, Ok(v) if v == expected)
8176
}
8277

78+
fn deserialize_x_fails(value: serde_json::Value, expected_error_message: &'static str) {
79+
let e: Result<NonEmptyString, _> = serde_json::from_value(value);
80+
assert_matches!(e, Err(error) if &error.to_string() == expected_error_message)
81+
}
82+
8383
#[test]
8484
fn deserialize_empty_fails() {
85-
let e: Result<NonEmptyString, _> = serde_json::from_value(json!(""));
85+
deserialize_x_fails(
86+
json!(""),
87+
"invalid value: string \"\", expected a string with a length of more than 0",
88+
)
89+
}
90+
91+
#[test]
92+
fn deserialize_number_fails() {
93+
deserialize_x_fails(
94+
json!(8),
95+
"invalid type: integer `8`, expected a string with a length of more than 0",
96+
)
97+
}
98+
99+
#[test]
100+
fn deserialize_object_fails() {
101+
deserialize_x_fails(
102+
json!({}),
103+
"invalid type: map, expected a string with a length of more than 0",
104+
)
105+
}
86106

87-
assert!(e.is_err());
88-
// assert_matches!(e, Ok(expected))
107+
#[test]
108+
fn deserialize_sequence_fails() {
109+
deserialize_x_fails(
110+
json!([]),
111+
"invalid type: sequence, expected a string with a length of more than 0",
112+
)
89113
}
90114
}

0 commit comments

Comments
 (0)