Skip to content

Commit

Permalink
less braces in ledgers\
Browse files Browse the repository at this point in the history
  • Loading branch information
TBS1996 committed Feb 26, 2025
1 parent 53cee88 commit d320070
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 99 deletions.
64 changes: 31 additions & 33 deletions speki-core/src/card/basecard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ pub fn normalize_string(str: &str) -> String {
}

impl RunLedger<CardEvent> for BaseCard {
type Error = ();
fn caches(&self) -> HashSet<String>{
let mut out: HashSet<String> = Default::default();

Expand Down Expand Up @@ -622,54 +623,55 @@ impl RunLedger<CardEvent> for BaseCard {
}
}

fn run_event(mut self, event: CardEvent) -> Self {
match event.action {
CardAction::SetFrontAudio { audio } => {
self.front_audio = audio;
},
CardAction::SetBackAudio { audio } => {
self.back_audio = audio;
},
CardAction::UpsertCard { ty } => {
self.ty = ty;
},
CardAction::DeleteCard => {
},
CardAction::AddDependency { dependency } => {
self.dependencies.insert(dependency);
},
CardAction::RemoveDependency { dependency } => {
self.dependencies.remove(&dependency);
self.ty.remove_dep(dependency);
},
CardAction::SetBackRef { reff } => {
let backside = BackSide::Card(reff);
self.ty = self.ty.set_backside(backside);
},
fn run_event(mut self, event: CardEvent) -> Result<Self, ()> {
for action in event.action {
match action {
CardAction::SetFrontAudio ( audio ) => {
self.front_audio = audio;
},
CardAction::SetBackAudio ( audio ) => {
self.back_audio = audio;
},
CardAction::UpsertCard ( ty ) => {
self.ty = ty;
},
CardAction::DeleteCard => {},
CardAction::AddDependency ( dependency ) => {
self.dependencies.insert(dependency);
},
CardAction::RemoveDependency ( dependency ) => {
self.dependencies.remove(&dependency);
self.ty.remove_dep(dependency);
},
CardAction::SetBackRef ( reff ) => {
let backside = BackSide::Card(reff);
self.ty = self.ty.set_backside(backside);
},
}
}

self
Ok(self)
}

fn derive_events(&self) -> Vec<CardEvent> {
let mut actions = vec![];

let action = CardAction::UpsertCard { ty: self.ty.clone() };
let action = CardAction::UpsertCard (self.ty.clone() );
actions.push(action);

if let Some(audio) = self.front_audio {
let action = CardAction::SetFrontAudio { audio: Some(audio)};
let action = CardAction::SetFrontAudio ( Some(audio));
actions.push(action);
}

if let Some(audio) = self.back_audio {
let action = CardAction::SetFrontAudio { audio: Some(audio)};
let action = CardAction::SetFrontAudio ( Some(audio));
actions.push(action);
}


for dep in &self.dependencies {
let action = CardAction::AddDependency {dependency: *dep};
let action = CardAction::AddDependency (*dep);
actions.push(action);
}

Expand All @@ -681,10 +683,6 @@ impl RunLedger<CardEvent> for BaseCard {
fn item_id(&self) -> String {
self.id.to_string()
}

fn identifier() -> &'static str {
"cards"
}
}


Expand Down
8 changes: 4 additions & 4 deletions speki-core/src/card/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ impl Card {
pub async fn set_ref(mut self, reff: CardId) -> Card {
let backside = BackSide::Card(reff);
self.base.ty = self.base.ty.set_backside(backside);
let action = CardAction::SetBackRef { reff };
let action = CardAction::SetBackRef ( reff );
let event = CardEvent::new(self.id, action);
self.card_provider.providers.run_event(event).await;
self.refresh().await;
Expand All @@ -246,15 +246,15 @@ impl Card {

info!("dep was there: {res}");
self.base.ty.remove_dep(dependency);
let action = CardAction::RemoveDependency {dependency };
let action = CardAction::RemoveDependency (dependency );
let event = CardEvent::new(self.id, action);
self.card_provider.providers.run_event(event).await;
self.refresh().await;
}

pub async fn add_dependency(&mut self, dependency: CardId) {
self.base.dependencies.insert(dependency);
let action = CardAction::AddDependency { dependency };
let action = CardAction::AddDependency ( dependency );
let event = CardEvent::new(self.id, action);
self.card_provider.providers.run_event(event).await;
self.refresh().await;
Expand All @@ -270,7 +270,7 @@ impl Card {

pub async fn into_type(mut self, data: impl Into<CardType>) -> Self {
self.base.ty = data.into();
let action = CardAction::UpsertCard { ty: self.base.ty.clone() };
let action = CardAction::UpsertCard ( self.base.ty.clone() );
let event = CardEvent::new(self.id, action);
self.card_provider.providers.run_event(event).await;
self.refresh().await;
Expand Down
11 changes: 5 additions & 6 deletions speki-core/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ pub struct Collection {


impl RunLedger<CollectionEvent> for Collection {
fn run_event(mut self, event: CollectionEvent) -> Self {
type Error = ();

fn run_event(mut self, event: CollectionEvent) -> Result<Self, Self::Error> {
match event.action {
CollectionAction::SetName(s) => self.name = s,
CollectionAction::InsertDyn(val) => self.dyncards.push(val),
Expand All @@ -36,7 +38,8 @@ impl RunLedger<CollectionEvent> for Collection {
}
}

self

Ok(self)
}

fn derive_events(&self) -> Vec<CollectionEvent> {
Expand All @@ -54,10 +57,6 @@ impl RunLedger<CollectionEvent> for Collection {
fn item_id(&self) -> String {
self.id.to_string()
}

fn identifier() -> &'static str {
"collections"
}
}

impl Collection {
Expand Down
34 changes: 13 additions & 21 deletions speki-core/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ use crate::{audio::AudioId, card::{BaseCard, CardId}, collection::{CollectionId,
pub fn decompose(card: &BaseCard) -> Vec<CardEvent> {
let mut actions = vec![];

let action = CardAction::UpsertCard { ty: card.ty.clone() };
let action = CardAction::UpsertCard (card.ty.clone() );
actions.push(action);


let action = CardAction::SetFrontAudio { audio: card.front_audio.clone() };
let action = CardAction::SetFrontAudio ( card.front_audio.clone() );
actions.push(action);

let action = CardAction::SetBackAudio { audio: card.back_audio.clone() };
let action = CardAction::SetBackAudio ( card.back_audio.clone() );
actions.push(action);

for dep in &card.dependencies {
let action = CardAction::AddDependency {dependency: *dep};
let action = CardAction::AddDependency (*dep);
actions.push(action);
}

Expand All @@ -34,21 +34,21 @@ pub fn check_compose(old_card: BaseCard) {
let mut card: BaseCard = BaseCard::new_default(old_card.id.to_string());

for action in actions {
card = card.run_event(action);
card = card.run_event(action).unwrap();
}

assert_eq!(&old_card, &card);
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct CardEvent {
pub action: CardAction,
pub action: Vec<CardAction>,
pub id: CardId,
}

impl CardEvent {
pub fn new(id: CardId, action: CardAction) -> Self {
Self {id, action}
Self {id, action: vec![action]}
}
}

Expand All @@ -60,21 +60,13 @@ impl LedgerEvent for CardEvent {

#[derive(Deserialize, Serialize, Clone, Debug)]
pub enum CardAction {
SetFrontAudio{audio: Option<AudioId>},
SetBackAudio{audio: Option<AudioId>},
UpsertCard {
ty: CardType,
},
SetFrontAudio(Option<AudioId>),
SetBackAudio(Option<AudioId>),
UpsertCard(CardType),
DeleteCard,
AddDependency {
dependency: CardId,
},
RemoveDependency {
dependency: CardId,
},
SetBackRef{
reff: CardId,
},
AddDependency(CardId),
RemoveDependency(CardId),
SetBackRef(CardId),
}


Expand Down
23 changes: 7 additions & 16 deletions speki-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,7 @@ impl Provider {
}
}

for i in 0..actions.len() {
if matches!(&actions[i].action, CardAction::UpsertCard {..}) {
let action = actions.remove(i);
actions.insert(0, action);
}
}

actions
todo!();
}

pub async fn run_event(&self, event: impl Into<Event>) {
Expand Down Expand Up @@ -352,10 +345,8 @@ impl App {

let id = CardId::new_v4();

let action = CardAction::UpsertCard { ty: data.into() };
let event = CardEvent {
action, id
};
let action = CardAction::UpsertCard ( data.into() );
let event = CardEvent::new(id, action);

self.provider.run_event(event).await;
id
Expand All @@ -374,15 +365,15 @@ impl App {
class,
};
let id = CardId::new_v4();
let event = CardEvent::new(id, CardAction::UpsertCard { ty: data.into() });
let event = CardEvent::new(id, CardAction::UpsertCard ( data.into() ));
self.provider.run_event(event).await;
id
}

pub async fn add_card_with_id(&self, front: String, back: impl Into<BackSide>, id: CardId) {
let back = back.into();
let data = NormalCard { front, back };
let event = CardEvent::new(id, CardAction::UpsertCard { ty: data.into() });
let event = CardEvent::new(id, CardAction::UpsertCard ( data.into() ));
self.provider.run_event(event).await;
}

Expand All @@ -391,15 +382,15 @@ impl App {
let data = NormalCard { front, back };

let id = CardId::new_v4();
let event = CardEvent::new(id, CardAction::UpsertCard { ty: data.into() });
let event = CardEvent::new(id, CardAction::UpsertCard ( data.into() ));
self.provider.run_event(event).await;
id
}

pub async fn add_unfinished(&self, front: String) -> CardId {
let data = UnfinishedCard { front };
let id = CardId::new_v4();
let event = CardEvent::new(id, CardAction::UpsertCard { ty: data.into() });
let event = CardEvent::new(id, CardAction::UpsertCard ( data.into() ));
self.provider.run_event(event).await;
id
}
Expand Down
10 changes: 4 additions & 6 deletions speki-core/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ impl Metadata {
}

impl RunLedger<MetaEvent> for Metadata {
fn run_event(mut self, event: MetaEvent) -> Self {
type Error = ();

fn run_event(mut self, event: MetaEvent) -> Result<Self, ()> {
match event.action {
crate::ledger::MetaAction::Suspend(flag) => self.suspended = flag.into(),
}

self
Ok(self)
}

fn derive_events(&self) -> Vec<MetaEvent> {
Expand All @@ -53,10 +55,6 @@ impl RunLedger<MetaEvent> for Metadata {
fn item_id(&self) -> String {
self.id.to_string()
}

fn identifier() -> &'static str {
"metadata"
}
}

#[derive(Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Clone)]
Expand Down
11 changes: 5 additions & 6 deletions speki-core/src/recall_rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,11 @@ impl LedgerEvent for ReviewEvent {
}
}


impl RunLedger<ReviewEvent> for History {
fn run_event(mut self, event: ReviewEvent) -> Self {
type Error = ();

fn run_event(mut self, event: ReviewEvent) -> Result<Self, ()> {
let review = Review {
timestamp: event.timestamp,
grade: event.grade,
Expand All @@ -197,7 +200,7 @@ impl RunLedger<ReviewEvent> for History {

self.push(review);

self
Ok(self)
}

fn derive_events(&self) -> Vec<ReviewEvent> {
Expand All @@ -223,10 +226,6 @@ impl RunLedger<ReviewEvent> for History {
fn item_id(&self) -> String {
self.id.to_string()
}

fn identifier() -> &'static str {
"history"
}
}


Expand Down
Loading

0 comments on commit d320070

Please sign in to comment.