Skip to content

Display implementation omits the 1 ordinal for NWeekday::Nth #148

Description

@taasan

According to the current Display implementation and its doc test:

assert_eq!(format!("{}", NWeekday::Every(Weekday::Mon)), "MO");
assert_eq!(format!("{}", NWeekday::Nth(1, Weekday::Mon)), "MO");
assert_eq!(format!("{}", NWeekday::Nth(2, Weekday::Mon)), "2MO");

impl Display for NWeekday {
/// Returns a string representation of the [`NWeekday`]
///
/// ```
/// use chrono::Weekday;
/// use rrule::NWeekday;
///
/// assert_eq!(format!("{}", NWeekday::Every(Weekday::Mon)), "MO");
/// assert_eq!(format!("{}", NWeekday::Nth(1, Weekday::Mon)), "MO");
/// assert_eq!(format!("{}", NWeekday::Nth(2, Weekday::Mon)), "2MO");
/// ```
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let weekday = match self {
Self::Every(wd) => weekday_to_str(*wd),
Self::Nth(number, wd) => {
let mut wd_str = weekday_to_str(*wd);
if *number != 1 {
wd_str = format!("{}{}", number, wd_str);
};
wd_str
}
};
write!(f, "{}", weekday)
}
}

Every(Mon) and Nth(1, Mon) both serialize to MO.

However, MO and 1MO are not equivalent. MO matches every Monday, while 1MO matches only the first Monday selected by the recurrence rule.

As a result, Nth(1, ...) cannot be represented correctly.

The implementation should always include the ordinal for Nth, including 1. The doc test should likewise expect:

assert_eq!(format!("{}", NWeekday::Nth(1, Weekday::Mon)), "1MO");

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions