chant/repository/
in_memory.rs1use std::collections::HashMap;
2
3use anyhow::{Context, Result};
4
5use crate::spec::Spec;
6
7use super::spec_repository::SpecRepository;
8
9pub struct InMemorySpecRepository {
11 specs: HashMap<String, Spec>,
12}
13
14impl Default for InMemorySpecRepository {
15 fn default() -> Self {
16 Self::new()
17 }
18}
19
20impl InMemorySpecRepository {
21 pub fn new() -> Self {
23 Self {
24 specs: HashMap::new(),
25 }
26 }
27
28 pub fn with_specs(specs: Vec<Spec>) -> Self {
30 let mut map = HashMap::new();
31 for spec in specs {
32 map.insert(spec.id.clone(), spec);
33 }
34 Self { specs: map }
35 }
36}
37
38impl SpecRepository for InMemorySpecRepository {
39 fn load(&self, id: &str) -> Result<Spec> {
40 self.specs
41 .get(id)
42 .cloned()
43 .context(format!("Spec not found: {}", id))
44 }
45
46 fn save(&self, _spec: &Spec) -> Result<()> {
47 Ok(())
50 }
51
52 fn list_all(&self) -> Result<Vec<Spec>> {
53 Ok(self.specs.values().cloned().collect())
54 }
55}