nyar_number/utils/
mod.rs

1use crate::NyarReal;
2use num::{bigint::Sign, BigUint};
3#[cfg(feature = "serde")]
4use serde::de::{MapAccess, Visitor};
5#[cfg(feature = "serde")]
6use std::fmt::Formatter;
7
8/// A non-number appears in analysis or operation.
9pub struct RealVisitor {
10    pub r#type: String,
11    pub sign: Sign,
12    pub digits: Vec<u64>,
13    pub numerator: Option<BigUint>,
14    pub denominator: Option<BigUint>,
15    pub re: Option<NyarReal>,
16    pub im: Option<NyarReal>,
17}
18
19impl Default for RealVisitor {
20    fn default() -> Self {
21        Self { r#type: String::new(), sign: Sign::Plus, digits: vec![], numerator: None, denominator: None, re: None, im: None }
22    }
23}
24
25#[cfg(feature = "serde")]
26impl<'de> Visitor<'de> for RealVisitor {
27    type Value = Self;
28
29    fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
30        // never fail
31        formatter.write_str("")
32    }
33
34    fn visit_map<A>(mut self, mut map: A) -> Result<Self::Value, A::Error>
35    where
36        A: MapAccess<'de>,
37    {
38        while let Some(s) = map.next_key::<String>()? {
39            match s.as_str() {
40                "type" | "typing" => {
41                    self.r#type = map.next_value()?;
42                }
43                "sign" => {
44                    self.sign = map.next_value()?;
45                }
46                "value" => {
47                    self.digits = map.next_value()?;
48                }
49                "numerator" => {
50                    self.numerator = map.next_value()?;
51                }
52                "denominator" => {
53                    self.denominator = map.next_value()?;
54                }
55                "re" => {
56                    self.re = map.next_value()?;
57                }
58                "im" => {
59                    self.im = map.next_value()?;
60                }
61                _ => {
62
63                    // drop
64                }
65            }
66        }
67        Ok(self)
68    }
69}