const SUBFAMILIES: &[(&str, &str)] = &[
("mimx93", "01235"),
("mimx95", "0345789n"),
("mimx952", "234589"),
];
pub(crate) fn normalize(chip: &str) -> String {
let lowered = chip.to_ascii_lowercase();
match lowered.strip_prefix("imx") {
Some(rest) => format!("mimx{rest}"),
None => lowered,
}
}
pub(crate) fn matches(needle: &str, family: &str) -> bool {
let Some(rest) = needle.strip_prefix(family) else {
return false;
};
let Some((_, allowed)) = SUBFAMILIES.iter().find(|(name, _)| *name == family) else {
return true;
};
rest.chars()
.next()
.is_none_or(|next| allowed.contains(next))
}
#[cfg(test)]
mod tests {
use super::*;
const IMX93: &[&str] = &[
"mimx9301", "mimx9302", "mimx9311", "mimx9312", "mimx9321", "mimx9322", "mimx9331",
"mimx9332", "mimx9351", "mimx9352",
];
const IMX95: &[&str] = &[
"mimx9506", "mimx9534", "mimx9536", "mimx9546", "mimx9554", "mimx9556", "mimx9574",
"mimx9576", "mimx9586", "mimx9594", "mimx9596", "mimx95n4", "mimx95n6",
];
const IMX952: &[&str] = &[
"mimx9522", "mimx9523", "mimx9524", "mimx9525", "mimx9528", "mimx9529",
];
const IMX937: &[&str] = &["mimx9371", "mimx9373", "mimx9375"];
#[test]
fn accepts_every_variant_of_its_family() {
for part in IMX93 {
assert!(matches(part, "mimx93"), "{part}");
}
for part in IMX95 {
assert!(matches(part, "mimx95"), "{part}");
}
for part in IMX952 {
assert!(matches(part, "mimx952"), "{part}");
}
}
#[test]
fn accepts_full_part_numbers_and_bare_family() {
assert!(matches("mimx9352dvvxm", "mimx93"));
assert!(matches("mimx9596xvt", "mimx95"));
assert!(matches("mimx95n6cvyxn", "mimx95"));
assert!(matches("mimx95", "mimx95"));
assert!(matches(&normalize("iMX93"), "mimx93"));
}
#[test]
fn rejects_other_dies_sharing_a_prefix() {
for part in IMX952.iter().chain(IMX937) {
assert!(!matches(part, "mimx93"), "{part} matched mimx93");
assert!(!matches(part, "mimx95"), "{part} matched mimx95");
}
for part in IMX937 {
assert!(!matches(part, "mimx952"), "{part} matched mimx952");
}
assert!(!matches("mimx9529avzxn", "mimx95"));
assert!(!matches(&normalize("IMX952"), "mimx95"));
assert!(matches(&normalize("IMX952"), "mimx952"));
assert!(matches("mimx9529avzxn", "mimx952"));
assert!(!matches("mimx9521", "mimx952"), "no i.MX 952 segment 1");
}
#[test]
fn families_without_subfamily_rules_stay_prefix_matches() {
assert!(matches("stm32mp257fak3", "stm32mp25"));
assert!(!matches("stm32mp235fak3", "stm32mp25"));
}
}