Skip to content

Commit

Permalink
refactor: pass sources as mut ref
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdoret committed Oct 22, 2024
1 parent a7d94e0 commit 70e192e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
14 changes: 7 additions & 7 deletions fuzon/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ pub fn get_file_stamp(path: &str) -> Result<String> {
/// Generate a fixed cache key based on a collection of source paths.
/// Each path is converted to a stamp in the format "{path}-{fingerprint}-{modified-date}".
/// Stamps are then concatenated and hash of this concatenation is returned.
pub fn get_cache_key(paths: Vec<&str>) -> Result<String> {
pub fn get_cache_key(paths: &mut Vec<&str>) -> Result<String> {
paths.sort();

// Craft all stamps and concatenate them
let mut concat = String::new();
for path in paths.into_iter() {
for path in paths.iter() {
if !PathBuf::from(path).exists() && Url::parse(path).is_err() {
return Err(anyhow::anyhow!("Invalid path: {}", path));
}
Expand All @@ -61,9 +61,9 @@ pub fn get_cache_key(paths: Vec<&str>) -> Result<String> {
}

/// Get the full cross-platform cache path for a collection of source paths.
pub fn get_cache_path(sources: &Vec<&str>) -> Result<PathBuf> {
pub fn get_cache_path(sources: &mut Vec<&str>) -> Result<PathBuf> {
let cache_dir = dirs::cache_dir().unwrap().join("fuzon");
let cache_key = get_cache_key(&sources)?;
let cache_key = get_cache_key(sources)?;
let cache_path = cache_dir.join(&cache_key);

return Ok(cache_path);
Expand All @@ -89,9 +89,9 @@ mod tests {

#[test]
fn cache_path() {
let sources = vec!["Cargo.toml", "https://www.rust-lang.org/"];
let path = get_cache_path(&sources).unwrap();
let key = get_cache_key(&sources).unwrap();
let mut sources = vec!["Cargo.toml", "https://www.rust-lang.org/"];
let path = get_cache_path(&mut sources.clone()).unwrap();
let key = get_cache_key(&mut sources).unwrap();
assert!(path.ends_with(key));
}
}
10 changes: 3 additions & 7 deletions fuzon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ struct Args {
fn main() -> Result<()> {
let args = Args::parse();

let sources = args.source.iter().map(|s| s.as_str()).collect();
let mut sources = args.source.iter().map(|s| s.as_str()).collect();

// Attempt to load from cache
let matcher: TermMatcher;
if !args.no_cache {
let cache_path = get_cache_path(&sources)?;
let cache_path = get_cache_path(&mut sources)?;
let _ = fs::create_dir_all(cache_path.parent().unwrap());
// Cache hit
matcher = if let Ok(matcher) = TermMatcher::load(&cache_path) {
Expand Down Expand Up @@ -61,8 +61,4 @@ fn main() -> Result<()> {
}

#[cfg(test)]
mod tests {
use super::*;

fn match_urls() {}
}
mod tests {}
11 changes: 5 additions & 6 deletions pyfuzon/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anyhow::Result;
use core::fmt;
use pyo3::prelude::*;
use std::{io::BufRead, path::PathBuf};
use std::path::PathBuf;

use fuzon::{cache, gather_terms, get_source, TermMatcher};
use rff;
Expand Down Expand Up @@ -97,18 +96,18 @@ pub fn dump_terms(terms: Vec<Term>, path: PathBuf) -> PyResult<()> {
/// Get a full platform-specific cache path based on input collection of sources.
#[pyfunction]
pub fn get_cache_path(sources: Vec<String>) -> PyResult<String> {
let src_ref = sources.iter().map(|s| s.as_str()).collect();
let cache_path = cache::get_cache_path(&src_ref)?;
let mut src_ref = sources.iter().map(|s| s.as_str()).collect();
let cache_path = cache::get_cache_path(&mut src_ref)?;

return Ok(cache_path.to_str().unwrap().to_owned());
}

/// Get a deterministic cache key based on input collection of sources
#[pyfunction]
pub fn get_cache_key(sources: Vec<String>) -> PyResult<String> {
let src_ref = sources.iter().map(|s| s.as_str()).collect();
let mut src_ref = sources.iter().map(|s| s.as_str()).collect();

return Ok(cache::get_cache_key(&src_ref)?);
return Ok(cache::get_cache_key(&mut src_ref)?);
}

#[pymodule]
Expand Down

0 comments on commit 70e192e

Please sign in to comment.