re/
form.rs

1use std::{fmt, fmt::Display};
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Serialize, Deserialize, Default)]
6pub struct Error(Vec<(String, String)>);
7
8impl Display for Error {
9  fn fmt(&self, f: &mut fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
10    write!(f, "{}", sonic_rs::to_string(&self).unwrap())
11  }
12}
13
14impl std::error::Error for Error {}
15
16pub type Result = std::result::Result<(), Error>;
17
18impl Error {
19  pub fn new() -> Self {
20    Self::default()
21  }
22
23  pub fn ok(self) -> Result {
24    if !self.0.is_empty() {
25      return Err(self)?;
26    }
27    Ok(())
28  }
29
30  pub fn throw(key: impl Into<String>, val: impl Into<String>) -> Result {
31    Err(Self(vec![(key.into(), val.into())]))?
32  }
33
34  pub fn set(&mut self, key: impl Into<String>, val: impl Into<String>) {
35    self.0.push((key.into(), val.into()));
36  }
37}