regit-web3 1.0.1

Modular Web3 primitives for exact values, chain and provider reads, swap quotes, and transaction preparation
Documentation
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 Regit

//! Bounded, ordered collections of independently contextualized observations.
//!
//! Keys and observations remain caller-selected types. A collection neither
//! reads data nor establishes a shared snapshot, remote completeness, or a
//! relationship between a key and its observation. Family observation types
//! retain their own validation and source, network, and evaluation contexts.

use std::{fmt, marker::PhantomData};

use serde::{
    Deserialize, Deserializer, Serialize,
    de::{self, DeserializeSeed, MapAccess, SeqAccess, Visitor},
};

use crate::error::Error;

/// A fixed, input-free collection validation failure.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum CollectionError {
    /// The item limit is zero or exceeds [`CollectionLimit::MAX_ITEMS`].
    InvalidLimit,
    /// More items were supplied than the explicitly configured limit permits.
    TooManyItems,
    /// At least two supplied keys compare equal.
    DuplicateKey,
}

impl fmt::Display for CollectionError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(match self {
            Self::InvalidLimit => "invalid observation collection limit",
            Self::TooManyItems => "observation collection item limit exceeded",
            Self::DuplicateKey => "duplicate observation collection key",
        })
    }
}

impl std::error::Error for CollectionError {}

/// An explicit maximum item count between one and 1,024 inclusive.
///
/// Serialization uses an integer. Deserialization applies the constructor's
/// same bound; there is no implicit default or unlimited mode.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "usize", into = "usize")]
pub struct CollectionLimit(usize);

impl CollectionLimit {
    /// The absolute item-count ceiling for construction and deserialization.
    pub const MAX_ITEMS: usize = 1_024;

    /// Constructs an explicitly bounded item limit.
    ///
    /// # Errors
    /// Returns [`CollectionError::InvalidLimit`] for zero or a value above
    /// [`Self::MAX_ITEMS`].
    pub const fn new(maximum: usize) -> Result<Self, CollectionError> {
        if maximum == 0 || maximum > Self::MAX_ITEMS {
            return Err(CollectionError::InvalidLimit);
        }
        Ok(Self(maximum))
    }

    /// Returns the explicitly configured maximum item count.
    #[must_use]
    pub const fn get(self) -> usize {
        self.0
    }
}

impl TryFrom<usize> for CollectionLimit {
    type Error = CollectionError;

    fn try_from(maximum: usize) -> Result<Self, Self::Error> {
        Self::new(maximum)
    }
}

impl From<CollectionLimit> for usize {
    fn from(limit: CollectionLimit) -> Self {
        limit.get()
    }
}

/// One exact caller-selected key and either a typed observation or failure.
///
/// A failed item retains its key and the existing fixed [`Error`] category.
/// Successes retain the observation unchanged. The caller establishes whether
/// a key identifies its observation; no key normalization or context is added.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ObservationItem<K, O> {
    key: K,
    outcome: Result<O, Error>,
}

impl<K, O> ObservationItem<K, O> {
    /// Records an exact key and its explicit success or failure.
    #[must_use]
    pub const fn new(key: K, outcome: Result<O, Error>) -> Self {
        Self { key, outcome }
    }

    /// Returns the unchanged caller-selected key.
    #[must_use]
    pub const fn key(&self) -> &K {
        &self.key
    }

    /// Returns the typed observation or fixed item failure.
    pub const fn outcome(&self) -> &Result<O, Error> {
        &self.outcome
    }

    /// Returns the successful observation, if this item succeeded.
    #[must_use]
    pub fn observation(&self) -> Option<&O> {
        self.outcome.as_ref().ok()
    }

    /// Returns the typed failure, if this item failed.
    #[must_use]
    pub fn failure(&self) -> Option<Error> {
        self.outcome.as_ref().err().copied()
    }

    /// Consumes the item and returns its exact key and outcome.
    pub fn into_parts(self) -> (K, Result<O, Error>) {
        (self.key, self.outcome)
    }
}

