Skip to content

Commit

Permalink
Add comment to file.
Browse files Browse the repository at this point in the history
  • Loading branch information
bloodnighttw committed Jul 17, 2024
1 parent 7fe87ee commit d968d94
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 12 additions & 1 deletion reginleif-utils/src/save_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,9 @@ pub trait Cache:DeserializeOwned{
Ok(json)
}}

/// 1. the file exist and the sha is valid, return the data from disk.
/// 2. the file exist and the sha is invalid, fetch the data from the source and save it to the disk, then return the data.
/// 3. the file not exist, fetch the data from the source and save it to the disk, then return the data.
fn check_cache<P: AsRef<Path>+Send>(base:&Self::AcceptStorePoint, suffix:P, client: Client, url: &str, sha:SHA)
-> impl std::future::Future<Output = anyhow::Result<Self>> + Send{async move {

Expand Down Expand Up @@ -413,7 +416,7 @@ pub trait Cache:DeserializeOwned{
Ok(json)
}}


/// Return a builder for the cache.
fn builder() -> CacheBuilder<Self::AcceptStorePoint,Self> where Self::AcceptStorePoint:Clone{
CacheBuilder{
url:"".to_string(),
Expand All @@ -425,6 +428,9 @@ pub trait Cache:DeserializeOwned{

}


/// Using builder pattern for [Cache] trait.
/// This builder is required [T] impl Clone trait to use.
pub struct CacheBuilder<T:BaseStorePoint,U:Cache>{
url:String,
buf:PathBuf,
Expand All @@ -435,27 +441,32 @@ pub struct CacheBuilder<T:BaseStorePoint,U:Cache>{

impl <T,U> CacheBuilder<T, U> where U:Cache<AcceptStorePoint=T>, T:BaseStorePoint+Clone{

/// append the path to the buffer.
pub fn add<P: AsRef<Path>+Send>(mut self,args:P) -> Self{
self.buf.push(args);
self
}

/// change the url you want to fetch.
pub fn url<P: AsRef<str>>(mut self,args:P) -> Self{
self.url = args.as_ref().to_string();
self
}

/// set the base path of the data.
pub fn base_on(mut self, args:&T) -> Self{
self.base = Some(args.clone());
self
}

/// run [U::check_cache] from builder and return the result.
pub fn build_check(&self, client: Client, sha:SHA)
-> impl std::future::Future<Output=anyhow::Result<U>> + Send + '_{
let base = &self.base.as_ref().unwrap();
U::check_cache(base,&self.buf,client,&self.url,sha)
}

/// run [U::try_cache] from builder and return the result.
pub fn build_try(&self, client: Client) -> impl std::future::Future<Output = anyhow::Result<U>> + Send + '_{
let base = &self.base.as_ref().unwrap();
U::try_cache(base,&self.buf,client,&self.url)
Expand Down
6 changes: 5 additions & 1 deletion reginleif-utils/src/sha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl<'de> Deserialize<'de> for SHA{
}
}

/// decode hex to u8 array
fn decode_hex(s: &str) -> Result<Vec<u8>, ParseIntError> {
(0..s.len())
.step_by(2)
Expand All @@ -44,7 +45,7 @@ fn decode_hex(s: &str) -> Result<Vec<u8>, ParseIntError> {
impl TryFrom<&str> for SHA{
type Error = ParseIntError;


/// decode str to SHA
fn try_from(value: &str) -> Result<Self, Self::Error> {

let decode = decode_hex(value)?;
Expand All @@ -63,12 +64,14 @@ impl TryFrom<&str> for SHA{
impl TryFrom<String> for SHA{
type Error = ParseIntError;

/// convert String to SHA
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::try_from(value.as_str())
}
}

impl From<&SHA> for String{
/// convert SHA to String
fn from(value: &SHA) -> Self {
match value {
SHA::SHA1(v) => {
Expand All @@ -82,6 +85,7 @@ impl From<&SHA> for String{
}

impl From<SHA> for String{
/// encode SHA to String
fn from(value: SHA) -> Self {
Self::from(&value)
}
Expand Down

0 comments on commit d968d94

Please sign in to comment.