vest_lib/
errors.rs

1// pub use crate::utils::*;
2use vstd::prelude::*;
3use vstd::*;
4
5use alloc::string::String;
6
7verus! {
8
9/// Parser errors
10#[derive(Debug)]
11pub enum ParseError {
12    /// The second combinator of AndThen did not consume all bytes
13    AndThenUnusedBytes,
14    UnexpectedEndOfInput,
15    OrdChoiceNoMatch,
16    CondFailed,
17    TryMapFailed,
18    RefinedPredicateFailed,
19    NotEof,
20    Other(String),
21}
22
23/// Serializer errors
24#[derive(Debug)]
25pub enum SerializeError {
26    InsufficientBuffer,
27    Other(String),
28}
29
30/// Sum of both parse and serialize errors
31#[derive(Debug)]
32pub enum Error {
33    /// Parser error
34    Parse(ParseError),
35    /// Serializer error
36    Serialize(SerializeError),
37}
38
39
40#[cfg(verus_keep_ghost)]
41impl vstd::std_specs::convert::FromSpecImpl<ParseError> for Error {
42    open spec fn obeys_from_spec() -> bool {
43        true
44    }
45
46    open spec fn from_spec(e: ParseError) -> Self {
47        Error::Parse(e)
48    }
49}
50
51#[cfg(verus_keep_ghost)]
52impl vstd::std_specs::convert::FromSpecImpl<SerializeError> for Error {
53    open spec fn obeys_from_spec() -> bool {
54        true
55    }
56
57    open spec fn from_spec(e: SerializeError) -> Self {
58        Error::Serialize(e)
59    }
60}
61
62impl core::convert::From<ParseError> for Error {
63    fn from(e: ParseError) -> Self {
64        Error::Parse(e)
65    }
66}
67
68impl core::convert::From<SerializeError> for Error {
69    fn from(e: SerializeError) -> Self {
70        Error::Serialize(e)
71    }
72}
73
74} // verus!
75impl core::fmt::Display for ParseError {
76    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
77        match self {
78            ParseError::AndThenUnusedBytes => {
79                write!(f, "`AndThen` combinator did not consume all bytes")
80            }
81            ParseError::UnexpectedEndOfInput => write!(f, "Unexpected end of input"),
82            ParseError::OrdChoiceNoMatch => {
83                write!(f, "`OrdChoice` combinator did not match any of its options")
84            }
85            ParseError::CondFailed => write!(f, "`Cond` combinator failed"),
86            ParseError::TryMapFailed => write!(f, "`TryMap` combinator failed"),
87            ParseError::RefinedPredicateFailed => {
88                write!(f, "`Refined` combinator predicate failed")
89            }
90            ParseError::NotEof => write!(f, "Expected end of input"),
91            ParseError::Other(s) => write!(f, "{}", s),
92        }
93    }
94}
95
96impl core::fmt::Display for SerializeError {
97    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
98        match self {
99            SerializeError::InsufficientBuffer => write!(f, "Insufficient buffer"),
100            SerializeError::Other(s) => write!(f, "{}", s),
101        }
102    }
103}
104
105impl core::error::Error for ParseError {}
106
107impl core::error::Error for SerializeError {}