| 
 | 1 | +use std::num::{IntErrorKind, TryFromIntError};  | 
 | 2 | +use std::time::Duration;  | 
 | 3 | + | 
 | 4 | +pub trait IncrementCounter: Sized + Default {  | 
 | 5 | +    type Increment;  | 
 | 6 | + | 
 | 7 | +    type IncrementCount;  | 
 | 8 | +    type IncrementProduct;  | 
 | 9 | + | 
 | 10 | +    fn new(size: &Self::Increment, quantity: &Self::IncrementCount) -> Self;  | 
 | 11 | + | 
 | 12 | +    fn add(&self, add: Self::IncrementCount) -> Result<Self, IntErrorKind>;  | 
 | 13 | +    fn sub(&self, sub: Self::IncrementCount) -> Result<Self, IntErrorKind>;  | 
 | 14 | + | 
 | 15 | +    fn product_current(&self) -> Result<Option<Self::IncrementProduct>, TryFromIntError>;  | 
 | 16 | +    fn product_next(&self) -> Result<Option<Self::IncrementProduct>, TryFromIntError>;  | 
 | 17 | +}  | 
 | 18 | + | 
 | 19 | +pub type ExtentUnit = Duration;  | 
 | 20 | +pub type ExtentUnitSeconds = u64;  | 
 | 21 | +pub type ExtentCount = u64;  | 
 | 22 | +pub type ExtentProduct = ExtentUnit;  | 
 | 23 | + | 
 | 24 | +#[derive(Debug, Default, Hash, PartialEq, Eq)]  | 
 | 25 | +pub struct Extent {  | 
 | 26 | +    pub size: ExtentUnit,  | 
 | 27 | +    pub quantity: ExtentCount,  | 
 | 28 | +}  | 
 | 29 | + | 
 | 30 | +impl Extent {  | 
 | 31 | +    pub const fn from_sec(size: ExtentUnitSeconds, quantity: &ExtentCount) -> Self {  | 
 | 32 | +        Self {  | 
 | 33 | +            size: ExtentUnit::from_secs(size),  | 
 | 34 | +            quantity: *quantity,  | 
 | 35 | +        }  | 
 | 36 | +    }  | 
 | 37 | +}  | 
 | 38 | + | 
 | 39 | +impl IncrementCounter for Extent {  | 
 | 40 | +    type Increment = ExtentUnit;  | 
 | 41 | +    type IncrementCount = ExtentCount;  | 
 | 42 | +    type IncrementProduct = ExtentProduct;  | 
 | 43 | + | 
 | 44 | +    fn new(size: &Self::Increment, quantity: &Self::IncrementCount) -> Self {  | 
 | 45 | +        Self {  | 
 | 46 | +            size: *size,  | 
 | 47 | +            quantity: *quantity,  | 
 | 48 | +        }  | 
 | 49 | +    }  | 
 | 50 | + | 
 | 51 | +    fn add(&self, add: Self::IncrementCount) -> Result<Self, IntErrorKind> {  | 
 | 52 | +        match self.quantity.checked_add(add) {  | 
 | 53 | +            None => Err(IntErrorKind::PosOverflow),  | 
 | 54 | +            Some(quantity) => Ok(Self {  | 
 | 55 | +                size: self.size,  | 
 | 56 | +                quantity,  | 
 | 57 | +            }),  | 
 | 58 | +        }  | 
 | 59 | +    }  | 
 | 60 | + | 
 | 61 | +    fn sub(&self, sub: Self::IncrementCount) -> Result<Self, IntErrorKind> {  | 
 | 62 | +        match self.quantity.checked_sub(sub) {  | 
 | 63 | +            None => Err(IntErrorKind::NegOverflow),  | 
 | 64 | +            Some(quantity) => Ok(Self {  | 
 | 65 | +                size: self.size,  | 
 | 66 | +                quantity,  | 
 | 67 | +            }),  | 
 | 68 | +        }  | 
 | 69 | +    }  | 
 | 70 | + | 
 | 71 | +    fn product_current(&self) -> Result<Option<Self::IncrementProduct>, TryFromIntError> {  | 
 | 72 | +        match u32::try_from(self.quantity) {  | 
 | 73 | +            Err(error) => Err(error),  | 
 | 74 | +            Ok(quantity) => Ok(self.size.checked_mul(quantity)),  | 
 | 75 | +        }  | 
 | 76 | +    }  | 
 | 77 | + | 
 | 78 | +    fn product_next(&self) -> Result<Option<Self::IncrementProduct>, TryFromIntError> {  | 
 | 79 | +        match u32::try_from(self.quantity) {  | 
 | 80 | +            Err(e) => Err(e),  | 
 | 81 | +            Ok(quantity) => match quantity.checked_add(1) {  | 
 | 82 | +                None => Ok(None),  | 
 | 83 | +                Some(quantity) => match self.size.checked_mul(quantity) {  | 
 | 84 | +                    None => Ok(None),  | 
 | 85 | +                    Some(extent) => Ok(Some(extent)),  | 
 | 86 | +                },  | 
 | 87 | +            },  | 
 | 88 | +        }  | 
 | 89 | +    }  | 
 | 90 | +}  | 
 | 91 | + | 
 | 92 | +#[cfg(test)]  | 
 | 93 | +mod test {  | 
 | 94 | + | 
 | 95 | +    use std::time::Duration;  | 
 | 96 | + | 
 | 97 | +    use crate::protocol::clock::extent::{Extent, IncrementCounter};  | 
 | 98 | + | 
 | 99 | +    #[test]  | 
 | 100 | +    fn it_should_get_the_total_time_of_a_period() {  | 
 | 101 | +        assert_eq!(Extent::default().product_current().unwrap().unwrap(), Duration::ZERO);  | 
 | 102 | + | 
 | 103 | +        assert_eq!(  | 
 | 104 | +            Extent::from_sec(12, &12).product_current().unwrap().unwrap(),  | 
 | 105 | +            Duration::from_secs(144)  | 
 | 106 | +        );  | 
 | 107 | +        assert_eq!(  | 
 | 108 | +            Extent::from_sec(12, &12).product_next().unwrap().unwrap(),  | 
 | 109 | +            Duration::from_secs(156)  | 
 | 110 | +        );  | 
 | 111 | +    }  | 
 | 112 | +}  | 
0 commit comments