use toml_edit::DocumentMut;
use crate::finding::Finding;
use crate::types::Severity;
#[must_use]
#[expect(
clippy::type_complexity,
reason = "Returning (DocumentMut, Vec<Finding>) is the natural shape for this helper."
)]
pub fn parse_or_report(
current_bytes: Option<&[u8]>,
file_label: &str,
) -> (DocumentMut, Vec<Finding>) {
let mut findings: Vec<Finding> = Vec::new();
let text = match current_bytes {
Some(bytes) => match std::str::from_utf8(bytes) {
Ok(s) => s,
Err(e) => {
findings.push(Finding::ParseError {
message: format!("{file_label} is not valid UTF-8: {e}"),
severity: Severity::Error,
});
return (DocumentMut::new(), findings);
}
},
None => "",
};
match text.parse::<DocumentMut>() {
Ok(doc) => (doc, findings),
Err(e) => {
findings.push(Finding::ParseError {
message: format!("{file_label} is not valid TOML: {e}"),
severity: Severity::Error,
});
(DocumentMut::new(), findings)
}
}
}
#[must_use]
pub fn parse_version_tuple(v: &str) -> (u64, u64, u64) {
let normalized = v.trim_start_matches('v');
let mut parts = normalized.split('.').map(|p| p.parse::<u64>().unwrap_or(0));
(
parts.next().unwrap_or(0),
parts.next().unwrap_or(0),
parts.next().unwrap_or(0),
)
}