/// An explicitly bounded collection retaining caller order and unique keys.
///
/// Equality of keys uses only `K: Eq`, permitting existing family identities
/// without imposing hashing, sorting, or normalization. Construction checks
/// duplicates in bounded quadratic time; lookup is linear in at most 1,024
/// items. Supplied observation values and contexts remain unchanged, including
/// different networks, sources, retrieval times, and evaluation anchors.
///
/// Deserialization buffers at most [`CollectionLimit::MAX_ITEMS`] items, never
/// allocates from an untrusted sequence size hint, and rejects an extra item
/// before decoding its key or outcome. When `limit` precedes `items`, decoding
/// uses the configured lower bound immediately. Otherwise the absolute ceiling
/// applies while decoding and the configured limit is checked afterward.
/// Memory within each key or observation remains governed by its own type and
/// deserializer; this container bounds item count, not input bytes.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct ObservationCollection<K, O> {
    limit: CollectionLimit,
    items: Vec<ObservationItem<K, O>>,
}

impl<K: Eq, O> ObservationCollection<K, O> {
    /// Validates a supplied ordered collection without changing keys or values.
    ///
    /// Empty collections are permitted. Every supplied key must be unique,
    /// including keys of failed items. Construction does not fetch missing
    /// items or prove that all requested or remote records were supplied.
    ///
    /// # Errors
    /// Returns [`CollectionError::TooManyItems`] when the item count exceeds
    /// `limit`, or [`CollectionError::DuplicateKey`] for equal supplied keys.
    pub fn new(
        limit: CollectionLimit,
        items: Vec<ObservationItem<K, O>>,
    ) -> Result<Self, CollectionError> {
        if items.len() > limit.get() {
            return Err(CollectionError::TooManyItems);
        }
        for (index, item) in items.iter().enumerate() {
            if items[..index]
                .iter()
                .any(|previous| previous.key == item.key)
            {
                return Err(CollectionError::DuplicateKey);
            }
        }
        Ok(Self { limit, items })
    }

    /// Finds a supplied item by exact key equality in bounded linear time.
    #[must_use]
    pub fn get(&self, key: &K) -> Option<&ObservationItem<K, O>> {
        self.items.iter().find(|item| item.key() == key)
    }
}

impl<K, O> ObservationCollection<K, O> {
    /// Returns the explicitly supplied item-count limit.
    #[must_use]
    pub const fn limit(&self) -> CollectionLimit {
        self.limit
    }

    /// Returns all supplied items in their original order.
    #[must_use]
    pub fn items(&self) -> &[ObservationItem<K, O>] {
        &self.items
    }

    /// Returns the number of supplied successful and failed items together.
    #[must_use]
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// Returns whether no items were supplied.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    /// Counts successful outcomes among supplied items only.
    #[must_use]
    pub fn success_count(&self) -> usize {
        self.items
            .iter()
            .filter(|item| item.outcome.is_ok())
            .count()
    }

    /// Counts explicit failures among supplied items only.
    #[must_use]
    pub fn failure_count(&self) -> usize {
        self.len() - self.success_count()
    }

    /// Returns whether every supplied item succeeded, including an empty set.
    ///
    /// This describes supplied outcomes only. It does not prove that every
    /// requested item was included, remote coverage, or a shared snapshot.
    #[must_use]
    pub fn all_succeeded(&self) -> bool {
        self.items.iter().all(|item| item.outcome.is_ok())
    }

    /// Consumes the collection and returns its limit and ordered items.
    #[must_use]
    pub fn into_parts(self) -> (CollectionLimit, Vec<ObservationItem<K, O>>) {
        (self.limit, self.items)
    }
}

impl<'de, K, O> Deserialize<'de> for ObservationCollection<K, O>
where
    K: Deserialize<'de> + Eq,
    O: Deserialize<'de>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_struct(
            "ObservationCollection",
            &["limit", "items"],
            CollectionVisitor(PhantomData),
        )
    }
}

enum Field {
    Limit,
    Items,
}

impl<'de> Deserialize<'de> for Field {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct FieldVisitor;

