//! Functions for reading from cache. use std::path::Path; use ssri::{Algorithm, Integrity}; use crate::content::read::{self, Reader}; use crate::errors::Error; use crate::index::{self, Entry}; pub struct Get { reader: Reader, } impl std::io::Read for Get { fn read(&mut self, buf: &mut [u8]) -> std::io::Result { self.reader.read(buf) } } impl Get { pub fn check(self) -> Result { self.reader.check() } } pub fn open(cache: P, key: K) -> Result where P: AsRef, K: AsRef, { if let Some(entry) = index::find(cache.as_ref(), key.as_ref())? { let reader = read::open(cache.as_ref(), entry.integrity)?; Ok(Get { reader }) } else { Err(Error::NotFound) } } pub fn open_hash

(cache: P, sri: Integrity) -> Result where P: AsRef, { Ok(Get { reader: read::open(cache.as_ref(), sri)?, }) } pub fn read(cache: P, key: K) -> Result, Error> where P: AsRef, K: AsRef, { if let Some(entry) = index::find(cache.as_ref(), key.as_ref())? { read_hash(cache, &entry.integrity) } else { Err(Error::NotFound) } } pub fn read_hash

(cache: P, sri: &Integrity) -> Result, Error> where P: AsRef, { Ok(read::read(cache.as_ref(), sri)?) } pub fn copy(cache: P, key: K, to: Q) -> Result where P: AsRef, K: AsRef, Q: AsRef, { if let Some(entry) = index::find(cache.as_ref(), key.as_ref())? { copy_hash(cache, &entry.integrity, to) } else { Err(Error::NotFound) } } pub fn copy_hash(cache: P, sri: &Integrity, to: Q) -> Result where P: AsRef, Q: AsRef, { read::copy(cache.as_ref(), sri, to.as_ref()) } pub fn info(cache: P, key: K) -> Result, Error> where P: AsRef, K: AsRef, { index::find(cache.as_ref(), key.as_ref()) } pub fn hash_exists>(cache: P, sri: &Integrity) -> bool { read::has_content(cache.as_ref(), &sri).is_some() }