Skip to content

Commit

Permalink
try add example (#2)
Browse files Browse the repository at this point in the history
add an example
  • Loading branch information
asukaminato0721 authored Jan 28, 2025
1 parent 384e139 commit b53fa44
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ jobs:
- name: Flutter pub get
run: flutter pub get

# - name: Flutter analyze
# run: flutter analyze
- name: Flutter analyze
run: flutter analyze
3 changes: 0 additions & 3 deletions build.dart

This file was deleted.

30 changes: 30 additions & 0 deletions lib/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'src/rust/frb_generated.dart';
import 'src/rust/api/fsrs_api.dart';

Future<void> example() async {
// Initialize the Rust bridge
await RustLib.init();

// Create a new FSRS instance with default parameters
final fsrs = await Fsrs.newInstance(
parameters: List<double>.from([
0.4, 0.6, 2.4, 5.8, 4.93, 0.94, 0.86, 0.01, 1.49, 0.14, 0.94,
2.18, 0.05, 0.34, 1.26, 0.29, 2.61, 0.05, 0.4
]),
);

// Example: Calculate next states for a new item
// Initial review with no prior memory state
final nextStates = fsrs.nextStates(
currentMemoryState: null, // No prior state for new items
desiredRetention: 0.9, // 90% retention target
daysElapsed: 0, // First review
);

// Print the results
print('Next states calculated:');
print('Again state: ${(nextStates.again).interval} days');
print('Hard state: ${(nextStates.hard).interval} days');
print('Good state: ${(nextStates.good).interval} days');
print('Easy state: ${(nextStates.easy).interval} days');
}
15 changes: 15 additions & 0 deletions rust/src/api/fsrs_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ pub fn init_app() {
}

#[derive(Debug, Clone)]
#[frb(opaque)]
pub struct FSRS(fsrs::FSRS);
impl FSRS {
pub fn new(parameters: Vec<f32>) -> Self {
Self(fsrs::FSRS::new(Some(&parameters)).unwrap())
}
#[frb(sync)]
pub fn next_states(
&self,
current_memory_state: Option<MemoryState>,
Expand Down Expand Up @@ -57,6 +59,7 @@ impl FSRS {
}
}
#[derive(Debug, Clone)]
#[frb(opaque)]
pub struct MemoryState(fsrs::MemoryState);

impl MemoryState {
Expand All @@ -69,29 +72,37 @@ impl MemoryState {
}

#[derive(Debug, Clone)]
#[frb(opaque)]
pub struct NextStates(fsrs::NextStates);
impl NextStates {
#[frb(sync, getter)]
pub fn hard(&self) -> ItemState {
ItemState(self.0.hard.clone())
}
#[frb(sync, getter)]
pub fn good(&self) -> ItemState {
ItemState(self.0.good.clone())
}
#[frb(sync, getter)]
pub fn easy(&self) -> ItemState {
ItemState(self.0.easy.clone())
}
#[frb(sync, getter)]
pub fn again(&self) -> ItemState {
ItemState(self.0.again.clone())
}
}

#[derive(Debug, Clone)]
#[frb(opaque)]
pub struct ItemState(fsrs::ItemState);

impl ItemState {
#[frb(sync, getter)]
pub fn memory(&self) -> MemoryState {
MemoryState(self.0.memory.clone())
}
#[frb(sync, getter)]
pub fn interval(&self) -> f32 {
self.0.interval
}
Expand All @@ -101,9 +112,11 @@ impl ItemState {
}

#[derive(Debug, Clone)]
#[frb(opaque)]
pub struct FSRSItem(fsrs::FSRSItem);

impl FSRSItem {
#[frb(sync)]
pub fn new(reviews: Vec<FSRSReview>) -> Self {
Self(fsrs::FSRSItem {
reviews: reviews.iter().map(|x| x.0).collect(),
Expand Down Expand Up @@ -133,9 +146,11 @@ impl FSRSItem {
}

#[derive(Debug, Clone)]
#[frb(opaque)]
pub struct FSRSReview(fsrs::FSRSReview);

impl FSRSReview {
#[frb(sync)]
pub fn new(rating: u32, delta_t: u32) -> Self {
Self(fsrs::FSRSReview { rating, delta_t })
}
Expand Down

0 comments on commit b53fa44

Please sign in to comment.