generated from napi-rs/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
92 lines (84 loc) · 1.64 KB
/
lib.rs
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
#![deny(clippy::all)]
// https://github.com/rust-lang/rust-analyzer/issues/17429
use chrono::DateTime;
use chrono::Utc;
use napi_derive::napi;
#[napi(js_name = "FSRS")]
#[derive(Debug)]
pub struct FSRS(fsrs::FSRS);
#[napi]
#[derive(Debug)]
pub struct ScheduledCards(fsrs::ScheduledCards);
#[napi]
impl ScheduledCards {
#[napi]
pub fn select_card(&self, rating: Rating) -> Card {
Card(self.0.select_card(rating.into()))
}
}
#[napi]
pub enum Rating {
Again = 1,
Hard = 2,
Good = 3,
Easy = 4,
}
impl From<Rating> for fsrs::Rating {
fn from(value: Rating) -> Self {
use fsrs::Rating as r;
use Rating::*;
match value {
Again => r::Again,
Hard => r::Hard,
Good => r::Good,
Easy => r::Easy,
}
}
}
impl From<fsrs::Rating> for Rating {
fn from(value: fsrs::Rating) -> Self {
use fsrs::Rating::*;
use Rating as r;
match value {
Again => r::Again,
Hard => r::Hard,
Good => r::Good,
Easy => r::Easy,
}
}
}
#[napi]
#[derive(Debug)]
pub struct Card(fsrs::Card);
#[napi]
#[derive(Debug)]
pub struct ReviewLog(fsrs::ReviewLog);
#[napi]
impl FSRS {
#[napi(constructor)]
pub fn new() -> Self {
FSRS(fsrs::FSRS::default())
}
#[napi]
pub fn schedule(&self, card: &Card, now: DateTime<Utc>) -> ScheduledCards {
ScheduledCards(self.0.schedule(card.0.clone(), now))
}
}
#[napi]
impl Card {
#[napi(constructor)]
pub fn new() -> Self {
Self(fsrs::Card::new())
}
#[napi]
pub fn log(&self) -> ReviewLog {
ReviewLog(self.0.log.clone().unwrap())
}
}
#[napi]
impl ReviewLog {
#[napi]
pub fn to_string(&self) -> String {
format!("{:?}", self.0)
}
}