Files
harmony/harmony_assets/src/asset.rs
Jean-Gabriel Gill-Couture 50f62b6437 chore: warning sweep — auto-fix pass + scoped allows for generated code
Workspace warning count: 408 → 105.

Three buckets cleared:

* Auto-fixable (`cargo fix` + `cargo clippy --fix`): unused imports
  removed, unused variables prefixed with `_`, deprecated method
  calls updated. Applied across harmony, harmony-k8s, harmony-agent,
  harmony_inventory_agent, the fleet/ workspace, and ~15 examples.
* Generated code (opnsense-api/src/generated/): 269 snake_case
  warnings + ~10 unreachable-pattern warnings come from
  CamelCase-preserving bindings to OPNsense's HAProxy/Caddy XML
  schemas. Scoped a single `#[allow(non_snake_case,
  unreachable_patterns)]` at `pub mod generated;` rather than
  fighting the codegen — renaming would break serde round-trips
  and the codegen would regenerate them anyway.
* opnsense-codegen parser's defensive `let...else` guards on
  `XmlNode` (currently single-variant): file-level
  `#![allow(irrefutable_let_patterns)]` with a comment explaining
  why we keep the `else` arms (they re-arm if the IR grows a
  second variant).

`harmony_inventory_agent::local_presence::{DiscoveryEvent,
discover_agents}` re-exports were stripped twice by the auto-fix
passes (consumers live in another crate, so the local crate looks
"unused" to lint). Anchored with explicit `pub use` + an
`#[allow(unused_imports)]` annotation noting why.

All 151 harmony lib tests still pass. Remaining ~105 warnings are
mostly real dead code in non-fleet modules + a handful of
unused-imports/variables clippy couldn't auto-resolve; cleared in
the next pass.
2026-05-06 22:51:44 -04:00

134 lines
3.6 KiB
Rust

use crate::hash::ChecksumAlgo;
use std::path::PathBuf;
use url::Url;
#[derive(Debug, Clone)]
pub struct Asset {
pub url: Url,
pub checksum: String,
pub checksum_algo: ChecksumAlgo,
pub file_name: String,
pub size: Option<u64>,
}
impl Asset {
pub fn new(url: Url, checksum: String, checksum_algo: ChecksumAlgo, file_name: String) -> Self {
Self {
url,
checksum,
checksum_algo,
file_name,
size: None,
}
}
pub fn with_size(mut self, size: u64) -> Self {
self.size = Some(size);
self
}
pub fn formatted_checksum(&self) -> String {
crate::hash::format_checksum(&self.checksum, self.checksum_algo.clone())
}
}
#[derive(Debug, Clone)]
pub struct LocalCache {
pub base_dir: PathBuf,
}
impl LocalCache {
pub fn new(base_dir: PathBuf) -> Self {
Self { base_dir }
}
pub fn path_for(&self, asset: &Asset) -> PathBuf {
let prefix = &asset.checksum[..16.min(asset.checksum.len())];
self.base_dir.join(prefix).join(&asset.file_name)
}
pub fn cache_key_dir(&self, asset: &Asset) -> PathBuf {
let prefix = &asset.checksum[..16.min(asset.checksum.len())];
self.base_dir.join(prefix)
}
pub async fn ensure_dir(&self, asset: &Asset) -> Result<(), crate::errors::AssetError> {
let dir = self.cache_key_dir(asset);
tokio::fs::create_dir_all(&dir)
.await
.map_err(crate::errors::AssetError::IoError)?;
Ok(())
}
}
impl Default for LocalCache {
fn default() -> Self {
let base_dir = directories::ProjectDirs::from("io", "NationTech", "Harmony")
.map(|dirs| dirs.cache_dir().join("assets"))
.unwrap_or_else(|| PathBuf::from("/tmp/harmony_assets"));
Self::new(base_dir)
}
}
#[derive(Debug, Clone)]
pub struct StoredAsset {
pub url: Url,
pub checksum: String,
pub checksum_algo: ChecksumAlgo,
pub size: u64,
pub key: String,
}
#[cfg(test)]
mod tests {
use super::*;
fn test_asset(checksum: &str) -> Asset {
Asset::new(
Url::parse("https://example.com/test.iso").unwrap(),
checksum.to_string(),
ChecksumAlgo::BLAKE3,
"test.iso".to_string(),
)
}
#[test]
fn asset_path_uses_checksum_prefix_and_filename() {
let cache = LocalCache::new(PathBuf::from("/tmp/test_cache"));
let asset = test_asset("abcdef1234567890abcdef1234567890");
let path = cache.path_for(&asset);
assert_eq!(
path,
PathBuf::from("/tmp/test_cache/abcdef1234567890/test.iso")
);
}
#[test]
fn asset_path_handles_short_checksum() {
let cache = LocalCache::new(PathBuf::from("/tmp/test_cache"));
let asset = test_asset("abc");
let path = cache.path_for(&asset);
assert_eq!(path, PathBuf::from("/tmp/test_cache/abc/test.iso"));
}
#[test]
fn cache_key_dir_is_prefix_only() {
let cache = LocalCache::new(PathBuf::from("/tmp/test_cache"));
let asset = test_asset("abcdef1234567890abcdef1234567890");
let dir = cache.cache_key_dir(&asset);
assert_eq!(dir, PathBuf::from("/tmp/test_cache/abcdef1234567890"));
}
#[test]
fn asset_with_size() {
let asset = test_asset("abc").with_size(1024);
assert_eq!(asset.size, Some(1024));
}
#[test]
fn formatted_checksum_includes_algo_prefix() {
let asset = test_asset("deadbeef");
assert_eq!(asset.formatted_checksum(), "blake3:deadbeef");
}
}