strict_encoding/
error.rs

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