/home/a220/proj/radnelac/src/day_count/prelude.rs
Line | Count | Source |
1 | | // This Source Code Form is subject to the terms of the Mozilla Public |
2 | | // License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | | // file, You can obtain one at https://mozilla.org/MPL/2.0/. |
4 | | |
5 | | use crate::common::error::CalendarError; |
6 | | use crate::common::math::TermNum; |
7 | | use num_traits::AsPrimitive; |
8 | | |
9 | | pub trait EffectiveBound: Copy + Clone + PartialEq + PartialOrd { |
10 | | fn effective_min() -> Self; |
11 | | fn effective_max() -> Self; |
12 | | } |
13 | | |
14 | | pub trait BoundedDayCount<T: TermNum>: EffectiveBound { |
15 | | fn new(t: T) -> Self; |
16 | | fn get(self) -> T; |
17 | | |
18 | 2.69M | fn almost_in_effective_bounds(t: T, dt: T) -> Result<(), CalendarError> { |
19 | 2.69M | if t.is_a_number() { |
20 | 2.69M | let min = Self::effective_min().get() - dt; |
21 | 2.69M | let max = Self::effective_max().get() + dt; |
22 | 2.69M | if t >= min && t <= max2.69M { |
23 | 2.69M | Ok(()) |
24 | | } else { |
25 | 4 | Err(CalendarError::OutOfBounds) |
26 | | } |
27 | | } else { |
28 | 1 | Err(CalendarError::EncounteredNaN) |
29 | | } |
30 | 2.69M | } |
31 | | |
32 | 815k | fn in_effective_bounds(t: T) -> Result<(), CalendarError> { |
33 | 815k | Self::almost_in_effective_bounds(t, T::zero()) |
34 | 815k | } |
35 | | |
36 | 737k | fn cast_new<U: AsPrimitive<T>>(t: U) -> Self { |
37 | 737k | Self::new(t.as_()) |
38 | 737k | } |
39 | | } |