yomi_core/package/
error.rs1use super::*;
4
5#[non_exhaustive]
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum CommandErrorKind {
9 Failed {
11 code: String,
13 args: Vec<(&'static str, DiagArg)>,
15 },
16 IntegerOverflow,
18 NonFiniteFloat,
20 FloatOutOfRange,
22 DivisionByZero,
24 Interrupted,
26}
27
28#[derive(Debug, Clone, PartialEq, Eq)]
34pub struct CommandError {
35 pub(super) kind: CommandErrorKind,
36}
37
38impl CommandError {
39 pub fn with_code<I>(code: impl Into<String>, args: I) -> Self
44 where
45 I: IntoIterator<Item = (&'static str, DiagArg)>,
46 {
47 Self {
48 kind: CommandErrorKind::Failed {
49 code: code.into(),
50 args: args.into_iter().collect(),
51 },
52 }
53 }
54
55 #[must_use]
57 pub fn arg(mut self, name: &'static str, value: impl Into<DiagArg>) -> Self {
58 if let CommandErrorKind::Failed { args, .. } = &mut self.kind {
59 args.push((name, value.into()));
60 }
61 self
62 }
63
64 pub fn integer_overflow() -> Self {
66 Self {
67 kind: CommandErrorKind::IntegerOverflow,
68 }
69 }
70
71 pub fn non_finite_float() -> Self {
73 Self {
74 kind: CommandErrorKind::NonFiniteFloat,
75 }
76 }
77
78 pub fn float_out_of_range() -> Self {
80 Self {
81 kind: CommandErrorKind::FloatOutOfRange,
82 }
83 }
84
85 pub fn division_by_zero() -> Self {
87 Self {
88 kind: CommandErrorKind::DivisionByZero,
89 }
90 }
91
92 pub fn interrupted() -> Self {
94 Self {
95 kind: CommandErrorKind::Interrupted,
96 }
97 }
98
99 pub fn kind(&self) -> &CommandErrorKind {
101 &self.kind
102 }
103
104 pub fn code(&self) -> &str {
106 match &self.kind {
107 CommandErrorKind::Failed { code, .. } => code,
108 CommandErrorKind::IntegerOverflow => "integer_overflow",
109 CommandErrorKind::NonFiniteFloat => "non_finite_float",
110 CommandErrorKind::FloatOutOfRange => "float_out_of_range",
111 CommandErrorKind::DivisionByZero => "division_by_zero",
112 CommandErrorKind::Interrupted => "interrupted",
113 }
114 }
115
116 pub fn args(&self) -> &[(&'static str, DiagArg)] {
118 match &self.kind {
119 CommandErrorKind::Failed { args, .. } => args,
120 _ => &[],
121 }
122 }
123
124 pub(crate) fn into_vm_kind(self) -> VmErrorKind {
126 match self.kind {
127 CommandErrorKind::Failed { code, args } => VmErrorKind::CommandFailed { code, args },
128 CommandErrorKind::IntegerOverflow => VmErrorKind::IntegerOverflow,
129 CommandErrorKind::NonFiniteFloat => VmErrorKind::NonFiniteFloat,
130 CommandErrorKind::FloatOutOfRange => VmErrorKind::FloatOutOfRange,
131 CommandErrorKind::DivisionByZero => VmErrorKind::DivisionByZero,
132 CommandErrorKind::Interrupted => VmErrorKind::Interrupted,
133 }
134 }
135}
136
137impl std::fmt::Display for CommandError {
139 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
140 write!(f, "{}", self.code())?;
141 let args = self.args();
142 if !args.is_empty() {
143 write!(f, " (")?;
144 for (i, (name, value)) in args.iter().enumerate() {
145 if i > 0 {
146 write!(f, ", ")?;
147 }
148 write!(f, "{name}={value}")?;
149 }
150 write!(f, ")")?;
151 }
152 Ok(())
153 }
154}
155
156impl std::error::Error for CommandError {}
157
158#[derive(Debug, Clone, PartialEq, Eq)]
160pub struct MigrateError {
161 pub(super) message: String,
162}
163
164impl MigrateError {
165 pub fn new(message: impl Into<String>) -> Self {
167 Self {
168 message: message.into(),
169 }
170 }
171
172 pub fn unsupported_version(old_version: u32) -> Self {
174 Self::new(format!("unsupported slice version {old_version}"))
175 }
176
177 pub fn message(&self) -> &str {
179 &self.message
180 }
181}
182
183impl std::fmt::Display for MigrateError {
184 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
185 write!(f, "{}", self.message)
186 }
187}
188
189impl std::error::Error for MigrateError {}
190
191pub fn decode_slice<T: DeserializeOwned>(raw: &[u8]) -> Result<T, MigrateError> {
194 postcard::from_bytes(raw).map_err(|e| MigrateError::new(format!("decode: {e}")))
195}
196
197pub fn encode_slice<T: Serialize>(value: &T) -> Result<Vec<u8>, MigrateError> {
199 postcard::to_stdvec(value).map_err(|e| MigrateError::new(format!("encode: {e}")))
200}