Skip to content

Commit 6453140

Browse files
committed
Add doc comments to Mdf methods
1 parent a41e4c0 commit 6453140

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/naive/internals.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ impl fmt::Debug for YearFlags {
130130
const MAX_OL: u32 = 366 << 1; // `(366 << 1) | 1` would be day 366 in a non-leap year
131131
const MAX_MDL: u32 = (12 << 6) | (31 << 1) | 1;
132132

133+
// The next table are adjustment values to convert a date encoded as month-day-leapyear to
134+
// ordinal-leapyear. OL = MDL - adjustment.
135+
// Dates that do not exist are encoded as `XX`.
133136
const XX: i8 = 0;
134137
const MDL_TO_OL: &[i8; MAX_MDL as usize + 1] = &[
135138
XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
@@ -229,6 +232,14 @@ const OL_TO_MDL: &[u8; MAX_OL as usize + 1] = &[
229232
pub(super) struct Mdf(u32);
230233

231234
impl Mdf {
235+
/// Makes a new `Mdf` value from month, day and `YearFlags`.
236+
///
237+
/// This method doesn't fully validate the range of the `month` and `day` parameters, only as
238+
/// much as what can't be deferred until later. The year `flags` are trusted to be correct.
239+
///
240+
/// # Errors
241+
///
242+
/// Returns `None` if `month > 12` or `day > 31`.
232243
#[inline]
233244
pub(super) const fn new(month: u32, day: u32, YearFlags(flags): YearFlags) -> Option<Mdf> {
234245
match month <= 12 && day <= 31 {
@@ -237,19 +248,29 @@ impl Mdf {
237248
}
238249
}
239250

251+
/// Makes a new `Mdf` value from an `i32` with an ordinal and a leap year flag, and year
252+
/// `flags`.
253+
///
254+
/// The `ol` is trusted to be valid, and the `flags` are trusted to match it.
240255
#[inline]
241256
pub(super) const fn from_ol(ol: i32, YearFlags(flags): YearFlags) -> Mdf {
242257
debug_assert!(ol > 1 && ol <= MAX_OL as i32);
243258
// Array is indexed from `[2..=MAX_OL]`, with a `0` index having a meaningless value.
244259
Mdf(((ol as u32 + OL_TO_MDL[ol as usize] as u32) << 3) | flags as u32)
245260
}
246261

262+
/// Returns the month of this `Mdf`.
247263
#[inline]
248264
pub(super) const fn month(&self) -> u32 {
249265
let Mdf(mdf) = *self;
250266
mdf >> 9
251267
}
252268

269+
/// Replaces the month of this `Mdf`, keeping the day and flags.
270+
///
271+
/// # Errors
272+
///
273+
/// Returns `None` if `month > 12`.
253274
#[inline]
254275
pub(super) const fn with_month(&self, month: u32) -> Option<Mdf> {
255276
if month > 12 {
@@ -260,12 +281,18 @@ impl Mdf {
260281
Some(Mdf((mdf & 0b1_1111_1111) | (month << 9)))
261282
}
262283

284+
/// Returns the day of this `Mdf`.
263285
#[inline]
264286
pub(super) const fn day(&self) -> u32 {
265287
let Mdf(mdf) = *self;
266288
(mdf >> 4) & 0b1_1111
267289
}
268290

291+
/// Replaces the day of this `Mdf`, keeping the month and flags.
292+
///
293+
/// # Errors
294+
///
295+
/// Returns `None` if `day > 31`.
269296
#[inline]
270297
pub(super) const fn with_day(&self, day: u32) -> Option<Mdf> {
271298
if day > 31 {
@@ -276,12 +303,22 @@ impl Mdf {
276303
Some(Mdf((mdf & !0b1_1111_0000) | (day << 4)))
277304
}
278305

306+
/// Replaces the flags of this `Mdf`, keeping the month and day.
279307
#[inline]
280308
pub(super) const fn with_flags(&self, YearFlags(flags): YearFlags) -> Mdf {
281309
let Mdf(mdf) = *self;
282310
Mdf((mdf & !0b1111) | flags as u32)
283311
}
284312

313+
/// Returns the ordinal that corresponds to this `Mdf`.
314+
///
315+
/// This does a table lookup to calculate the corresponding ordinal. It will return an error if
316+
/// the `Mdl` turns out not to be a valid date.
317+
///
318+
/// # Errors
319+
///
320+
/// Returns `None` if `month == 0` or `day == 0`, or if a the given day does not exist in the
321+
/// given month.
285322
#[inline]
286323
pub(super) const fn ordinal(&self) -> Option<u32> {
287324
let mdl = self.0 >> 3;
@@ -291,11 +328,21 @@ impl Mdf {
291328
}
292329
}
293330

331+
/// Returns the year flags of this `Mdf`.
294332
#[inline]
295333
pub(super) const fn year_flags(&self) -> YearFlags {
296334
YearFlags((self.0 & 0b1111) as u8)
297335
}
298336

337+
/// Returns the ordinal that corresponds to this `Mdf`, encoded as a value including year flags.
338+
///
339+
/// This does a table lookup to calculate the corresponding ordinal. It will return an error if
340+
/// the `Mdl` turns out not to be a valid date.
341+
///
342+
/// # Errors
343+
///
344+
/// Returns `None` if `month == 0` or `day == 0`, or if a the given day does not exist in the
345+
/// given month.
299346
#[inline]
300347
pub(super) const fn ordinal_and_flags(&self) -> Option<i32> {
301348
let mdl = self.0 >> 3;

0 commit comments

Comments
 (0)