risc0_zkvm_serde/
err.rs

1// Copyright 2022 Risc0, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use core::fmt::{Display, Formatter};
16
17// use alloc::string::{String, ToString};
18
19#[derive(Clone, Debug, Eq, PartialEq)]
20pub enum Error {
21    // Custom(String),
22    DeserializeBadBool,
23    DeserializeBadChar,
24    DeserializeBadOption,
25    DeserializeBadUtf8,
26    DeserializeUnexpectedEnd,
27    NotSupported,
28    SerializeBufferFull,
29}
30
31pub type Result<T> = core::result::Result<T, Error>;
32
33impl Display for Error {
34    fn fmt(&self, formatter: &mut Formatter) -> core::fmt::Result {
35        formatter.write_str(match self {
36            // Self::Custom(msg) => msg,
37            Self::DeserializeBadBool => "Found a bool that wasn't 0 or 1",
38            Self::DeserializeBadChar => "Found an invalid unicode char",
39            Self::DeserializeBadOption => "Found an Option discriminant that wasn't 0 or 1",
40            Self::DeserializeBadUtf8 => "Tried to parse invalid utf-8",
41            Self::DeserializeUnexpectedEnd => "Unexpected end during deserialization",
42            Self::NotSupported => "Not supported",
43            Self::SerializeBufferFull => "The serialize buffer is full",
44        })
45    }
46}
47
48impl serde::ser::Error for Error {
49    fn custom<T: Display>(_msg: T) -> Self {
50        // Error::Custom(msg.to_string())
51        Error::NotSupported
52    }
53}
54
55impl serde::de::Error for Error {
56    fn custom<T: Display>(_msg: T) -> Self {
57        // Error::Custom(msg.to_string())
58        Error::NotSupported
59    }
60}
61
62// This is an alias for either std::Error, or serde's no_std error replacement.
63impl serde::ser::StdError for Error {}