1pub fn map_with_table(name: &str, table: &[(&str, &str)]) -> Option<String> {
10 table
11 .iter()
12 .find(|(from, _)| *from == name)
13 .map(|(_, to)| (*to).to_string())
14}
15
16#[macro_export]
25macro_rules! tensor_map {
26 ($name:expr, { $($from:literal => $to:literal),* $(,)? } $(, $fallback:expr)?) => {{
27 const TABLE: &[(&str, &str)] = &[$(($from, $to)),*];
28 match $crate::map_with_table($name, TABLE) {
29 Some(mapped) => Some(mapped),
30 None => {
31 $( $fallback )?
32 None::<String>
33 }
34 }
35 }};
36}
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41
42 #[test]
43 fn exact_hits() {
44 const T: &[(&str, &str)] = &[("a.weight", "b.weight")];
45 assert_eq!(map_with_table("a.weight", T).as_deref(), Some("b.weight"));
46 assert_eq!(map_with_table("missing", T), None);
47 }
48
49 #[test]
50 fn macro_form() {
51 let out: Option<String> = tensor_map!("a.weight", {"a.weight" => "b.weight"});
52 assert_eq!(out.as_deref(), Some("b.weight"));
53 }
54}