Expand description
Attribute types: one Rust type, its JSON Schema, and how it is read back.
resource! needs three things for every field it is
given: the JSON Schema that describes it, how to read it out of a
validated document, and how to write it back into a response. Rather than
matching on type names in the macro — which would only ever know the types
the macro was written for — each is a method on this trait.
Implementations ship for String, the integer and float types, bool,
and Option<T> over any of them. Implement it yourself for a newtype, an
enum, or anything else you want a resource to carry:
ⓘ
struct Priority(u8);
impl Attribute for Priority {
fn schema() -> serde_json::Value {
serde_json::json!({ "type": "integer", "minimum": 1, "maximum": 5 })
}
fn read(value: &serde_json::Value) -> Option<Self> {
value.as_u64().map(|n| Priority(n as u8))
}
fn write(&self) -> serde_json::Value {
serde_json::Value::from(self.0)
}
}Validation still happens against the schema before read runs, so read
is reached only for a value the schema already accepted.
Structs§
- Date
- RFC 3339 full-date, e.g.
2024-02-29. - Date
Time - RFC 3339 timestamp, e.g.
2024-02-29T12:00:00Z. - Duration
- ISO 8601 duration, e.g.
P3DT4H. - RFC 5321 mailbox.
- Formatted
- A string attribute in a declared JSON Schema
format. - Hostname
- RFC 1123 host name.
- IdnEmail
- RFC 6531 internationalized mailbox.
- Ipv4
- Dotted-quad address.
- Ipv6
- RFC 4291 address.
- Json
Pointer - RFC 6901 JSON Pointer.
- Relative
Json Pointer - Relative JSON Pointer.
- Time
- RFC 3339 full-time.
- Uuid
- RFC 4122 UUID.
Traits§
- Attribute
- A type a resource attribute can hold.
- Format
Name - The name of a JSON Schema
format, as it appears in a schema.