-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make js depend on own rrule (#172)
* feat: make js depend on own rrule * docs: update readme
- Loading branch information
Showing
40 changed files
with
1,838 additions
and
1,842 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,6 @@ | ||
mod datetime; | ||
mod frequency; | ||
mod month; | ||
mod n_weekday; | ||
mod rrule; | ||
mod rrule_set; | ||
mod weekday; | ||
|
||
pub use datetime::DateTime; | ||
pub use frequency::Frequency; | ||
pub use month::Month; | ||
pub use n_weekday::NWeekday; | ||
pub use rrule::RRule; | ||
pub use rrule_set::RRuleSet; | ||
pub use weekday::Weekday; | ||
pub mod frequency; | ||
pub mod month; | ||
pub mod n_weekday; | ||
pub mod rrule; | ||
pub mod rrule_set; | ||
pub mod weekday; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,27 @@ | ||
use std::str::FromStr; | ||
|
||
use super::weekday::Weekday; | ||
use crate::rrule::n_weekday; | ||
use napi_derive::napi; | ||
|
||
#[napi(object, js_name = "NWeekday")] | ||
pub struct NWeekday { | ||
/// If set, this represents the nth occurrence of the weekday. | ||
/// Otherwise it represents every occurrence of the weekday. | ||
/// | ||
/// A negative value represents nth occurrence from the end. | ||
pub n: Option<i16>, | ||
pub weekday: Weekday, | ||
} | ||
|
||
impl From<rrule::NWeekday> for NWeekday { | ||
fn from(nday: rrule::NWeekday) -> Self { | ||
match nday { | ||
rrule::NWeekday::Every(weekday) => NWeekday { | ||
n: None, | ||
weekday: Weekday::from(weekday), | ||
}, | ||
rrule::NWeekday::Nth(n, weekday) => NWeekday { | ||
n: Some(n), | ||
weekday: weekday.into(), | ||
}, | ||
impl Into<n_weekday::NWeekday> for NWeekday { | ||
fn into(self) -> n_weekday::NWeekday { | ||
n_weekday::NWeekday { | ||
n: self.n, | ||
weekday: self.weekday.into(), | ||
} | ||
} | ||
} | ||
|
||
impl Into<rrule::NWeekday> for NWeekday { | ||
fn into(self) -> rrule::NWeekday { | ||
match self.n { | ||
Some(n) => rrule::NWeekday::Nth(n, self.weekday.into()), | ||
None => rrule::NWeekday::Every(self.weekday.into()), | ||
impl From<&n_weekday::NWeekday> for NWeekday { | ||
fn from(n_weekday: &n_weekday::NWeekday) -> Self { | ||
Self { | ||
n: n_weekday.n, | ||
weekday: (&n_weekday.weekday).into(), | ||
} | ||
} | ||
} | ||
|
||
impl Into<String> for &NWeekday { | ||
fn into(self) -> String { | ||
match self.n { | ||
Some(n) => { | ||
let n = if n > 1 || n < 0 { | ||
n.to_string() | ||
} else { | ||
"".to_string() | ||
}; | ||
let weekday: String = self.weekday.into(); | ||
|
||
format!("{}{}", n, weekday) | ||
} | ||
None => self.weekday.into(), | ||
} | ||
} | ||
} | ||
|
||
impl FromStr for NWeekday { | ||
type Err = String; | ||
|
||
fn from_str(str: &str) -> Result<Self, Self::Err> { | ||
let weekday = extract_weekday(str)?; | ||
let n = if str.len() > 2 { | ||
Some(extract_number(str)?) | ||
} else { | ||
None | ||
}; | ||
|
||
Ok(NWeekday { n, weekday }) | ||
} | ||
} | ||
|
||
fn extract_weekday(str: &str) -> Result<Weekday, String> { | ||
let weekday = str | ||
.chars() | ||
.rev() | ||
.take(2) | ||
.collect::<String>() | ||
.chars() | ||
.rev() | ||
.collect::<String>(); | ||
|
||
weekday.parse() | ||
} | ||
|
||
fn extract_number(str: &str) -> Result<i16, String> { | ||
let number = str | ||
.chars() | ||
.take_while(|c| c.is_digit(10) || *c == '-') | ||
.collect::<String>(); | ||
|
||
number | ||
.parse() | ||
.map_err(|_| format!("Invalid number: {}", number)) | ||
} |
Oops, something went wrong.