Skip to main content

rust_web_server/null/
mod.rs

1//! [`Null`]: a value representing JSON `null`, with `FromStr` (failing with
2//! [`ParseNullError`]), `Display`, `Clone`, `Debug`, `PartialEq` and
3//! [`New`](crate::core::New).
4
5#[cfg(test)]
6mod tests;
7
8use std::fmt::{Debug, Display, Formatter};
9use std::str::FromStr;
10use crate::core::New;
11
12pub const NULL: &'static Null = &Null{};
13
14pub struct Null {}
15
16pub struct ParseNullError {
17    pub message: String
18}
19
20impl Debug for ParseNullError {
21    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22        f.write_str(&self.message)
23    }
24}
25
26impl Display for Null {
27    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
28        f.write_str("null")
29    }
30}
31
32impl PartialEq for Null {
33    fn eq(&self, _other: &Self) -> bool {
34        true
35    }
36}
37
38impl Debug for Null {
39    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
40        f.write_str("null")
41    }
42}
43
44impl New for Null {
45    fn new() -> Self {
46        Null{}
47    }
48}
49
50impl Clone for Null {
51    fn clone(&self) -> Self {
52        Null::new()
53    }
54}
55
56impl FromStr for Null {
57    type Err = ParseNullError;
58
59    fn from_str(null: &str) -> Result<Self, Self::Err> {
60        if null.trim() != "null" {
61            let message = format!("error parsing null: {}", null);
62            return Err(ParseNullError { message })
63        }
64        Ok(Null::new())
65    }
66}