Files
convertis/src/runner.rs
T
Elias Wendland f58f73194e
Release / check-release (push) Successful in 59s
Release / build_gnu (push) Skipped
Release / build_musl (push) Skipped
Release / build_windows (push) Skipped
Release / package_gnu (deb) (push) Skipped
Release / package_gnu (rpm) (push) Skipped
Release / package_musl (deb) (push) Skipped
Release / package_musl (rpm) (push) Skipped
Release / publish-release (push) Skipped
CI / Test (push) Successful in 1m44s
CI / Build Linux (push) Successful in 2m3s
feat: add ffmpeg audio plugin and migrate verbosity to tracing levels
2026-07-15 22:57:00 +02:00

134 lines
4.0 KiB
Rust

// Copyright (C) 2026 Elias Wendland <eliaswendland@pm.me>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3 exclusively.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::plugin::Plugin;
use std::path::Path;
use std::time::Instant;
pub fn run_conversion(
path: &[(&dyn Plugin, &str, &str)],
input: &[u8],
temp_dir: &Path,
) -> Result<Vec<u8>, String> {
let path_str: Vec<_> = path.iter().map(|(p, _, _)| p.name()).collect();
tracing::info!("Path taken: {}", path_str.join(" -> "));
let mut current_data = input.to_vec();
for (plugin, from_format, to_format) in path {
tracing::info!(
"Converting {} to {} using plugin {}...",
from_format,
to_format,
plugin.name()
);
tracing::debug!("[Plugin Log] Running plugin: {}", plugin.name());
tracing::debug!("[Plugin Log] Source format: {}", from_format);
tracing::debug!("[Plugin Log] Target format: {}", to_format);
tracing::debug!(
"[Plugin Log] Metrics - Familiarity: {}, Quality: {}, Speed: {}",
plugin.familiarity(*from_format, *to_format),
plugin.quality(*from_format, *to_format),
plugin.speed(*from_format, *to_format)
);
let start_time = Instant::now();
current_data = plugin.convert(&current_data, from_format, to_format, temp_dir)?;
let elapsed = start_time.elapsed();
tracing::info!(
"Step '{} -> {}' completed in {:?}",
from_format, to_format, elapsed
);
}
Ok(current_data)
}
#[cfg(test)]
mod tests {
use super::*;
struct MockPlugin {
name: &'static str,
f: u8,
q: u8,
s: u8,
fail: bool,
}
impl Plugin for MockPlugin {
fn name(&self) -> &'static str {
self.name
}
fn from_formats(&self) -> Vec<&'static str> {
vec!["a"]
}
fn to_formats(&self) -> Vec<&'static str> {
vec!["b"]
}
fn familiarity(&self, _from: &str, _to: &str) -> u8 {
self.f
}
fn quality(&self, _from: &str, _to: &str) -> u8 {
self.q
}
fn speed(&self, _from: &str, _to: &str) -> u8 {
self.s
}
fn convert(
&self,
input: &[u8],
_from: &str,
_to: &str,
_temp_dir: &Path,
) -> Result<Vec<u8>, String> {
if self.fail {
return Err("Failed".to_string());
}
let mut out = input.to_vec();
out.push(1);
Ok(out)
}
}
macro_rules! test_runner {
($name:ident, $fail:expr, $expected_res:expr, $expected_len:expr) => {
#[test]
fn $name() {
let plugin = MockPlugin {
name: "mock",
f: 10,
q: 10,
s: 10,
fail: $fail,
};
let path: Vec<(&dyn Plugin, &str, &str)> = vec![(&plugin, "a", "b")];
let input = vec![0];
let result =
run_conversion(&path, &input, std::path::Path::new("/dev/shm"));
assert_eq!(result.is_ok(), $expected_res);
if let Ok(res) = result {
assert_eq!(res.len(), $expected_len);
}
}
};
}
test_runner!(test_runner_verb_0, false, true, 2);
test_runner!(test_runner_verb_0_fail, true, false, 0);
}