kine_core/results.rs
1use crate::Time;
2
3/// The result of trying to figure out what real-world time matches a given written time
4#[derive(Clone, Debug, Eq, PartialEq)]
5pub enum TimeResult {
6 /// The written time matches exactly one real-world time
7 ///
8 /// This is the "normal path."
9 One(Time),
10
11 /// The written time matches two real-world times
12 ///
13 /// This happens relatively often, eg. when a timezone goes back in time.
14 Two(Time, Time),
15
16 /// The written time matches many real-world times
17 ///
18 /// This should happen very rarely, but can theoretically happen, eg. if a backwards
19 /// leap second happens while a timezone is going back in time then there would be three
20 /// possible real-world times matching this Time. Given how rare this should be (basically,
21 /// probably never actually happen until all programs using this crate are long forgotten),
22 /// in this scenario a single example value is returned.
23 Many(Time),
24
25 /// The written time never actually happened
26 ///
27 /// In this case, the two times returned are the one just before the time at which
28 /// this written time would have been if it existed, and the one just after.
29 DidNotExist(Time, Time),
30}
31
32impl TimeResult {
33 /// Returns any point in time that matches the given written time
34 ///
35 /// Note that if the written time did not exist, this will return an approximate version
36 /// of what it would have been if it did actually exist.
37 pub fn any_approximate(self) -> Time {
38 match self {
39 TimeResult::One(t)
40 | TimeResult::Two(t, _)
41 | TimeResult::Many(t)
42 | TimeResult::DidNotExist(t, _) => t,
43 }
44 }
45}
46
47/// A specialized Result type for `kine`.
48pub type Result<T> = core::result::Result<T, Error>;
49
50/// Represents all the ways a function can fail within `kine`.
51#[derive(Clone, Copy, Debug, Eq, PartialEq)]
52pub enum Error {
53 /// Overflowed the allowed range for the return type
54 OutOfRange,
55}
56
57// TODO: implement Error once error_in_core is stable
58// impl core::error::Error for Error {}