@@ -176,7 +176,7 @@ impl TryFrom<u8> for Month {
176176}
177177
178178/// A duration in calendar months
179- #[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq , PartialOrd ) ]
179+ #[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq , PartialOrd , Ord ) ]
180180pub struct Months ( pub ( crate ) u32 ) ;
181181
182182impl Months {
@@ -186,6 +186,66 @@ impl Months {
186186 }
187187}
188188
189+ /// A difference in a number of months, either forwards or backwards.
190+ ///
191+ /// This type is often returned from fuctions but is generally not used as a parameter.
192+ /// Instead the inner `Months` must first be extracted and then used. There are helper methods
193+ /// which can assist with this.
194+ #[ derive( Clone , Copy , Debug , PartialOrd , Ord ) ]
195+ pub enum MonthsDelta {
196+ /// the forwards direction
197+ Forwards ( Months ) ,
198+ /// the backwards direction
199+ Backwards ( Months ) ,
200+ }
201+
202+ impl MonthsDelta {
203+ /// Assert that the direction is forwards and throw away the `Months` otherwise.
204+ pub fn forwards ( self ) -> Option < Months > {
205+ match self {
206+ MonthsDelta :: Forwards ( d) => Some ( d) ,
207+ MonthsDelta :: Backwards ( _) => None ,
208+ }
209+ }
210+
211+ /// Assert that the direction is backwards and throw away the `Months` otherwise.
212+ pub fn backwards ( self ) -> Option < Months > {
213+ match self {
214+ MonthsDelta :: Forwards ( _) => None ,
215+ MonthsDelta :: Backwards ( d) => Some ( d) ,
216+ }
217+ }
218+
219+ /// Get the contained `Months`, no matter which direction
220+ pub fn abs ( self ) -> Months {
221+ match self {
222+ MonthsDelta :: Backwards ( d) => d,
223+ MonthsDelta :: Forwards ( d) => d,
224+ }
225+ }
226+ }
227+
228+ impl PartialEq for MonthsDelta {
229+ fn eq ( & self , other : & MonthsDelta ) -> bool {
230+ match ( self , other) {
231+ ( MonthsDelta :: Forwards ( f1) , MonthsDelta :: Forwards ( f2) ) => f1 == f2,
232+ ( MonthsDelta :: Backwards ( b1) , MonthsDelta :: Backwards ( b2) ) => b1 == b2,
233+ ( MonthsDelta :: Forwards ( lhs) , MonthsDelta :: Backwards ( rhs) )
234+ | ( MonthsDelta :: Backwards ( lhs) , MonthsDelta :: Forwards ( rhs) ) => {
235+ * lhs == Months ( 0 ) && * rhs == Months ( 0 )
236+ }
237+ }
238+ }
239+ }
240+
241+ impl Eq for MonthsDelta { }
242+
243+ impl From < Months > for MonthsDelta {
244+ fn from ( s : Months ) -> Self {
245+ MonthsDelta :: Forwards ( s)
246+ }
247+ }
248+
189249/// An error resulting from reading `<Month>` value with `FromStr`.
190250#[ derive( Clone , PartialEq , Eq ) ]
191251pub struct ParseMonthError {
0 commit comments