feat: add backend constructor
This commit is contained in:
Generated
+1
-1
@@ -158,7 +158,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "droplet-rs"
|
||||
version = "0.8.0"
|
||||
version = "0.8.1"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"dyn-clone",
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
edition = "2021"
|
||||
authors = ["Drop-OSS"]
|
||||
name = "droplet-rs"
|
||||
version = "0.8.0"
|
||||
version = "0.8.1"
|
||||
license = "AGPL-3.0-only"
|
||||
description = "Droplet is a `napi.rs` Rust/Node.js package full of high-performance and low-level utils for Drop"
|
||||
|
||||
|
||||
@@ -1,2 +1,54 @@
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
use crate::versions::{
|
||||
backends::{
|
||||
PathVersionBackend, ZipVersionBackend, SEVEN_ZIP_INSTALLED, SUPPORTED_FILE_EXTENSIONS,
|
||||
},
|
||||
types::VersionBackend,
|
||||
};
|
||||
|
||||
pub mod backends;
|
||||
pub mod types;
|
||||
pub mod backends;
|
||||
pub fn create_backend_constructor<'a>(
|
||||
path: &Path,
|
||||
) -> Option<Box<dyn FnOnce() -> Result<Box<dyn VersionBackend + Send + 'a>>>> {
|
||||
if !path.exists() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let is_directory = path.is_dir();
|
||||
if is_directory {
|
||||
let base_dir = path.to_path_buf();
|
||||
return Some(Box::new(move || {
|
||||
Ok(Box::new(PathVersionBackend { base_dir }))
|
||||
}));
|
||||
};
|
||||
|
||||
if *SEVEN_ZIP_INSTALLED {
|
||||
/*
|
||||
Slow 7zip integrity test
|
||||
let mut test = Command::new("7z");
|
||||
test.args(vec!["t", path.to_str().expect("invalid utf path")]);
|
||||
let status = test.status().ok()?;
|
||||
if status.code().unwrap_or(1) == 0 {
|
||||
let buf = path.to_path_buf();
|
||||
return Some(Box::new(move || Ok(Box::new(ZipVersionBackend::new(buf)?))));
|
||||
}
|
||||
*/
|
||||
// Fast filename-based test
|
||||
if let Some(extension) = path.extension().and_then(|v| v.to_str()) {
|
||||
let supported = SUPPORTED_FILE_EXTENSIONS
|
||||
.iter()
|
||||
.find(|v| ***v == *extension)
|
||||
.is_some();
|
||||
if supported {
|
||||
let buf = path.to_path_buf();
|
||||
return Some(Box::new(move || Ok(Box::new(ZipVersionBackend::new(buf)?))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user