Skip to main content

dpp_domain/schemas/
types.rs

1//! Supporting types for the versioned schema registry: origin tracking,
2//! entries, and registration errors.
3
4use semver::Version;
5
6/// Tracks whether a schema was baked in at compile time or loaded at runtime.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum SchemaOrigin {
10    /// Compiled into the binary via `include_str!()`.
11    Embedded,
12    /// Loaded at runtime via [`crate::schemas::VersionedSchemaRegistry::register`].
13    Runtime,
14}
15
16/// A single (sector, version) → JSON schema mapping.
17#[derive(Debug, Clone)]
18pub struct SchemaEntry {
19    pub sector: String,
20    pub version: Version,
21    pub json: String,
22    pub origin: SchemaOrigin,
23}
24
25/// Errors returned by [`crate::schemas::VersionedSchemaRegistry::register`].
26#[derive(Debug, Clone, PartialEq, Eq)]
27#[non_exhaustive]
28pub enum SchemaRegistrationError {
29    /// The provided JSON string is not valid JSON.
30    InvalidJson(String),
31    /// A schema for this (sector, version) already exists.
32    /// Use `register_or_replace` to overwrite.
33    AlreadyExists { sector: String, version: Version },
34    /// The version string is not valid semver.
35    InvalidVersion(String),
36}
37
38impl std::fmt::Display for SchemaRegistrationError {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        match self {
41            Self::InvalidJson(msg) => write!(f, "invalid JSON schema: {msg}"),
42            Self::AlreadyExists { sector, version } => {
43                write!(f, "schema already exists for {sector} v{version}")
44            }
45            Self::InvalidVersion(v) => write!(f, "invalid semver version: {v}"),
46        }
47    }
48}
49
50impl std::error::Error for SchemaRegistrationError {}