solver/lookup/
repository.rs1use crate::lookup::id::ModelIdentifier;
2use crate::lookup::table::LookupTable;
3use std::fs;
4use std::path::{Path, PathBuf};
5
6pub struct LookupRepository {
8 base_path: PathBuf,
9}
10
11impl LookupRepository {
12 pub fn new<P: AsRef<Path>>(base_path: P) -> Self {
13 let path = base_path.as_ref().to_path_buf();
14 if !path.exists() {
15 fs::create_dir_all(&path)
16 .unwrap_or_else(|e| eprintln!("Failed to create repo dir: {}", e));
17 }
18 Self { base_path: path }
19 }
20
21 pub fn save(&self, id: &ModelIdentifier, table: &LookupTable) -> bincode::Result<()> {
22 let filename = id.to_filename();
23 let path = self.base_path.join(filename);
24 table.save(&path)
25 }
26
27 pub fn load(&self, id: &ModelIdentifier) -> bincode::Result<LookupTable> {
28 let filename = id.to_filename();
29 let path = self.base_path.join(filename);
30 LookupTable::load(&path)
31 }
32
33 pub fn exists(&self, id: &ModelIdentifier) -> bool {
34 let filename = id.to_filename();
35 self.base_path.join(filename).exists()
36 }
37
38 pub fn list_files(&self) -> Vec<PathBuf> {
42 let mut files = Vec::new();
43 if let Ok(entries) = fs::read_dir(&self.base_path) {
44 for entry in entries.flatten() {
45 if let Ok(file_type) = entry.file_type()
46 && file_type.is_file()
47 {
48 files.push(entry.path());
49 }
50 }
51 }
52 files
53 }
54}