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
//! Newtype-struct "tokens" used to smuggle JSONX extended types through serde's
//! data model.
//!
//! serde's data model has no native concept of a datetime, IP address, or a
//! distinct machine-width `int`/`uint`, nor of any user-defined `type(value)`
//! constructor. To represent these losslessly when a value flows through serde
//! (e.g. `Serialize for Value`, the wrapper types in [`crate::wrappers`], or a
//! caller's own [`JsonxConstructor`](crate::JsonxConstructor) type), we
//! serialize them as `serialize_newtype_struct` calls whose *name* begins with
//! [`CTOR_SENTINEL`] followed by the constructor name.
//!
//! Our own [`crate::ser::Serializer`] and [`crate::de::Deserializer`] recognize
//! the sentinel (via [`strip_ctor`]) and emit/parse the proper `type(value)`
//! syntax; every other serde format simply sees a transparent newtype and
//! renders the inner value plainly. The public [`ctor!`](crate::ctor) macro
//! produces the same encoding for third-party types.
//!
//! The sentinel uses control characters so it can never collide with a real
//! Rust type name (the usual newtype-struct name) or a JSONX identifier.
/// Prefix that marks a newtype-struct name as a JSONX constructor request. The
/// bytes after it are the constructor name (`ip`, `datetime`, a user name, ...).
pub const CTOR_SENTINEL: &str = "\u{1}jx\u{1}";
pub const TOKEN_INT: &str = "\u{1}jx\u{1}int";
pub const TOKEN_UINT: &str = "\u{1}jx\u{1}uint";
pub const TOKEN_DATETIME: &str = "\u{1}jx\u{1}datetime";
pub const TOKEN_IP: &str = "\u{1}jx\u{1}ip";
pub const TOKEN_IPPORT: &str = "\u{1}jx\u{1}ipport";
/// Marks a newtype-struct name as a *dynamic-named* constructor request
/// ([`Value::Constructor`](crate::Value)). Unlike the tokens above, the
/// constructor name is not known at compile time, so it travels inside the value
/// (as a single-entry map `{name: arg}`) rather than in the token name. The
/// trailing control byte keeps it distinct from any [`CTOR_SENTINEL`]-encoded
/// name, whose suffix is always a JSONX identifier.
pub const TOKEN_CTOR: &str = "\u{1}jx\u{1}\u{1}";
/// If `name` is a sentinel-encoded constructor token, returns the bare
/// constructor name (e.g. `"ip"`); otherwise `None`.
pub
/// Returns `true` if `s` is a legal bare JSONX identifier — the form a
/// constructor name (and an unquoted object key) must take: a leading ASCII
/// letter or `_`, then ASCII alphanumerics or `_`, and never empty.
///
/// This is the exact set the deserializer's `is_ident_start`/`is_ident_continue`
/// scanner accepts, so a name passing this check renders to JSONX that parses
/// back to the same constructor.
pub