Skip to content

Commit

Permalink
all getter
Browse files Browse the repository at this point in the history
  • Loading branch information
asukaminato0721 committed Oct 9, 2024
1 parent 734530f commit 39fd55b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 29 deletions.
15 changes: 9 additions & 6 deletions examples/basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ int main(void) {
fsrs_SchedulingInfo scheduling_info =
fsrs_ScheduledCards_get(&scheduled_cards, rating);
fsrs_Card card = fsrs_SchedulingInfo_card(&scheduling_info);
fsrs_ReviewLog log = fsrs_SchedulingInfo_review_log(&scheduling_info);

printf("scheduled_days: %" PRId64 "\nelapsed_days: %" PRId64
"\ndate: %s"
fsrs_ReviewLog review_log =
fsrs_SchedulingInfo_review_log(&scheduling_info);
time_t reviewed_date = fsrs_ReviewLog_reviewed_date(&review_log);
printf("scheduled_days: %" PRId64 "\nelapsed_days: %" PRId64 "\ndate: %s"
"rating: %d"
"\nstate: %d"
"\n",
log.scheduled_days, log.elapsed_days,
asctime(localtime(&log.reviewed_date_s)), log.rating, log.state);
fsrs_ReviewLog_scheduled_days(&review_log),
fsrs_ReviewLog_elapsed_days(&review_log),
asctime(localtime(&reviewed_date)),
fsrs_ReviewLog_rating(&review_log),
fsrs_ReviewLog_state(&review_log));
printf("card:\n elapsed_days: %ld\n scheduled_days: %ld\n due: %ld\n "
"stability: %f\n difficulty: %f\n reps: %d\n lapses: %d\n "
"state: %u\n last_review: %ld\n",
Expand Down
22 changes: 14 additions & 8 deletions include/fsrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,14 @@ typedef struct fsrs_RecordLog {
const struct fsrs_RecordLog *_0;
} fsrs_RecordLog;

typedef struct fsrs_ReviewLog {
const struct fsrs_ReviewLog *_0;
} fsrs_ReviewLog;

typedef struct fsrs_SchedulingInfo {
const struct fsrs_SchedulingInfo *_0;
} fsrs_SchedulingInfo;

typedef struct fsrs_ReviewLog {
enum fsrs_Rating rating;
int64_t elapsed_days;
int64_t scheduled_days;
enum fsrs_State state;
int64_t reviewed_date_s;
} fsrs_ReviewLog;

double fsrs_Card_difficulty(const struct fsrs_Card *self);

int64_t fsrs_Card_due(const struct fsrs_Card *self);
Expand Down Expand Up @@ -88,6 +84,16 @@ struct fsrs_RecordLog fsrs_Fsrs_repeat_timestamp(const struct fsrs_Fsrs *fsrs,
const struct fsrs_Card *card,
int64_t now);

int64_t fsrs_ReviewLog_elapsed_days(const struct fsrs_ReviewLog *self);

enum fsrs_Rating fsrs_ReviewLog_rating(const struct fsrs_ReviewLog *self);

int64_t fsrs_ReviewLog_reviewed_date(const struct fsrs_ReviewLog *self);

int64_t fsrs_ReviewLog_scheduled_days(const struct fsrs_ReviewLog *self);

enum fsrs_State fsrs_ReviewLog_state(const struct fsrs_ReviewLog *self);

struct fsrs_SchedulingInfo fsrs_ScheduledCards_get(const struct fsrs_RecordLog *self,
enum fsrs_Rating r);

Expand Down
49 changes: 34 additions & 15 deletions src/fsrs_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,7 @@ impl SchedulingInfo {
#[no_mangle]
pub extern "C" fn fsrs_SchedulingInfo_review_log(&self) -> ReviewLog {
let s = unsafe { &*(*self).0 };
let log = s.review_log.clone();
ReviewLog {
elapsed_days: log.elapsed_days,
scheduled_days: log.scheduled_days,
reviewed_date_s: log.reviewed_date.timestamp(),
rating: log.rating.into(),
state: log.state.into(),
}
ReviewLog(to_raw(s.review_log.clone()))
}
}

Expand Down Expand Up @@ -212,11 +205,37 @@ impl From<fsrs::State> for State {
}
}
#[repr(C)]
#[derive(Debug, Clone, Default)]
pub struct ReviewLog {
pub rating: Rating,
pub elapsed_days: i64,
pub scheduled_days: i64,
pub state: State,
pub reviewed_date_s: i64,
#[derive(Debug, Clone)]
pub struct ReviewLog(*const fsrs::ReviewLog);

impl ReviewLog {
#[no_mangle]
pub extern "C" fn fsrs_ReviewLog_rating(&self) -> Rating {
let r = unsafe { &*(*self).0 };
r.rating.into()
}

#[no_mangle]
pub extern "C" fn fsrs_ReviewLog_elapsed_days(&self) -> i64 {
let r = unsafe { &*(*self).0 };
r.elapsed_days
}

#[no_mangle]
pub extern "C" fn fsrs_ReviewLog_scheduled_days(&self) -> i64 {
let r = unsafe { &*(*self).0 };
r.scheduled_days
}

#[no_mangle]
pub extern "C" fn fsrs_ReviewLog_state(&self) -> State {
let r = unsafe { &*(*self).0 };
r.state.into()
}

#[no_mangle]
pub extern "C" fn fsrs_ReviewLog_reviewed_date(&self) -> i64 {
let r = unsafe { &*(*self).0 };
r.reviewed_date.timestamp()
}
}

0 comments on commit 39fd55b

Please sign in to comment.