aperture_cli/engine/
loader.rs

1use crate::cache::models::CachedSpec;
2use crate::error::Error;
3use std::fs;
4use std::path::Path;
5
6/// Loads a cached `OpenAPI` specification from the binary cache.
7///
8/// # Arguments
9/// * `cache_dir` - The directory containing cached spec files
10/// * `spec_name` - The name of the spec to load (without .bin extension)
11///
12/// # Returns
13/// * `Ok(CachedSpec)` - The loaded and deserialized specification
14/// * `Err(Error)` - If the file doesn't exist or deserialization fails
15///
16/// # Errors
17/// Returns an error if the cache file doesn't exist or if deserialization fails
18pub fn load_cached_spec<P: AsRef<Path>>(
19    cache_dir: P,
20    spec_name: &str,
21) -> Result<CachedSpec, Error> {
22    let cache_path = cache_dir.as_ref().join(format!("{spec_name}.bin"));
23
24    // Check if the cache file exists
25    if !cache_path.exists() {
26        return Err(Error::CachedSpecNotFound {
27            name: spec_name.to_string(),
28        });
29    }
30
31    // Read the binary cache file
32    let cache_data = fs::read(&cache_path).map_err(Error::Io)?;
33
34    // Deserialize using bincode
35    bincode::deserialize(&cache_data).map_err(|e| Error::CachedSpecCorrupted {
36        name: spec_name.to_string(),
37        reason: e.to_string(),
38    })
39}