Skip to main content

gonzalo_ticket/
mock.rs

1//! An in-memory [`TicketSource`] — the read-only reference implementation,
2//! analogous to `gonzalo_vector::MemoryVectorIndex`. Useful for tests and for
3//! ingesting hand-built tickets without a network connector.
4
5use crate::source::{Capabilities, Cursor, Page, Result, SourceError, TicketSource};
6use async_trait::async_trait;
7use gonzalo_domain::Ticket;
8
9/// A fixed set of tickets served from memory. Read-only: write methods inherit
10/// the trait's `Unsupported` defaults.
11pub struct InMemorySource {
12    tickets: Vec<Ticket>,
13}
14
15impl InMemorySource {
16    pub fn new(tickets: Vec<Ticket>) -> Self {
17        Self { tickets }
18    }
19}
20
21#[async_trait]
22impl TicketSource for InMemorySource {
23    fn capabilities(&self) -> Capabilities {
24        Capabilities::default()
25    }
26
27    async fn fetch_changed(&self, _cursor: &Cursor) -> Result<Page> {
28        Ok(Page {
29            tickets: self.tickets.clone(),
30            next: Cursor::default(),
31        })
32    }
33
34    async fn get(&self, uid: &str) -> Result<Ticket> {
35        self.tickets
36            .iter()
37            .find(|t| t.uid == uid)
38            .cloned()
39            .ok_or_else(|| SourceError::Backend(format!("no ticket with uid {uid}")))
40    }
41}