strict_encoding/
error.rs

1// Strict encoding library for deterministic binary serialization.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Designed in 2019-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
6// Written in 2024-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
7//
8// Copyright (C) 2019-2022 LNP/BP Standards Association.
9// Copyright (C) 2022-2025 Laboratories for Ubiquitous Deterministic Computing (UBIDECO),
10//                         Institute for Distributed and Cognitive Systems (InDCS), Switzerland.
11// Copyright (C) 2019-2025 Dr Maxim Orlovsky.
12// All rights under the above copyrights are reserved.
13//
14// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
15// in compliance with the License. You may obtain a copy of the License at
16//
17//        http://www.apache.org/licenses/LICENSE-2.0
18//
19// Unless required by applicable law or agreed to in writing, software distributed under the License
20// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
21// or implied. See the License for the specific language governing permissions and limitations under
22// the License.
23
24use std::io;
25use std::ops::Range;
26
27use amplify::{confinement, IoError};
28
29#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
30#[display(doc_comments)]
31pub enum DecodeError {
32    #[display(inner)]
33    #[from(io::Error)]
34    Io(IoError),
35
36    /// confinement requirements are not satisfied. Specifically, {0}
37    #[from]
38    Confinement(confinement::Error),
39
40    /// non-zero natural number can't have a value equal to zero.
41    ZeroNatural,
42
43    /// string data are not in valid UTF-8 encoding.\nDetails: {0}
44    #[from]
45    Utf8(std::string::FromUtf8Error),
46
47    /// string data are not in valid UTF-8 encoding.\nDetails: {0}
48    #[from]
49    Ascii(amplify::ascii::AsAsciiStrError),
50
51    /// value occurs multiple times in a set
52    RepeatedSetValue,
53
54    /// key occurs multiple times in a map
55    RepeatedMapValue,
56
57    /// unsupported value `{1}` for enum `{0}` encountered during decode
58    /// operation
59    EnumTagNotKnown(String, u8),
60
61    /// unsupported value `{1}` for union `{0}` encountered during decode
62    /// operation
63    UnionTagNotKnown(String, u8),
64
65    /// decoding resulted in value `{2}` for type `{0}` that exceeds the
66    /// supported range {1:#?}
67    ValueOutOfRange(String, Range<u128>, u128),
68
69    /// encoded values are not deterministically ordered within a set
70    BrokenSetOrder,
71
72    /// encoded map has wrong order of keys
73    BrokenMapOrder,
74
75    /// data integrity problem during strict decoding operation.\nDetails: {0}
76    DataIntegrityError(String),
77}
78
79#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
80#[display(doc_comments)]
81pub enum SerializeError {
82    #[display(inner)]
83    #[from(io::Error)]
84    Io(IoError),
85
86    /// confinement requirements are not satisfied. Specifically, {0}
87    #[from]
88    Confinement(confinement::Error),
89}
90
91#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
92#[display(doc_comments)]
93pub enum DeserializeError {
94    #[display(inner)]
95    #[from]
96    #[from(io::Error)]
97    Decode(DecodeError),
98
99    /// data are not entirely consumed during strict deserialize operation
100    DataNotEntirelyConsumed,
101}