Skip to content

Commit

Permalink
refactor: restructure code (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
lsndr authored Apr 10, 2024
1 parent 8b22129 commit e16cebb
Show file tree
Hide file tree
Showing 15 changed files with 949 additions and 885 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ crate-type = ["cdylib"]

[dependencies]
chrono = "0.4.24"
chrono-tz = "0.8.5"
# Default enable napi5 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2.11.1", default-features = false, features = ["napi5"] }
napi-derive = "2.11.0"
Expand Down
2 changes: 1 addition & 1 deletion __test__/monthly.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,5 @@ test('Monthly on the second to last Monday of the month for 6 months', () => {
test('Errors on invalid by-weekday', () => {
expect(() =>
new RRule(Frequency.Monthly).setByWeekday(['invalid' as any]),
).toThrowError('Value is non of these types `JsNWeekday`, `JsWeekday`');
).toThrowError('Value is non of these types `NWeekday`, `Weekday`');
});
80 changes: 39 additions & 41 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,6 @@ export const enum Frequency {
Minutely = 5,
Secondly = 6
}
export const enum Weekday {
Monday = 0,
Tuesday = 1,
Wednesday = 2,
Thursday = 3,
Friday = 4,
Saturday = 5,
Sunday = 6
}
export interface 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.
*/
n?: number
weekday: Weekday
}
export const enum Month {
January = 0,
February = 1,
Expand All @@ -45,30 +26,25 @@ export const enum Month {
November = 10,
December = 11
}
export class RRuleTimezone {
constructor(tz: string)
export interface NWeekday {
/**
* The name of the timezone. If the timezone is local, it will return "Local".
*/
get name(): string
get isLocal(): boolean
* 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.
*/
n?: number
weekday: Weekday
}
export class RRuleDateTime {
constructor(date: Date | number, timezone?: string | undefined | null)
get timestamp(): number
get timezone(): RRuleTimezone
get day(): number
get month(): number
get year(): number
get hour(): number
get minute(): number
get second(): number
get millisecond(): number
get toString(): string
toDate(): Date
toUtcDate(): Date
export const enum Weekday {
Monday = 0,
Tuesday = 1,
Wednesday = 2,
Thursday = 3,
Friday = 4,
Saturday = 5,
Sunday = 6
}
export type JsRRule = RRule
export class RRule {
constructor(frequency: Frequency)
static parse(str: string): RRule
Expand Down Expand Up @@ -101,7 +77,21 @@ export class RRule {
setWeekstart(day: Weekday): this
setUntil(dateTime: RRuleDateTime | Date): this
}
export type JsRRuleSet = RRuleSet
export class RRuleDateTime {
constructor(date: Date | number, timezone?: string | undefined | null)
get timestamp(): number
get timezone(): RRuleTimezone
get day(): number
get month(): number
get year(): number
get hour(): number
get minute(): number
get second(): number
get millisecond(): number
get toString(): string
toDate(): Date
toUtcDate(): Date
}
export class RRuleSet {
constructor(dtstart: RRuleDateTime | Date)
static parse(str: string): RRuleSet
Expand All @@ -122,3 +112,11 @@ export class RRuleSet {
export class Occurrences {
[Symbol.iterator](): Iterator<RRuleDateTime, void, void>
}
export class RRuleTimezone {
constructor(tz: string)
/**
* The name of the timezone. If the timezone is local, it will return "Local".
*/
get name(): string
get isLocal(): boolean
}
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,13 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { Frequency, Weekday, Month, RRuleTimezone, RRuleDateTime, RRule, RRuleSet, Occurrences } = nativeBinding
const { Frequency, Month, RRule, RRuleDateTime, RRuleSet, Occurrences, RRuleTimezone, Weekday } = nativeBinding

module.exports.Frequency = Frequency
module.exports.Weekday = Weekday
module.exports.Month = Month
module.exports.RRuleTimezone = RRuleTimezone
module.exports.RRuleDateTime = RRuleDateTime
module.exports.RRule = RRule
module.exports.RRuleDateTime = RRuleDateTime
module.exports.RRuleSet = RRuleSet
module.exports.Occurrences = Occurrences
module.exports.RRuleTimezone = RRuleTimezone
module.exports.Weekday = Weekday
17 changes: 17 additions & 0 deletions src/js.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
mod frequency;
mod month;
mod n_weekday;
mod rrule;
mod rrule_datetime;
mod rrule_set;
mod rrule_timezone;
mod weekday;

pub use frequency::Frequency;
pub use month::Month;
pub use n_weekday::NWeekday;
pub use rrule::RRule;
pub use rrule_datetime::RRuleDateTime;
pub use rrule_set::RRuleSet;
pub use rrule_timezone::RRuleTimezone;
pub use weekday::Weekday;
40 changes: 40 additions & 0 deletions src/js/frequency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use napi_derive::napi;

#[napi(js_name = "Frequency")]
pub enum Frequency {
Yearly,
Monthly,
Weekly,
Daily,
Hourly,
Minutely,
Secondly,
}

impl From<rrule::Frequency> for Frequency {
fn from(freq: rrule::Frequency) -> Self {
match freq {
rrule::Frequency::Daily => Frequency::Daily,
rrule::Frequency::Hourly => Frequency::Hourly,
rrule::Frequency::Minutely => Frequency::Minutely,
rrule::Frequency::Monthly => Frequency::Monthly,
rrule::Frequency::Secondly => Frequency::Secondly,
rrule::Frequency::Weekly => Frequency::Weekly,
rrule::Frequency::Yearly => Frequency::Yearly,
}
}
}

impl Into<rrule::Frequency> for Frequency {
fn into(self) -> rrule::Frequency {
match self {
Frequency::Daily => rrule::Frequency::Daily,
Frequency::Hourly => rrule::Frequency::Hourly,
Frequency::Minutely => rrule::Frequency::Minutely,
Frequency::Monthly => rrule::Frequency::Monthly,
Frequency::Secondly => rrule::Frequency::Secondly,
Frequency::Weekly => rrule::Frequency::Weekly,
Frequency::Yearly => rrule::Frequency::Yearly,
}
}
}
56 changes: 56 additions & 0 deletions src/js/month.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use napi_derive::napi;

#[napi(js_name = "Month")]
pub enum Month {
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December,
}

impl From<&u8> for Month {
fn from(month: &u8) -> Self {
match month {
0 => Month::January,
1 => Month::February,
2 => Month::March,
3 => Month::April,
4 => Month::May,
5 => Month::June,
6 => Month::July,
7 => Month::August,
8 => Month::September,
9 => Month::October,
10 => Month::November,
11 => Month::December,
_ => panic!("Unknown month index: {}", month),
}
}
}

impl Into<chrono::Month> for Month {
fn into(self) -> chrono::Month {
match self {
Month::January => chrono::Month::January,
Month::February => chrono::Month::February,
Month::March => chrono::Month::March,
Month::April => chrono::Month::April,
Month::May => chrono::Month::May,
Month::June => chrono::Month::June,
Month::July => chrono::Month::July,
Month::August => chrono::Month::August,
Month::September => chrono::Month::September,
Month::October => chrono::Month::October,
Month::November => chrono::Month::November,
Month::December => chrono::Month::December,
}
}
}
36 changes: 36 additions & 0 deletions src/js/n_weekday.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use super::weekday::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<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()),
}
}
}
Loading

0 comments on commit e16cebb

Please sign in to comment.