thalo_filestore/
lib.rs

1//! A filestore implementation of [EventStore](thalo::event_store::EventStore).
2//!
3//! Built on top of [thalo-inmemory](https://docs.rs/thalo-inmemory), events are persisted to a file.
4
5pub use error::Error;
6pub use event_store::FlatFileEventStore;
7
8mod error;
9mod event_store;
10
11#[cfg(test)]
12mod tests {
13    use std::fs;
14
15    use tempfile::NamedTempFile;
16    use thalo::{
17        event_store::EventStore,
18        tests_cfg::bank_account::{
19            BankAccount, BankAccountEvent, DepositedFundsEvent, OpenedAccountEvent,
20        },
21    };
22
23    use crate::FlatFileEventStore;
24
25    #[tokio::test]
26    async fn it_works() -> Result<(), Box<dyn std::error::Error>> {
27        let file_store = NamedTempFile::new()?.into_temp_path().to_path_buf();
28
29        let event_store = FlatFileEventStore::load(file_store.clone())?;
30
31        event_store.event_store.print();
32
33        event_store
34            .save_events::<BankAccount>(
35                &"jimmy".to_string(),
36                &[BankAccountEvent::OpenedAccount(OpenedAccountEvent {
37                    balance: 0.0,
38                })],
39            )
40            .await?;
41
42        event_store
43            .save_events::<BankAccount>(
44                &"jimmy".to_string(),
45                &[BankAccountEvent::DepositedFunds(DepositedFundsEvent {
46                    amount: 24.0,
47                })],
48            )
49            .await?;
50
51        let file_content = fs::read_to_string(file_store.clone())?;
52        println!("{}", file_content);
53
54        FlatFileEventStore::load(file_store)?.event_store.print();
55
56        Ok(())
57    }
58}