CacheManager

Struct CacheManager 

Source
pub struct CacheManager { /* private fields */ }
Expand description

Local cache manager for gpacks

Implementations§

Source§

impl CacheManager

Source

pub fn new() -> Result<Self>

Create a new cache manager

Uses the default cache directory (~/.cache/ggen/gpacks on Unix systems).

§Example
use ggen_core::cache::CacheManager;

let cache = CacheManager::new()?;
println!("Cache directory: {:?}", cache.cache_dir());
Source

pub fn with_dir(cache_dir: PathBuf) -> Result<Self>

Create a cache manager with custom directory (for testing)

§Example
use ggen_core::cache::CacheManager;
use std::path::PathBuf;

let cache = CacheManager::with_dir(PathBuf::from("/tmp/ggen-cache"))?;
println!("Cache directory: {:?}", cache.cache_dir());
Source

pub fn cache_dir(&self) -> &Path

Get the cache directory path

§Example
use ggen_core::cache::CacheManager;

let cache = CacheManager::new()?;
let cache_path = cache.cache_dir();
println!("Cache is at: {:?}", cache_path);
Source

pub async fn ensure(&self, resolved_pack: &ResolvedPack) -> Result<CachedPack>

Ensure a pack is cached locally

Downloads the pack from its git repository if not already cached, or verifies the cached version matches the expected SHA256 checksum.

SHA256 verification: If resolved_pack.sha256 is provided and not empty, the cached pack’s SHA256 is verified. If the checksum doesn’t match, the corrupted cache is automatically removed and the pack is re-downloaded. If no SHA256 is provided, the cached pack is used as-is without verification.

Automatic recovery: If a cached pack is found but its SHA256 doesn’t match the expected value, the method automatically removes the corrupted cache and re-downloads the pack. This ensures data integrity without manual intervention.

§Arguments
  • resolved_pack - Pack metadata including git URL, revision, and optional SHA256
§Returns

Returns information about the cached pack, including its local path.

§Errors

Returns an error if:

  • The pack cannot be downloaded from git
  • The SHA256 checksum doesn’t match after re-download (if provided)
  • The cache directory cannot be accessed or created
  • The corrupted cache cannot be removed (should not occur in normal use)
§Examples
§Success case
use ggen_core::cache::CacheManager;
use ggen_core::registry::ResolvedPack;

let cache = CacheManager::new()?;
let pack = ResolvedPack {
    id: "io.ggen.example".to_string(),
    version: "1.0.0".to_string(),
    git_url: "https://github.com/example/pack.git".to_string(),
    git_rev: "v1.0.0".to_string(),
    sha256: "abc123...".to_string(),
};

let cached = cache.ensure(&pack).await?;
println!("Pack cached at: {:?}", cached.path);
§Error case - Invalid git URL
use ggen_core::cache::CacheManager;
use ggen_core::registry::ResolvedPack;

let cache = CacheManager::new()?;
let pack = ResolvedPack {
    id: "io.ggen.example".to_string(),
    version: "1.0.0".to_string(),
    git_url: "https://invalid-url-that-does-not-exist.git".to_string(),
    git_rev: "v1.0.0".to_string(),
    sha256: "".to_string(),
};

// This will fail because the git URL is invalid
let result = cache.ensure(&pack).await;
assert!(result.is_err());
Source

pub fn load_cached(&self, pack_id: &str, version: &str) -> Result<CachedPack>

Load a cached pack

Returns information about a pack that is already cached locally.

§Errors

Returns an error if:

  • The pack is not found in the cache
  • The pack directory exists but is corrupted
  • The manifest file cannot be read or parsed
§Examples
§Success case
use ggen_core::cache::CacheManager;

let cache = CacheManager::new()?;
// Assuming pack is already cached
let cached = cache.load_cached("io.ggen.example", "1.0.0")?;
println!("Pack path: {:?}", cached.path);
println!("Pack SHA256: {}", cached.sha256);
§Error case - Pack not found
use ggen_core::cache::CacheManager;

let cache = CacheManager::new()?;
// This will fail because the pack is not cached
let result = cache.load_cached("nonexistent.pack", "1.0.0");
assert!(result.is_err());
Source

pub fn list_cached(&self) -> Result<Vec<CachedPack>>

List all cached packs

§Example
use ggen_core::cache::CacheManager;

let cache = CacheManager::new()?;
let cached_packs = cache.list_cached()?;

for pack in cached_packs {
    println!("{}@{}: {:?}", pack.id, pack.version, pack.path);
}
Source

pub fn remove(&self, pack_id: &str, version: &str) -> Result<()>

Remove a cached pack

§Errors

Returns an error if:

  • The pack directory cannot be removed
  • The cache directory cannot be accessed
§Examples
§Success case
use ggen_core::cache::CacheManager;

let cache = CacheManager::new()?;
// Remove a specific version of a pack
cache.remove("io.ggen.example", "1.0.0")?;
§Error case - Permission denied
use ggen_core::cache::CacheManager;

let cache = CacheManager::new()?;
// This may fail if we don't have permission to remove the pack
let result = cache.remove("io.ggen.example", "1.0.0");
// Handle error appropriately
if let Err(e) = result {
    eprintln!("Failed to remove pack: {}", e);
}
Source

pub fn cleanup_old_versions(&self) -> Result<()>

Clean up old versions, keeping only the latest

Trait Implementations§

Source§

impl Clone for CacheManager

Source§

fn clone(&self) -> CacheManager

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CacheManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> SendSyncUnwindSafe for T
where T: Send + Sync + UnwindSafe + ?Sized,