        impl Visitor<'_> for FieldVisitor {
            type Value = Field;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str("an observation collection field")
            }

            fn visit_str<E: de::Error>(self, field: &str) -> Result<Field, E> {
                match field {
                    "limit" => Ok(Field::Limit),
                    "items" => Ok(Field::Items),
                    _ => Err(E::custom("unknown observation collection field")),
                }
            }

            fn visit_bytes<E: de::Error>(self, field: &[u8]) -> Result<Field, E> {
                match field {
                    b"limit" => Ok(Field::Limit),
                    b"items" => Ok(Field::Items),
                    _ => Err(E::custom("unknown observation collection field")),
                }
            }

            fn visit_u64<E: de::Error>(self, field: u64) -> Result<Field, E> {
                match field {
                    0 => Ok(Field::Limit),
                    1 => Ok(Field::Items),
                    _ => Err(E::custom("unknown observation collection field")),
                }
            }
        }

        deserializer.deserialize_identifier(FieldVisitor)
    }
}

struct CollectionVisitor<K, O>(PhantomData<(K, O)>);

impl<'de, K, O> Visitor<'de> for CollectionVisitor<K, O>
where
    K: Deserialize<'de> + Eq,
    O: Deserialize<'de>,
{
    type Value = ObservationCollection<K, O>;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("an observation collection with an explicit limit and items")
    }

    fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
        let mut limit: Option<CollectionLimit> = None;
        let mut items = None;
        while let Some(field) = map.next_key()? {
            match field {
                Field::Limit => {
                    if limit.is_some() {
                        return Err(de::Error::duplicate_field("limit"));
                    }
                    limit = Some(map.next_value()?);
                }
                Field::Items => {
                    if items.is_some() {
                        return Err(de::Error::duplicate_field("items"));
                    }
                    items = Some(map.next_value_seed(ItemsSeed {
                        maximum: limit.map_or(CollectionLimit::MAX_ITEMS, CollectionLimit::get),
                        marker: PhantomData,
                    })?);
                }
            }
        }
        let limit = limit.ok_or_else(|| de::Error::missing_field("limit"))?;
        let items = items.ok_or_else(|| de::Error::missing_field("items"))?;
        ObservationCollection::new(limit, items).map_err(de::Error::custom)
    }

    fn visit_seq<A: SeqAccess<'de>>(self, mut sequence: A) -> Result<Self::Value, A::Error> {
        let limit: CollectionLimit = sequence
            .next_element()?
            .ok_or_else(|| de::Error::invalid_length(0, &self))?;
        let items = sequence
            .next_element_seed(ItemsSeed {
                maximum: limit.get(),
                marker: PhantomData,
            })?
            .ok_or_else(|| de::Error::invalid_length(1, &self))?;
        sequence.next_element_seed(RejectValue("unexpected observation collection field"))?;
        ObservationCollection::new(limit, items).map_err(de::Error::custom)
    }
}

struct ItemsSeed<K, O> {
    maximum: usize,
    marker: PhantomData<(K, O)>,
}

impl<'de, K: Deserialize<'de>, O: Deserialize<'de>> DeserializeSeed<'de> for ItemsSeed<K, O> {
    type Value = Vec<ObservationItem<K, O>>;

    fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<Self::Value, D::Error> {
        deserializer.deserialize_seq(self)
    }
}

impl<'de, K: Deserialize<'de>, O: Deserialize<'de>> Visitor<'de> for ItemsSeed<K, O> {
    type Value = Vec<ObservationItem<K, O>>;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("an ordered, bounded observation item sequence")
    }

    fn visit_seq<A: SeqAccess<'de>>(self, mut sequence: A) -> Result<Self::Value, A::Error> {
        let mut items = Vec::new();
        while items.len() < self.maximum {
            match sequence.next_element()? {
                Some(item) => items.push(item),
                None => return Ok(items),
            }
        }
        sequence.next_element_seed(RejectValue("observation collection item limit exceeded"))?;
        Ok(items)
    }
}

struct RejectValue(&'static str);

impl<'de> DeserializeSeed<'de> for RejectValue {
    type Value = ();

    fn deserialize<D: Deserializer<'de>>(self, _deserializer: D) -> Result<Self::Value, D::Error> {
        Err(de::Error::custom(self.0))
    }
}