-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from MidasLamb/serde-support
Serde support
- Loading branch information
Showing
6 changed files
with
177 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ jobs: | |
uses: actions-rs/[email protected] | ||
with: | ||
version: "0.15.0" | ||
args: "-- --test-threads 1" | ||
args: "--all-features -- --test-threads 1" | ||
|
||
- name: Upload to codecov.io | ||
uses: codecov/[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Changelog | ||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
||
## [Unreleased] | ||
### Added | ||
|
||
### Changed | ||
|
||
### Removed | ||
|
||
## [0.2.0] | ||
### Added | ||
* `serde` support behind the `serde` feature flag. | ||
* `Eq, PartialEq, Ord, PartialOrd` are now implemented for `NonEmptyString`. | ||
* `get` to retrieve a reference to the inner value. | ||
|
||
### Changed | ||
* `new` constructor now returns a `Result` rather than an `Option`, which contains the original string | ||
|
||
### Removed | ||
|
||
[Unreleased]: https://github.com/MidasLamb/non-empty-string/v0.2.0...HEAD | ||
[0.2.0]: https://github.com/MidasLamb/non-empty-string/compare/v0.1.0...v0.2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,23 @@ | ||
[package] | ||
name = "non-empty-string" | ||
version = "0.1.0" | ||
version = "0.2.0" | ||
edition = "2018" | ||
authors = ["Midas Lambrichts <[email protected]>"] | ||
license = "MIT OR Apache-2.0" | ||
description = "A simple type for non empty Strings, similar to NonZeroUsize and friends." | ||
repository = "https://github.com/MidasLamb/non-empty-string" | ||
keywords = ["nonemptystring", "string", "str"] | ||
keywords = ["nonemptystring", "string", "str", "non-empty", "nonempty"] | ||
|
||
[lib] | ||
name = "non_empty_string" | ||
|
||
[dependencies] | ||
serde = { version = "1", optional = true } | ||
|
||
[dev-dependencies] | ||
assert_matches = "1.5.0" | ||
serde_json = { version = "1" } | ||
|
||
[features] | ||
default = [] | ||
serde = ["dep:serde"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use std::fmt; | ||
|
||
use serde::{ | ||
de::{self, Unexpected, Visitor}, | ||
ser::Serialize, | ||
}; | ||
|
||
use crate::NonEmptyString; | ||
|
||
impl Serialize for NonEmptyString { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
serializer.serialize_str(self.get()) | ||
} | ||
} | ||
|
||
struct NonEmptyStringVisitor; | ||
|
||
impl<'de> de::Deserialize<'de> for NonEmptyString { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
deserializer.deserialize_string(NonEmptyStringVisitor) | ||
} | ||
} | ||
|
||
pub enum DeserializeError {} | ||
|
||
type Result<T, E = DeserializeError> = std::result::Result<T, E>; | ||
|
||
impl<'de> Visitor<'de> for NonEmptyStringVisitor { | ||
type Value = NonEmptyString; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
formatter.write_str("an integer between -2^31 and 2^31") | ||
} | ||
|
||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> | ||
where | ||
E: de::Error, | ||
{ | ||
self.visit_string(value.to_owned()) | ||
} | ||
|
||
fn visit_string<E>(self, value: String) -> Result<Self::Value, E> | ||
where | ||
E: de::Error, | ||
{ | ||
NonEmptyString::new(value).map_err(|e| de::Error::invalid_value(Unexpected::Str(&e), &self)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::*; | ||
use assert_matches::assert_matches; | ||
use serde_json::json; | ||
|
||
#[test] | ||
fn serialize_works() { | ||
let value = NonEmptyString("abc".to_owned()); | ||
let result = serde_json::to_string(&value); | ||
|
||
assert!(result.is_ok()); | ||
|
||
let json = serde_json::to_string(&json!("abc")).unwrap(); | ||
assert_eq!(result.unwrap(), json) | ||
} | ||
|
||
#[test] | ||
fn deserialize_works() { | ||
let e: Result<NonEmptyString, _> = serde_json::from_value(json!("abc")); | ||
|
||
let expected = NonEmptyString("abc".to_owned()); | ||
|
||
assert_matches!(e, Ok(v) if v == expected) | ||
} | ||
|
||
#[test] | ||
fn deserialize_empty_fails() { | ||
let e: Result<NonEmptyString, _> = serde_json::from_value(json!("")); | ||
|
||
assert!(e.is_err()); | ||
// assert_matches!(e, Ok(expected)) | ||
} | ||
} |