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
//! # EdgeDB Types Used for Data Modelling

mod bignum;
mod time;
mod json;
mod memory;

pub use self::time::{ LocalDatetime, LocalDate, LocalTime, Duration, Datetime };
pub use self::time::{RelativeDuration};
pub use self::bignum:: {BigInt, Decimal};
pub use self::json::Json;
pub use uuid::Uuid;
pub use memory::ConfigMemory;

use std::fmt;
use std::num::ParseIntError;

/// Error converting an out of range value to/from EdgeDB type.
#[derive(Debug, PartialEq)]
pub struct OutOfRangeError;

impl std::error::Error for OutOfRangeError {}
impl fmt::Display for OutOfRangeError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        "value is out of range".fmt(f)
    }
}

impl From<std::num::TryFromIntError> for OutOfRangeError {
    fn from(_: std::num::TryFromIntError) -> OutOfRangeError {
        OutOfRangeError
    }
}


/// Error parsing string into EdgeDB Duration type.
#[derive(Debug, PartialEq)]
pub struct ParseDurationError {
    pub(crate) message: String,
    pub(crate) pos: usize,
    pub(crate) is_final: bool,
}

impl std::error::Error for ParseDurationError {}
impl fmt::Display for ParseDurationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        format!(
            "Error parsing input at position {}: {}",
            self.pos,
            self.message,
        ).fmt(f)
    }
}

impl From<std::num::ParseIntError> for ParseDurationError {
    fn from(e: ParseIntError) -> Self {
        Self::new(format!("{}", e))
    }
}

impl ParseDurationError {
    pub(crate) fn new(message: impl Into<String>) -> Self {
        Self {
            pos: 0,
            message: message.into(),
            is_final: true,
        }
    }
    pub(crate) fn not_final(mut self) -> Self {
        self.is_final = false;
        self
    }
    pub(crate) fn pos(mut self, value: usize) -> Self {
        self.pos = value;
        self
    }
}