pub const STARTER_ID: &str = "stegobench-starter";
pub fn resolved_path(path: &std::path::Path) -> std::path::PathBuf {
std::path::absolute(path).unwrap_or_else(|_| path.to_path_buf())
}
pub fn plain_io(e: &std::io::Error) -> String {
match e.kind() {
std::io::ErrorKind::NotFound => "nothing is there".to_string(),
std::io::ErrorKind::PermissionDenied => "permission was refused".to_string(),
_ => {
let text = e.to_string();
match text.split_once(" (os error") {
Some((head, _)) => head.to_string(),
None => text,
}
}
}
}
pub fn human_duration(seconds: f64) -> String {
if seconds < 90.0 {
return format!("{seconds:.0} seconds");
}
if seconds < 5_400.0 {
return format!("{:.0} minutes", seconds / 60.0);
}
format!("{:.1} hours", seconds / 3_600.0)
}
use std::path::PathBuf;
use stegobench_plugin::availability;
use stegobench_plugin::availability::Presence;
pub fn unavailable_reason(
entry: &stegobench_core::registry::Entry,
adapter_roots: &[PathBuf],
) -> Option<String> {
if !crate::needs::can_be_driven(entry) {
return Some(crate::needs::undrivable_because(entry));
}
match availability::check(entry, adapter_roots).presence {
Presence::Present { .. } => None,
Presence::Unsupported { reason } => Some(format!("cannot run on this machine: {reason}")),
Presence::Absent { reason } => Some(format!(
"is registered but is not on this machine: {}",
plainer(&reason)
)),
Presence::Unknown { reason } => Some(format!(
"whether it can run here could not be established: {reason}"
)),
}
}
pub const LOOKS_LIKE_AN_IMAGE: &[&str] = &[
"png", "jpg", "jpeg", "tif", "tiff", "bmp", "webp", "gif", "pgm", "ppm", "heic",
];
pub fn smallest_way_in(reg: &stegobench_core::registry::Registry) -> Option<String> {
use stegobench_core::registry::Kind;
let mut candidates: Vec<(u64, &str, &str)> = reg
.entries
.values()
.filter(|e| e.kind == Kind::Detector)
.filter_map(|e| {
let image = e.image.as_ref()?;
Some((image.size_mb?, e.name.as_str(), image.reference.as_str()))
})
.collect();
candidates.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.cmp(b.1)));
let (mb, name, reference) = candidates.first()?;
Some(format!(
"To ask anything about an image you need ONE detector, not all of \
them. The smallest is {name}, about {mb} MB:\n \
docker pull {reference}\n \
then: stegobench examine <your image> --detector {name}"
))
}
pub fn shorten_digests(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut rest = text;
while let Some(at) = rest.find("@sha256:") {
let (before, from) = rest.split_at(at);
out.push_str(before);
let digest: String = from["@sha256:".len()..]
.chars()
.take_while(|c| c.is_ascii_hexdigit())
.collect();
if digest.len() == 64 {
out.push_str("@sha256:");
out.push_str(&digest[..8]);
out.push_str("...");
} else {
out.push_str("@sha256:");
out.push_str(&digest);
}
rest = &from["@sha256:".len() + digest.len()..];
}
out.push_str(rest);
out
}
fn plainer(reason: &str) -> String {
if let Some(program) = reason.strip_suffix(" is not on PATH") {
return format!(
"the program `{program}` is not installed, or is not anywhere \
this can find it"
);
}
reason.to_string()
}
pub mod cli;
pub mod examine;
pub mod fetch;
pub mod fixtures;
pub mod help_topics;
pub mod metrics;
pub mod needs;
pub mod registry;
pub mod report;
pub mod score;
#[cfg(test)]
mod novice_findings {
use super::*;
#[test]
fn the_smallest_detector_is_the_one_recommended() {
let reg =
stegobench_core::registry::Registry::load(std::path::Path::new("plugins/registry"))
.or_else(|_| {
stegobench_core::registry::Registry::load(std::path::Path::new(
"../../plugins/registry",
))
})
.expect("the shipped registry loads");
let way = smallest_way_in(®).expect("the shipped registry has a sized detector");
use stegobench_core::registry::Kind;
let mut sizes: Vec<(u64, &str)> = reg
.entries
.values()
.filter(|e| e.kind == Kind::Detector)
.filter_map(|e| Some((e.image.as_ref()?.size_mb?, e.name.as_str())))
.collect();
sizes.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.cmp(b.1)));
let (mb, name) = sizes[0];
assert!(
way.contains(name),
"names something other than {name}: {way}"
);
assert!(way.contains(&mb.to_string()), "does not say {mb} MB: {way}");
assert!(way.contains("docker pull"), "{way}");
assert!(way.contains("--detector"), "{way}");
assert!(way.contains("ONE detector"), "{way}");
}
#[test]
fn a_registry_with_no_sized_detector_recommends_nothing_rather_than_guessing() {
let empty = stegobench_core::registry::Registry::default();
assert!(smallest_way_in(&empty).is_none());
}
#[test]
fn a_full_digest_in_prose_is_cut_and_a_short_one_is_left_alone() {
let full = format!("docker pull repo/name@sha256:{}", "a".repeat(64));
let cut = shorten_digests(&full);
assert!(cut.ends_with("@sha256:aaaaaaaa..."), "{cut}");
assert!(!cut.contains(&"a".repeat(64)));
let short = "repo/name@sha256:abc";
assert_eq!(shorten_digests(short), short);
assert_eq!(shorten_digests("no digest here"), "no digest here");
let two = format!(
"first a@sha256:{} then b@sha256:{} end",
"b".repeat(64),
"c".repeat(64)
);
let cut = shorten_digests(&two);
assert_eq!(
cut, "first a@sha256:bbbbbbbb... then b@sha256:cccccccc... end",
"{cut}"
);
}
#[test]
fn the_path_jargon_becomes_something_a_beginner_can_act_on() {
let plain = plainer("stegcore is not on PATH");
assert!(plain.contains("`stegcore`"), "{plain}");
assert!(plain.contains("not installed"), "{plain}");
assert!(
!plain.contains("PATH"),
"still names the reader's shell: {plain}"
);
assert_eq!(
plainer("not pulled. docker pull x"),
"not pulled. docker pull x"
);
}
}