gel_protocol/
model.rs

1//! # Gel Types Used for Data Modelling
2
3mod bignum;
4mod json;
5mod memory;
6mod time;
7mod vector;
8
9pub(crate) mod range;
10
11pub use self::bignum::{BigInt, Decimal};
12pub use self::json::Json;
13pub use self::time::{DateDuration, RelativeDuration};
14pub use self::time::{Datetime, Duration, LocalDate, LocalDatetime, LocalTime};
15pub use memory::ConfigMemory;
16pub use range::Range;
17pub use uuid::Uuid;
18pub use vector::Vector;
19pub(crate) use vector::VectorRef;
20
21use std::fmt;
22use std::num::ParseIntError;
23
24/// Error converting an out of range value to/from Gel type.
25#[derive(Debug, PartialEq)]
26pub struct OutOfRangeError;
27
28impl std::error::Error for OutOfRangeError {}
29impl fmt::Display for OutOfRangeError {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        "value is out of range".fmt(f)
32    }
33}
34
35impl From<std::num::TryFromIntError> for OutOfRangeError {
36    fn from(_: std::num::TryFromIntError) -> OutOfRangeError {
37        OutOfRangeError
38    }
39}
40
41/// Error parsing string into Gel Duration type.
42#[derive(Debug, PartialEq)]
43pub struct ParseDurationError {
44    pub(crate) message: String,
45    pub(crate) pos: usize,
46    pub(crate) is_final: bool,
47}
48
49impl std::error::Error for ParseDurationError {}
50impl fmt::Display for ParseDurationError {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        format!(
53            "Error parsing input at position {}: {}",
54            self.pos, self.message,
55        )
56        .fmt(f)
57    }
58}
59
60impl From<std::num::ParseIntError> for ParseDurationError {
61    fn from(e: ParseIntError) -> Self {
62        Self::new(format!("{e}"))
63    }
64}
65
66impl ParseDurationError {
67    pub(crate) fn new(message: impl Into<String>) -> Self {
68        Self {
69            pos: 0,
70            message: message.into(),
71            is_final: true,
72        }
73    }
74    pub(crate) fn not_final(mut self) -> Self {
75        self.is_final = false;
76        self
77    }
78    pub(crate) fn pos(mut self, value: usize) -> Self {
79        self.pos = value;
80        self
81    }
82}