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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// =============================================================================
// Copyright (c) 2025 - 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! # qubit-metadata
//!
//! A general-purpose, typed-value, extensible metadata model for Rust.
//!
//! This crate provides a [`Metadata`] type — a structured key-value store
//! designed for any domain that needs to attach typed annotations to its data
//! models. It is not a plain `HashMap` — it is a structured extensibility point
//! with strict typed access, explicit conversion helpers,
//! [`qubit_value::Value`] backing, and first-class `serde` support.
//!
//! ## Design Goals
//!
//! - **Typed Values**: Strict typed reads, explicit conversion helpers,
//! chainable set, and replacement-aware insert APIs backed by
//! [`qubit_value::Value`]
//! - **Generality**: No domain-specific assumptions — usable in any Rust
//! project
//! - **Schema Support**: Optional schema validation for metadata and filters
//! through the `schema` feature
//! - **Serialization**: First-class `serde` support for JSON interchange
//! - **Filtering**: Composable query conditions through the `filter` feature
//!
//! ## Features
//!
//! - Core type: [`Metadata`] — an ordered key-value store with typed accessors
//! - Enable `filter` for composable filter expressions and their public types
//! - Enable `schema` (which includes `filter`) for field definitions and
//! validation APIs
//! - Enable `json` for bounded JSON-slice decoding and buffered encoding; JSON
//! APIs for schemas and filters also require their respective features
//! - Error type: [`MetadataError`] — structured failure reporting for reads,
//! conversions, validation, and wire boundaries
//! - `schema` also provides aggregate validation errors
//!
//! ## Example
//!
//! ```rust
//! use qubit_metadata::Metadata;
//!
//! let meta = Metadata::new()
//! .with("author", "alice")
//! .with("priority", 3_i64);
//!
//! // Optional strict reads distinguish absence from conversion/type failures.
//! let author = meta.get_optional::<String>("author").unwrap();
//! assert_eq!(author.as_deref(), Some("alice"));
//!
//! // Explicit API: preserve failure reasons for diagnostics.
//! let priority = meta.convert::<i64>("priority").unwrap();
//! assert_eq!(priority, 3);
//! ```
//!
//! Comparison predicates in the `filter` feature use three-valued logic: a
//! missing or unset value is unknown, remains unknown through negation, and
//! does not match at the public API boundary. Existence predicates are an
//! exception: `exists` and `not_exists` always return definite Booleans, and
//! `exists(...).not()` is true when no concrete value is stored.
//!
//! ## Bounded JSON
//!
//! Enable the `json` feature to use the bounded JSON helpers. The default
//! helpers use the crate's default decode or encode budget profile; use the
//! corresponding `_with_limits` helper to configure a profile for that
//! operation. Add `features = ["json"]` to the crate dependency. Decode and
//! encode budgets are independent.
//!
//! ```rust
//! # #[cfg(feature = "json")]
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use qubit_metadata::Metadata;
//!
//! let metadata = Metadata::new().with("tenant_id", "acme");
//! let encoded = metadata.to_json_vec()?;
//! let decoded = Metadata::decode_json_slice(&encoded)?;
//! assert_eq!(decoded.get_ref::<str>("tenant_id")?, "acme");
//! # Ok(())
//! # }
//! # #[cfg(not(feature = "json"))]
//! # fn main() {}
//! ```
//!
//! For complete untrusted JSON input, use the bounded slice decoder. Ordinary
//! Serde paths still check the domain wire contract, but do not automatically
//! provide the dedicated helper's complete-input byte admission and unified
//! JSON traversal budget. See the [bounded JSON decoding guide](https://github.com/qubit-ltd/rs-metadata/blob/main/doc/user_guide.md#bounded-json-decoding)
//! and [bounded JSON encoding guide](https://github.com/qubit-ltd/rs-metadata/blob/main/doc/user_guide.md#bounded-json-encoding)
//! for configuration and writer behavior. Writer helpers buffer the complete
//! document; a destination I/O error may leave a written prefix.
//!
//! ```rust
//! # #[cfg(feature = "filter")]
//! # fn main() -> qubit_metadata::MetadataResult<()> {
//! use qubit_metadata::FilterExpression;
//! use qubit_metadata::Metadata;
//! use qubit_metadata::MetadataFilter;
//!
//! let metadata = Metadata::new();
//! let comparison = MetadataFilter::builder()
//! .expression(FilterExpression::builder().eq("tenant", "acme").not().build()?)
//! .build()?;
//! let absent = MetadataFilter::builder()
//! .expression(FilterExpression::builder().not_exists("tenant").build()?)
//! .build()?;
//! let negated_exists = MetadataFilter::builder()
//! .expression(FilterExpression::builder().exists("tenant").not().build()?)
//! .build()?;
//! assert!(!comparison.matches(&metadata));
//! assert!(absent.matches(&metadata));
//! assert!(negated_exists.matches(&metadata));
//! # Ok(())
//! # }
//! # #[cfg(not(feature = "filter"))]
//! # fn main() {}
//! ```
//!
//! `Metadata`'s `Debug` and `Display` implementations are bounded and rendered
//! through the default redaction policy. They are useful diagnostic views, not
//! a confidentiality boundary for arbitrary user-defined keys or error text.
pub use Condition;
pub use FilterExpression;
pub use FilterExpressionBuilder;
pub use FilterExpressionView;
pub use FilterLimitKind;
pub use FilterLimits;
pub use FilterLimitsBuilder;
pub use FilterMatchOptions;
pub use FilterMatchOptionsBuilder;
pub use MetadataFilter;
pub use MetadataFilterBuilder;
pub use Metadata;
pub use MetadataError;
pub use DEFAULT_MAX_JSON_BYTES;
pub use DEFAULT_MAX_KEY_BYTES;
pub use DEFAULT_MAX_METADATA_ENTRIES;
pub use DEFAULT_MAX_SCHEMA_FIELDS;
pub use MetadataLimits;
pub use default_json_decode_limits;
pub use default_json_encode_limits;
pub use default_json_value_limits;
pub use MetadataLimitsBuilder;
pub use MetadataResult;
pub use MetadataValidationError;
pub use MetadataValidationResult;
pub use MetadataWireDecodeError;
pub use MetadataWireEncodeError;
pub use MetadataWireLimitKind;
pub use MetadataField;
pub use MetadataSchema;
pub use MetadataSchemaBuilder;
pub use UnknownFilterFieldPolicy;
pub use UnknownMetadataFieldPolicy;