Skip to main content

qubit_codec/value/
value_codec_registry_error.rs

1// =============================================================================
2//    Copyright (c) 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8
9//! Value-codec registry errors.
10
11use thiserror::Error;
12
13use crate::ValueCodecRegistrationSource;
14
15/// Failure while freezing a value-codec registry.
16///
17/// Freezing rejects duplicate stable IDs and reports the lexically first
18/// conflicting ID together with all of its declaration sites. Sources are
19/// sorted by their fields; repeated identical sources are retained because
20/// each registration is reported.
21///
22/// The registry returns this error when `ValueStringCodecRegistry::try_global`
23/// or `ValueBytesCodecRegistry::try_global` encounters conflicting linked
24/// registrations.
25///
26/// # Examples
27///
28/// ```
29/// use qubit_codec::ValueBytesCodecRegistry;
30/// use qubit_codec::ValueCodecRegistryError;
31///
32/// fn describe_registry_error(error: ValueCodecRegistryError) -> String {
33///     match error {
34///         ValueCodecRegistryError::DuplicateId { id, sources } => {
35///             format!("duplicate value codec ID {id} from {} source(s)", sources.len())
36///         }
37///     }
38/// }
39///
40/// if let Err(error) = ValueBytesCodecRegistry::try_global() {
41///     let description = describe_registry_error(error);
42///     assert!(description.starts_with("duplicate value codec ID "));
43/// }
44/// ```
45#[derive(Clone, Debug, Error)]
46#[must_use]
47pub enum ValueCodecRegistryError {
48    /// Multiple registrations claim one stable ID.
49    #[error("duplicate value codec ID {id} from {sources:?}")]
50    DuplicateId {
51        /// Conflicting stable ID.
52        id: &'static str,
53        /// Registration sources in deterministic order.
54        sources: Vec<ValueCodecRegistrationSource>,
55    },
56}