1use std::{
2 fmt,
3 str::FromStr,
4};
5
6#[derive(Debug, Clone, Copy, PartialEq)]
8pub enum Order {
9 Asc,
10 Desc,
11}
12
13#[derive(Debug)]
14pub struct ParseOrderError {
15 pub raw: String,
17}
18impl ParseOrderError {
19 pub fn new<S: Into<String>>(s: S) -> Self {
20 Self { raw: s.into() }
21 }
22}
23impl fmt::Display for ParseOrderError {
24 fn fmt(
25 &self,
26 f: &mut fmt::Formatter<'_>,
27 ) -> fmt::Result {
28 write!(
29 f,
30 "{:?} can't be parsed as a sort order. Use 'asc' or 'desc' (or nothing)",
31 self.raw
32 )
33 }
34}
35impl std::error::Error for ParseOrderError {}
36
37impl FromStr for Order {
38 type Err = ParseOrderError;
39 fn from_str(s: &str) -> Result<Self, ParseOrderError> {
40 let s = s.to_lowercase();
41 match s.as_ref() {
42 "a" | "asc" => Ok(Self::Asc),
43 "d" | "desc" => Ok(Self::Desc),
44 _ => Err(ParseOrderError::new(s)),
45 }
46 }
47}