1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! # Serde Str
//!
//! [Documentation](https://docs.rs/) |
//! [Github](https://github.com/tailhook/serde-str) |
//! [crates.io](https://crates.io/crates/) |
//! [lib.rs](https://lib.rs/)
//!
//! A (de)serializer for anything that has implemented `FromStr` / `Display`
//! but does not have `Serialize`/`Deserialize`.
//!
//! # Example
//!
//! ```rust
//! # #[macro_use] extern crate serde_derive;
//! use std::net::IpAddr;
//!
//! /// A demonstration structure that holds a lonesome IP address.
//! #[derive(Serialize, Deserialize)]
//! # #[derive(PartialEq, Debug)]
//! struct WithIp {
//! 	#[serde(with = "serde_strz")]
//! 	ip: IpAddr,
//! }
//!
//! use serde_json::{
//! 	from_str,
//! 	to_string,
//! };
//! # fn main() -> serde_json::Result<()> {
//! let with_ip: WithIp = from_str(r#"{"ip": "127.0.0.1"}"#)?;
//! assert_eq!(WithIp { ip: [127, 0, 0, 1].into() }, with_ip);
//! assert_eq!(to_string(&with_ip)?, r#"{"ip":"127.0.0.1"}"#);
//!
//! let with_ip: WithIp = from_str(r#"{"ip": "::"}"#)?;
//! assert_eq!(WithIp { ip: [0u16; 8].into() }, with_ip);
//! assert_eq!(to_string(&with_ip)?, r#"{"ip":"::"}"#);
//! # Ok(())
//! # }
//! ```
#![forbid(missing_docs, missing_debug_implementations, unsafe_code)]
#![deny(unused)]
use serde::{
	de::{
		Deserialize,
		Error as DeserializeError,
	},
	Deserializer,
	Serializer,
};
use std::{
	fmt,
	str::FromStr,
};

/// Deserialize function, see [crate docs examples](https://docs.rs/serde_strz) to see how to use it
pub fn deserialize<'de, D, T: FromStr>(deserializer: D) -> Result<T, D::Error>
where
	D: Deserializer<'de>,
	<T as FromStr>::Err: fmt::Display,
{
	let s = String::deserialize(deserializer)?;
	T::from_str(&s).map_err(DeserializeError::custom)
}

/// Serialize function, see [crate docs examples](https://docs.rs/serde_strz) to see how to use it
pub fn serialize<S, T>(
	value: &T,
	serializer: S,
) -> Result<S::Ok, S::Error>
where
	S: Serializer,
	T: fmt::Display,
{
	serializer.collect_str(value)
}

pub mod emp;
pub mod opt;