1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
#![doc = include_str!("../README.md")]
use std::{fmt, io};
use chrono::{Datelike, NaiveDate};
/// A compact representation of included days in a range of years, using u32-based bit arrays.
#[derive(Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct CompactCalendar {
first_year: i32,
calendar: Vec<CompactYear>,
}
impl CompactCalendar {
/// Create a new year that does not include any day but has the capacity to add any new day in
/// the range of `[first_year, last_year]`.
///
/// ```
/// use compact_calendar::CompactCalendar;
/// use chrono::NaiveDate;
///
/// let day1 = NaiveDate::from_ymd_opt(2013, 11, 3).unwrap();
/// let day2 = NaiveDate::from_ymd_opt(2022, 3, 5).unwrap();
/// let day3 = NaiveDate::from_ymd_opt(2055, 7, 5).unwrap();
///
/// let mut cal = CompactCalendar::new(2020, 2050);
/// assert!(!cal.insert(day1));
/// assert!(cal.insert(day2));
/// assert!(!cal.insert(day3));
/// assert_eq!(cal.count(), 1);
/// ```
pub fn new(first_year: i32, last_year: i32) -> Self {
assert!(
first_year <= last_year,
"use CompactCalendar::empty() if you need to init a calendar with no capacity",
);
let length: usize = (last_year - first_year + 1)
.try_into()
.expect("compact calendar is too large to be initialized");
Self {
first_year,
calendar: vec![CompactYear::new(); length],
}
}
/// Create a new calendar year that does not include any day and does not have the capacity to
/// add any.
///
/// ```
/// use compact_calendar::CompactCalendar;
/// use chrono::NaiveDate;
///
/// let mut cal = CompactCalendar::empty();
/// assert!(!cal.insert(NaiveDate::from_ymd_opt(2013, 11, 3).unwrap()));
/// assert_eq!(cal.count(), 0);
/// ```
pub const fn empty() -> Self {
Self { first_year: 0, calendar: Vec::new() }
}
/// Get a reference to the year containing give date.
///
/// ```
/// use compact_calendar::CompactCalendar;
/// use chrono::NaiveDate;
///
/// let day1 = NaiveDate::from_ymd_opt(2013, 11, 3).unwrap();
/// let day2 = NaiveDate::from_ymd_opt(2055, 3, 5).unwrap();
///
/// let mut cal = CompactCalendar::new(2000, 2050);
/// cal.insert(day1);
///
/// assert!(cal.year_for(day1).unwrap().contains(11, 3));
/// assert!(cal.year_for(day2).is_none());
/// ```
pub fn year_for(&self, date: NaiveDate) -> Option<&CompactYear> {
let year0 = usize::try_from(date.year() - self.first_year).ok()?;
self.calendar.get(year0)
}
/// Get a mutable reference to the year containing give date.
///
/// ```
/// use compact_calendar::CompactCalendar;
/// use chrono::NaiveDate;
///
/// let day1 = NaiveDate::from_ymd_opt(2013, 11, 3).unwrap();
/// let day2 = NaiveDate::from_ymd_opt(2022, 3, 5).unwrap();
/// let day3 = NaiveDate::from_ymd_opt(2055, 7, 5).unwrap();
///
/// let mut cal = CompactCalendar::new(2000, 2050);
/// assert!(cal.year_for_mut(day3).is_none());
///
/// cal.insert(day1);
/// assert!(cal.year_for_mut(day1).unwrap().contains(11, 3));
///
/// cal.year_for_mut(day2).unwrap().insert(3, 5);
/// assert!(cal.contains(day2));
/// ```
pub fn year_for_mut(&mut self, date: NaiveDate) -> Option<&mut CompactYear> {
let year0 = usize::try_from(date.year() - self.first_year).ok()?;
self.calendar.get_mut(year0)
}
/// Include a day in this calendar. Return false if the given day was not inserted because it
/// does not belong to the year range of this calendar.
///
/// ```
/// use compact_calendar::CompactCalendar;
/// use chrono::NaiveDate;
///
/// let day1 = NaiveDate::from_ymd_opt(2013, 11, 3).unwrap();
/// let day2 = NaiveDate::from_ymd_opt(2022, 3, 5).unwrap();
/// let day3 = NaiveDate::from_ymd_opt(2055, 9, 7).unwrap();
///
/// let mut cal = CompactCalendar::new(2000, 2050);
/// assert!(cal.insert(day1));
/// assert!(cal.insert(day1));
/// assert!(cal.insert(day2));
/// assert_eq!(cal.count(), 2);
///
/// assert!(!cal.insert(day3));
/// assert_eq!(cal.count(), 2);
/// ```
pub fn insert(&mut self, date: NaiveDate) -> bool {
if let Some(year) = self.year_for_mut(date) {
year.insert(date.month(), date.day());
true
} else {
false
}
}
/// Check if this calendar includes the given day.
///
/// ```
/// use compact_calendar::CompactCalendar;
/// use chrono::NaiveDate;
///
/// let day1 = NaiveDate::from_ymd_opt(2013, 11, 3).unwrap();
/// let day2 = NaiveDate::from_ymd_opt(2022, 3, 5).unwrap();
/// let day3 = NaiveDate::from_ymd_opt(2022, 8, 12).unwrap();
///
/// let mut cal = CompactCalendar::new(2000, 2050);
/// cal.insert(day1);
/// cal.insert(day2);
///
/// assert!(cal.contains(day1));
/// assert!(cal.contains(day2));
/// assert!(!cal.contains(day3));
/// ```
pub fn contains(&self, date: NaiveDate) -> bool {
if let Some(year) = self.year_for(date) {
year.contains(date.month(), date.day())
} else {
false
}
}
/// Iterate over the days included in this calendar.
///
/// ```
/// use compact_calendar::CompactCalendar;
/// use chrono::NaiveDate;
///
/// let day1 = NaiveDate::from_ymd_opt(2013, 11, 3).unwrap();
/// let day2 = NaiveDate::from_ymd_opt(2022, 3, 5).unwrap();
/// let day3 = NaiveDate::from_ymd_opt(2022, 8, 12).unwrap();
///
/// let mut cal = CompactCalendar::new(2000, 2050);
/// cal.insert(day3);
/// cal.insert(day1);
/// cal.insert(day2);
///
/// let days: Vec<_> = cal.iter().collect();
/// assert_eq!(days, [day1, day2, day3])
/// ```
pub fn iter(&self) -> impl Iterator<Item = NaiveDate> + '_ {
(self.first_year..)
.zip(self.calendar.iter())
.flat_map(|(year_i, year)| {
year.iter().map(move |(month, day)| {
NaiveDate::from_ymd_opt(year_i, month, day)
.expect("invalid date loaded from calendar")
})
})
}
/// Get the first day included in this calendar that follows the input day, if such a day
/// exists.
///
/// ```
/// use compact_calendar::CompactCalendar;
/// use chrono::NaiveDate;
///
/// let day0 = NaiveDate::from_ymd_opt(2010, 1, 1).unwrap();
/// let day1 = NaiveDate::from_ymd_opt(2013, 11, 3).unwrap();
/// let day2 = NaiveDate::from_ymd_opt(2022, 3, 5).unwrap();
/// let day3 = NaiveDate::from_ymd_opt(2022, 8, 12).unwrap();
///
/// let mut cal = CompactCalendar::new(2000, 2050);
/// cal.insert(day1);
/// cal.insert(day2);
/// cal.insert(day3);
///
/// assert_eq!(cal.first_after(day0), Some(day1));
/// assert_eq!(cal.first_after(day2), Some(day3));
/// assert_eq!(cal.first_after(day3), None);
/// ```
pub fn first_after(&self, date: NaiveDate) -> Option<NaiveDate> {
if let Some(year) = self.year_for(date) {
let from_first_year = year
.first_after(date.month(), date.day())
.map(|(month, day)| {
NaiveDate::from_ymd_opt(date.year(), month, day)
.expect("invalid date loaded from calendar")
});
from_first_year.or_else(|| {
let year0 = usize::try_from(date.year() - self.first_year).ok()?;
(date.year() + 1..)
.zip(&self.calendar[year0 + 1..])
.find_map(|(year_i, year)| {
let (month, day) = year.first()?;
Some(
NaiveDate::from_ymd_opt(year_i, month, day)
.expect("invalid date loaded from calendar"),
)
})
})
} else {
None
}
}
/// Count number of days included for this calendar.
///
/// ```
/// use compact_calendar::CompactCalendar;
/// use chrono::NaiveDate;
///
/// let mut cal = CompactCalendar::new(2000, 2050);
/// cal.insert(NaiveDate::from_ymd(2022, 8, 12));
/// cal.insert(NaiveDate::from_ymd(2022, 3, 5));
/// assert_eq!(cal.count(), 2);
/// ```
pub fn count(&self) -> u32 {
self.calendar.iter().map(CompactYear::count).sum()
}
/// Serialize this calendar into a writer.
///
/// ```
/// use compact_calendar::CompactCalendar;
/// use chrono::NaiveDate;
///
/// let mut cal = CompactCalendar::new(2000, 2050);
///
/// let mut buf1 = Vec::new();
/// cal.insert(NaiveDate::from_ymd(2022, 8, 12));
/// cal.serialize(&mut buf1).unwrap();
///
/// let mut buf2 = Vec::new();
/// cal.insert(NaiveDate::from_ymd(2022, 3, 5));
/// cal.serialize(&mut buf2).unwrap();
///
/// assert_ne!(buf1, buf2);
/// ```
pub fn serialize(&self, mut writer: impl io::Write) -> io::Result<()> {
writer.write_all(&self.first_year.to_ne_bytes())?;
writer.write_all(&self.calendar.len().to_ne_bytes())?;
for year in &self.calendar {
year.serialize(&mut writer)?;
}
Ok(())
}
/// Deserialize a calendar from a reader.
///
/// ```
/// use compact_calendar::CompactCalendar;
/// use chrono::NaiveDate;
///
/// let mut cal1 = CompactCalendar::new(2000, 2050);
/// cal1.insert(NaiveDate::from_ymd(2022, 8, 12));
/// cal1.insert(NaiveDate::from_ymd(2022, 3, 5));
///
/// let mut buf = Vec::new();
/// cal1.serialize(&mut buf).unwrap();
///
/// let cal2 = CompactCalendar::deserialize(buf.as_slice()).unwrap();
/// assert_eq!(cal1, cal2);
/// ```
pub fn deserialize(mut reader: impl io::Read) -> io::Result<Self> {
let first_year = {
let mut buf = [0; std::mem::size_of::<i32>()];
reader.read_exact(&mut buf)?;
i32::from_ne_bytes(buf)
};
let length = {
let mut buf = [0; std::mem::size_of::<usize>()];
reader.read_exact(&mut buf)?;
usize::from_ne_bytes(buf)
};
let calendar = (0..length)
.map(|_| CompactYear::deserialize(&mut reader))
.collect::<Result<_, _>>()?;
Ok(Self { first_year, calendar })
}
}
impl fmt::Debug for CompactCalendar {
/// ```
/// use compact_calendar::CompactCalendar;
/// use chrono::NaiveDate;
///
/// let mut cal = CompactCalendar::new(2000, 2050);
/// cal.insert(NaiveDate::from_ymd(2022, 8, 12));
/// cal.insert(NaiveDate::from_ymd(2022, 3, 5));
///
/// assert_eq!(
/// format!("{cal:?}"),
/// "CompactCalendar { first_year: 2000, last_year: 2050, calendar: {2022-03-05, 2022-08-12} }",
/// );
/// ```
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
struct DebugCalendar<'a>(&'a CompactCalendar);
impl<'a> fmt::Debug for DebugCalendar<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_set().entries(self.0.iter()).finish()
}
}
let last_year = self.first_year + self.calendar.len() as i32 - 1;
f.debug_struct("CompactCalendar")
.field("first_year", &self.first_year)
.field("last_year", &last_year)
.field("calendar", &DebugCalendar(self))
.finish()
}
}
/// A compact representation of included days for a year, using a collection of u32-based bit
/// array.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct CompactYear([CompactMonth; 12]);
impl CompactYear {
/// Create a new year that does not include any day.
///
/// ```
/// use compact_calendar::CompactYear;
///
/// let year = CompactYear::new();
/// assert_eq!(year.count(), 0);
/// ```
pub const fn new() -> Self {
Self([CompactMonth::new(); 12])
}
/// Include a day in this year.
///
/// ```
/// use compact_calendar::CompactYear;
///
/// let mut year = CompactYear::new();
/// year.insert(11, 3);
/// year.insert(11, 3);
/// year.insert(1, 25);
/// assert_eq!(year.count(), 2);
/// ```
pub fn insert(&mut self, month: u32, day: u32) {
assert!((1..=12).contains(&month));
assert!((1..=31).contains(&day));
self.0[(month - 1) as usize].insert(day)
}
/// Check if this year includes the given day.
///
/// ```
/// use compact_calendar::CompactYear;
///
/// let mut year = CompactYear::new();
/// year.insert(3, 1);
/// year.insert(9, 5);
///
/// assert!(year.contains(3, 1));
/// assert!(year.contains(9, 5));
/// assert!(!year.contains(7, 14));
/// ```
pub fn contains(&self, month: u32, day: u32) -> bool {
assert!((1..=12).contains(&month));
assert!((1..=31).contains(&day));
self.0[(month - 1) as usize].contains(day)
}
/// Iterate over the days included in this year.
///
/// ```
/// use compact_calendar::CompactYear;
///
/// let mut year = CompactYear::new();
/// year.insert(9, 5);
/// year.insert(3, 1);
///
/// let days: Vec<_> = year.iter().collect();
/// assert_eq!(days, [(3, 1), (9, 5)])
/// ```
pub fn iter(&self) -> impl Iterator<Item = (u32, u32)> + '_ {
((1..=12).zip(&self.0))
.flat_map(|(month_i, month)| month.iter().map(move |day| (month_i, day)))
}
/// Get the first day included in this year if it is not empty.
///
/// ```
/// use compact_calendar::CompactYear;
///
/// let mut year = CompactYear::new();
/// assert_eq!(year.first(), None);
///
/// year.insert(12, 31);
/// assert_eq!(year.first(), Some((12, 31)));
///
/// year.insert(5, 8);
/// assert_eq!(year.first(), Some((5, 8)));
/// ```
pub fn first(&self) -> Option<(u32, u32)> {
self.0.iter().enumerate().find_map(|(i, month)| {
let res_month = (i + 1) as u32;
Some((res_month, month.first()?))
})
}
/// Get the first day included in this year that follows the input day, if such a day exists.
///
/// ```
/// use compact_calendar::CompactYear;
///
/// let mut year = CompactYear::new();
/// year.insert(3, 15);
/// year.insert(10, 9);
/// year.insert(2, 7);
///
/// assert_eq!(year.first_after(2, 2), Some((2, 7)));
/// assert_eq!(year.first_after(2, 7), Some((3, 15)));
/// assert_eq!(year.first_after(11, 1), None);
/// ```
pub fn first_after(&self, month: u32, day: u32) -> Option<(u32, u32)> {
assert!((1..=12).contains(&month));
assert!((1..=31).contains(&day));
let month0: usize = (month - 1) as usize;
if let Some(res) = self.0[month0].first_after(day) {
Some((month, res))
} else {
self.0[month0 + 1..]
.iter()
.enumerate()
.find_map(|(i, month)| {
let res_month = (i + month0 + 2) as u32;
Some((res_month, month.first()?))
})
}
}
/// Count number of days included for this year.
///
/// ```
/// use compact_calendar::CompactYear;
///
/// let mut year = CompactYear::new();
/// year.insert(11, 3);
/// year.insert(4, 28);
/// assert_eq!(year.count(), 2);
/// ```
pub fn count(&self) -> u32 {
self.0.iter().copied().map(CompactMonth::count).sum()
}
/// Serialize this year into a writer.
///
/// ```
/// use compact_calendar::CompactYear;
///
/// let mut year = CompactYear::new();
///
/// let mut buf1 = Vec::new();
/// year.insert(11, 3);
/// year.serialize(&mut buf1).unwrap();
///
/// let mut buf2 = Vec::new();
/// year.insert(4, 28);
/// year.serialize(&mut buf2).unwrap();
///
/// assert_ne!(buf1, buf2);
/// ```
pub fn serialize(&self, mut writer: impl io::Write) -> io::Result<()> {
for month in self.0 {
month.serialize(&mut writer)?;
}
Ok(())
}
/// Deserialize a year from a reader.
///
/// ```
/// use compact_calendar::CompactYear;
///
/// let mut year1 = CompactYear::new();
/// year1.insert(11, 3);
/// year1.insert(4, 28);
///
/// let mut buf = Vec::new();
/// year1.serialize(&mut buf).unwrap();
///
/// let year2 = CompactYear::deserialize(buf.as_slice()).unwrap();
/// assert_eq!(year1, year2);
/// ```
pub fn deserialize(mut reader: impl io::Read) -> io::Result<Self> {
// NOTE: could use `try_from_fn` when stabilized:
// https://doc.rust-lang.org/std/array/fn.try_from_fn.html
let mut res = Self::new();
for month in &mut res.0 {
*month = CompactMonth::deserialize(&mut reader)?;
}
Ok(res)
}
}
impl fmt::Debug for CompactYear {
/// ```
/// use compact_calendar::CompactYear;
///
/// let mut year = CompactYear::new();
/// year.insert(11, 3);
/// year.insert(4, 28);
/// assert_eq!(format!("{year:?}"), "{04-28, 11-03}");
/// ```
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
struct DebugMonthDay {
month: u32,
day: u32,
}
impl fmt::Debug for DebugMonthDay {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:02}-{:02}", self.month, self.day)
}
}
f.debug_set()
.entries(self.iter().map(|(month, day)| DebugMonthDay { month, day }))
.finish()
}
}
/// A compact representation of included days for a month, using a u32-based bit array.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct CompactMonth(u32);
impl CompactMonth {
/// Create a new month that does not include any day.
///
/// ```
/// use compact_calendar::CompactMonth;
///
/// let month = CompactMonth::new();
/// assert_eq!(month.count(), 0);
/// ```
pub const fn new() -> Self {
Self(0)
}
/// Include a day in this month.
///
/// ```
/// use compact_calendar::CompactMonth;
///
/// let mut month = CompactMonth::new();
/// month.insert(2);
/// month.insert(2);
/// month.insert(19);
/// assert_eq!(month.count(), 2);
/// ```
pub fn insert(&mut self, day: u32) {
assert!((1..=31).contains(&day));
self.0 |= 1 << (day - 1)
}
/// Check if this month includes the given day.
///
/// ```
/// use compact_calendar::CompactMonth;
///
/// let mut month = CompactMonth::new();
/// month.insert(1);
/// month.insert(18);
///
/// assert!(month.contains(1));
/// assert!(month.contains(18));
/// assert!(!month.contains(5));
/// ```
pub fn contains(self, day: u32) -> bool {
assert!((1..=31).contains(&day));
self.0 & (1 << (day - 1)) != 0
}
/// Iterate over the days included in this month.
///
/// ```
/// use compact_calendar::CompactMonth;
///
/// let mut month = CompactMonth::new();
/// month.insert(18);
/// month.insert(1);
///
/// let days: Vec<u32> = month.iter().collect();
/// assert_eq!(days, [1, 18])
/// ```
pub fn iter(self) -> impl Iterator<Item = u32> {
let mut val = self.0;
std::iter::from_fn(move || {
if val != 0 {
let day0 = val.trailing_zeros();
val ^= 1 << day0;
Some(day0 + 1)
} else {
None
}
})
}
/// Get the first day included in this month if it is not empty.
///
/// ```
/// use compact_calendar::CompactMonth;
///
/// let mut month = CompactMonth::new();
/// assert_eq!(month.first(), None);
///
/// month.insert(31);
/// assert_eq!(month.first(), Some(31));
///
/// month.insert(8);
/// assert_eq!(month.first(), Some(8));
/// ```
pub fn first(self) -> Option<u32> {
if self.0 == 0 {
None
} else {
Some(self.0.trailing_zeros() + 1)
}
}
/// Get the first day included in this month that follows the input day, if such a day exists.
///
/// ```
/// use compact_calendar::CompactMonth;
///
/// let mut month = CompactMonth::new();
/// month.insert(4);
/// month.insert(17);
///
/// assert_eq!(month.first_after(2), Some(4));
/// assert_eq!(month.first_after(4), Some(17));
/// assert_eq!(month.first_after(17), None);
/// ```
pub fn first_after(self, day: u32) -> Option<u32> {
assert!((1..=31).contains(&day));
let shifted = self.0 >> day;
if shifted == 0 {
None
} else {
Some(day + shifted.trailing_zeros() + 1)
}
}
/// Count number of days included for this month.
///
/// ```
/// use compact_calendar::CompactMonth;
///
/// let mut month = CompactMonth::new();
/// month.insert(26);
/// month.insert(3);
/// assert_eq!(month.count(), 2);
/// ```
pub fn count(self) -> u32 {
self.0.count_ones()
}
/// Serialize this month into a writer.
///
/// ```
/// use compact_calendar::CompactMonth;
///
/// let mut month = CompactMonth::new();
///
/// let mut buf1 = Vec::new();
/// month.insert(31);
/// month.serialize(&mut buf1).unwrap();
///
/// let mut buf2 = Vec::new();
/// month.insert(1);
/// month.serialize(&mut buf2).unwrap();
///
/// assert_ne!(buf1, buf2);
/// ```
pub fn serialize(self, mut writer: impl io::Write) -> io::Result<()> {
writer.write_all(&self.0.to_ne_bytes())
}
/// Deserialize a month from a reader.
///
/// ```
/// use compact_calendar::CompactMonth;
///
/// let mut month1 = CompactMonth::new();
/// month1.insert(30);
/// month1.insert(2);
///
/// let mut buf = Vec::new();
/// month1.serialize(&mut buf).unwrap();
///
/// let month2 = CompactMonth::deserialize(buf.as_slice()).unwrap();
/// assert_eq!(month1, month2);
/// ```
pub fn deserialize(mut reader: impl io::Read) -> io::Result<Self> {
let mut buf = [0; std::mem::size_of::<u32>()];
reader.read_exact(&mut buf)?;
Ok(Self(u32::from_ne_bytes(buf)))
}
}
impl fmt::Debug for CompactMonth {
/// ```
/// use compact_calendar::CompactMonth;
///
/// let mut month = CompactMonth::new();
/// month.insert(26);
/// month.insert(3);
/// assert_eq!(format!("{month:?}"), "{03, 26}");
/// ```
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
struct DebugDay(u32);
impl fmt::Debug for DebugDay {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:02}", self.0)
}
}
f.debug_set().entries(self.iter().map(DebugDay)).finish()
}
}