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
//! Pluggable structured-text codecs over a shared value tree.
//!
//! `rskit-codec` provides a small, object-safe [`Codec`] contract for encoding
//! and decoding structured text formats (TOML, JSON, …) through one canonical value model —
//! [`serde_json::Value`]. Any crate that reads or writes a config file, manifest,
//! or document reuses these codecs instead of re-implementing "bounded read → parse → typed error" per format.
//!
//! # Value model
//!
//! [`serde_json::Value`] is the canonical in-memory tree. It is format-neutral
//! and the de-facto Rust interchange type, so TOML and JSON both decode into it
//! and the [`value`] merge operates on it. One consequence:
//! types without a JSON equivalent (notably TOML datetimes) are not part of the model —
//! represent such values as strings.
//!
//! # Object safety
//!
//! [`Codec`] is object-safe (`Arc<dyn Codec>`)
//! so a codec can be selected at runtime (e.g. by file extension via [`select`]). The generic,
//! type-driven conveniences [`encode`] and [`decode`] are free functions taking `&dyn Codec`,
//! keeping the trait object-safe while still supporting `#[derive(Serialize, Deserialize)]` types.
//! `decode::<T>` honors `#[serde(deny_unknown_fields)]`.
//!
//! # Quick start
//!
//! ```
//! use rskit_codec::{JsonCodec, decode, encode};
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Serialize, Deserialize, PartialEq, Debug)]
//! struct Settings {
//! name: String,
//! retries: u8,
//! }
//!
//! let codec = JsonCodec::default();
//! let parsed: Settings = decode(&codec, r#"{ "name": "svc", "retries": 3 }"#).unwrap();
//! assert_eq!(parsed, Settings { name: "svc".into(), retries: 3 });
//!
//! let text = encode(&codec, &parsed).unwrap();
//! assert!(text.contains("\"name\""));
//! ```
pub use ;
pub use ;
pub use TomlCodec;
/// Re-export of the canonical value model.
pub use Value;