-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement formatting for Chinese calendar with some todos #3760
Changes from 14 commits
ba3d48c
2afaf93
981a3b5
5f714a8
53995a2
29a8c95
7030380
5d1a32e
4370a0e
2b67c84
ec9517b
b971070
c61257a
1b5424d
27c7666
0c5cae8
fb2d2e0
e29f54c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ use core::convert::TryInto; | |
use core::fmt; | ||
use core::ops::{Add, AddAssign, Sub, SubAssign}; | ||
use core::str::FromStr; | ||
use tinystr::TinyAsciiStr; | ||
use tinystr::{TinyStr16, TinyStr4}; | ||
use zerovec::maps::ZeroMapKV; | ||
use zerovec::ule::AsULE; | ||
|
@@ -92,6 +93,35 @@ impl FormattableYear { | |
#[cfg_attr(feature = "serde", derive(serde::Deserialize))] | ||
pub struct MonthCode(pub TinyStr4); | ||
|
||
impl MonthCode { | ||
/// Returns an option which is Some containing the non-month version of a leap month | ||
/// if the MonthCode this method is called upon is a leap month, and None otherwise. | ||
/// This method assumes the MonthCode is valid. | ||
pub fn get_normal_if_leap(&self) -> Option<MonthCode> { | ||
let bytes = self.0.all_bytes(); | ||
if bytes[3] == b'L' { | ||
Some(MonthCode(TinyAsciiStr::from_bytes(&bytes[0..3]).ok()?)) | ||
} else { | ||
None | ||
} | ||
Comment on lines
+102
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: how does this code not create a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||
} | ||
} | ||
|
||
#[test] | ||
fn test_get_normal_month_code_if_leap() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. praise: thanks for writing a test! |
||
let mc1 = MonthCode(tinystr::tinystr!(4, "M01L")); | ||
let result1 = mc1.get_normal_if_leap(); | ||
assert_eq!(result1, Some(MonthCode(tinystr::tinystr!(4, "M01")))); | ||
|
||
let mc2 = MonthCode(tinystr::tinystr!(4, "M11L")); | ||
let result2 = mc2.get_normal_if_leap(); | ||
assert_eq!(result2, Some(MonthCode(tinystr::tinystr!(4, "M11")))); | ||
|
||
let mc_invalid = MonthCode(tinystr::tinystr!(4, "M10")); | ||
let result_invalid = mc_invalid.get_normal_if_leap(); | ||
assert_eq!(result_invalid, None); | ||
} | ||
|
||
impl AsULE for MonthCode { | ||
type ULE = TinyStr4; | ||
fn to_unaligned(self) -> TinyStr4 { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: MonthCode is Copy, this should just take
self
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done