summaryrefslogblamecommitdiff
path: root/src/cache.rs
blob: 199a44073b8a4df24a26b3a95e02aa231f2c613a (plain) (tree)
1
2
3
4
5
6
7
8
9



                         
 
                                                 


                                                                        
                                                               



                                                                                       






                                                                    
                

 

                                                                                
                                                                

                                                                      
                                  
          
 
use std::{
    collections::HashMap,
    fs::{copy, read_dir},
};

use crate::errors::{ConversionError, Error, MLE};

/// # Errors
pub fn get_cached_versions(path: &str) -> MLE<HashMap<String, String>> {
    let mut versions: HashMap<String, String> = HashMap::new();
    for file in read_dir(path)? {
        let path = file?.path();
        if path.is_file() && path.extension().ok_or(Error::NoFileExtension)? == "jar" {
            let pathstr = path.to_str().ok_or(ConversionError::InvalidPath)?;
            let namesplit: Vec<&str> = pathstr.split('.').collect();
            versions.insert(
                String::from(namesplit[namesplit.len() - 2]),
                String::from(pathstr),
            );
        }
    }
    Ok(versions)
}

/// # Errors
pub fn copy_cached_version(version_path: &str, download_path: &str) -> MLE<()> {
    let versplit: Vec<&str> = version_path.split('/').collect();
    let download =
        format!("{}/{}", download_path, versplit[versplit.len() - 1]);
    copy(version_path, download)?;
    Ok(())
}