p2panda_rs/schema/
error.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3//! Error types for creating schema instances and schema ids.
4use thiserror::Error;
5
6use crate::schema::SchemaId;
7
8/// Custom errors related to `SchemaName`.
9#[derive(Clone, Error, Debug)]
10pub enum SchemaNameError {
11    /// Encountered a malformed schema name.
12    #[error("Schema name contains too many or invalid characters")]
13    MalformedSchemaName,
14}
15
16impl Copy for SchemaNameError {}
17
18/// Custom errors related to `SchemaDescription`.
19#[derive(Clone, Error, Debug)]
20pub enum SchemaDescriptionError {
21    /// Encountered a too long schema description.
22    #[error("Schema description contains more than 256 characters")]
23    TooLongSchemaDescription,
24}
25
26/// Custom errors related to `SchemaFields`.
27#[derive(Clone, Error, Debug)]
28pub enum SchemaFieldError {
29    /// Encountered an invalid name in a schema field key.
30    #[error("Schema field found with invalid name")]
31    MalformedSchemaFieldName,
32
33    /// Maximum number of schema fields (1024) has been exceeded.
34    #[error("Schema fields contains more than 1024 fields")]
35    TooManyFields,
36
37    /// Schema fields length must be at least 1.
38    #[error("Schema fields must contain at least one entry")]
39    ZeroFields,
40
41    /// Schema fields cannot contain duplicate fields.
42    #[error("Schema fields cannot contain duplicate field names")]
43    DuplicateFields,
44}
45
46/// Custom errors related to `SchemaId`.
47#[derive(Error, Debug)]
48pub enum SchemaIdError {
49    /// Encountered a malformed schema id.
50    #[error("malformed schema id `{0}`: {1}")]
51    MalformedSchemaId(String, String),
52
53    /// Application schema ids must start with the schema's name.
54    #[error("application schema id is missing a name: {0}")]
55    MissingApplicationSchemaName(String),
56
57    /// Invalid system schema id.
58    #[error("unsupported system schema: {0}")]
59    UnknownSystemSchema(String),
60
61    /// Invalid hash in schema id.
62    #[error("encountered invalid hash while parsing application schema id: {0}")]
63    HashError(#[from] crate::hash::error::HashError),
64
65    /// Handle errors from validating document view ids.
66    #[error("encountered invalid document view id while parsing application schema id: {0}")]
67    DocumentViewIdError(#[from] crate::document::error::DocumentViewIdError),
68
69    /// Handle errors from validating operation ids.
70    #[error("encountered invalid hash while parsing application schema id: {0}")]
71    OperationIdError(#[from] crate::operation::error::OperationIdError),
72}
73
74/// Custom errors related to `Schema`.
75#[derive(Error, Debug)]
76pub enum SchemaError {
77    /// Invalid fields in schema.
78    #[error("invalid fields found for this schema")]
79    InvalidFields,
80
81    /// Use static definitions of system schemas instead of defining them dynamically.
82    #[error("dynamic redefinition of system schema {0}, use `Schema::get_system` instead")]
83    DynamicSystemSchema(SchemaId),
84
85    /// Schemas must have valid schema ids.
86    #[error(transparent)]
87    SchemaIdError(#[from] SchemaIdError),
88
89    /// Schemas must have valid schema names.
90    #[error(transparent)]
91    SchemaNameError(#[from] SchemaNameError),
92
93    /// Schemas must have valid schema descriptions.
94    #[error(transparent)]
95    SchemaDescriptionError(#[from] SchemaDescriptionError),
96
97    /// Schemas must have valid schema fields.
98    #[error(transparent)]
99    SchemaFieldsError(#[from] SchemaFieldError),
100}
101
102/// Custom error types for field types.
103#[derive(Error, Debug)]
104pub enum FieldTypeError {
105    /// Invalid field type found.
106    #[error("invalid field type '{0}'")]
107    InvalidFieldType(String),
108
109    /// Schema ids referenced by relation field types need to be valid.
110    #[error(transparent)]
111    RelationSchemaReference(#[from] SchemaIdError),
112}