1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use crate::NyarReal;
use bigdecimal::ParseBigDecimalError;
use num::{
    bigint::{ParseBigIntError, Sign},
    rational::ParseRatioError,
    BigUint,
};
#[cfg(feature = "serde")]
use serde::de::{MapAccess, Visitor};
use std::{
    error::Error,
    fmt::{Display, Formatter},
};

mod errors;

/// A non-number appears in analysis or operation.
#[derive(Debug, Clone)]
pub enum NyarNumberError {
    /// Cannot be resolved to the given type
    ParseError(String),
}

pub struct RealVisitor {
    pub r#type: String,
    pub sign: Sign,
    pub value: Option<BigUint>,
    pub numerator: Option<BigUint>,
    pub denominator: Option<BigUint>,
    pub re: Option<NyarReal>,
    pub im: Option<NyarReal>,
}

impl Default for RealVisitor {
    fn default() -> Self {
        Self { r#type: String::new(), sign: Sign::Plus, value: None, numerator: None, denominator: None, re: None, im: None }
    }
}

#[cfg(feature = "serde")]
impl<'de> Visitor<'de> for RealVisitor {
    type Value = Self;

    fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
        // never fail
        formatter.write_str("")
    }
    fn visit_map<A>(mut self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'de>,
    {
        while let Some(s) = map.next_key::<String>()? {
            match s.as_str() {
                "type" | "typing" => {
                    self.r#type = map.next_value()?;
                }
                "sign" => {
                    self.sign = map.next_value()?;
                }
                "value" => {
                    self.value = map.next_value()?;
                }
                "numerator" => {
                    self.numerator = map.next_value()?;
                }
                "denominator" => {
                    self.denominator = map.next_value()?;
                }
                "re" => {
                    self.re = map.next_value()?;
                }
                "im" => {
                    self.im = map.next_value()?;
                }
                _ => {
                    // drop
                }
            }
        }
        Ok(self)
    }
}