fn parse_hex_linetype(s: &str) -> Option<Vec<u32>> {
let len = s.len();
if !(2..=8).contains(&len) || len % 2 != 0 {
return None;
}
let mut result = Vec::with_capacity(len);
for c in s.chars() {
let digit = c.to_digit(16)?;
if digit == 0 {
return None; }
result.push(digit);
}
Some(result)
}
pub fn linetype_to_stroke_dash(name: &str) -> Option<Vec<u32>> {
match name.to_lowercase().as_str() {
"solid" => return Some(vec![]),
"dashed" => return Some(vec![6, 4]),
"dotted" => return Some(vec![1, 2]),
"dotdash" => return Some(vec![1, 2, 6, 2]),
"longdash" => return Some(vec![10, 4]),
"twodash" => return Some(vec![6, 2, 2, 2]),
_ => {}
}
parse_hex_linetype(name)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_linetype_to_stroke_dash_known() {
assert_eq!(linetype_to_stroke_dash("solid"), Some(vec![]));
assert_eq!(linetype_to_stroke_dash("dashed"), Some(vec![6, 4]));
assert_eq!(linetype_to_stroke_dash("dotted"), Some(vec![1, 2]));
assert_eq!(linetype_to_stroke_dash("dotdash"), Some(vec![1, 2, 6, 2]));
assert_eq!(linetype_to_stroke_dash("longdash"), Some(vec![10, 4]));
assert_eq!(linetype_to_stroke_dash("twodash"), Some(vec![6, 2, 2, 2]));
}
#[test]
fn test_linetype_to_stroke_dash_case_insensitive() {
assert!(linetype_to_stroke_dash("SOLID").is_some());
assert!(linetype_to_stroke_dash("Dashed").is_some());
assert!(linetype_to_stroke_dash("DoTdAsH").is_some());
}
#[test]
fn test_linetype_to_stroke_dash_unknown() {
assert!(linetype_to_stroke_dash("unknown").is_none());
assert!(linetype_to_stroke_dash("").is_none());
}
#[test]
fn test_hex_linetype_basic() {
assert_eq!(linetype_to_stroke_dash("33"), Some(vec![3, 3]));
assert_eq!(linetype_to_stroke_dash("44"), Some(vec![4, 4]));
assert_eq!(linetype_to_stroke_dash("13"), Some(vec![1, 3]));
}
#[test]
fn test_hex_linetype_four_digit() {
assert_eq!(linetype_to_stroke_dash("1343"), Some(vec![1, 3, 4, 3]));
assert_eq!(linetype_to_stroke_dash("2262"), Some(vec![2, 2, 6, 2]));
}
#[test]
fn test_hex_linetype_six_and_eight_digit() {
assert_eq!(
linetype_to_stroke_dash("123456"),
Some(vec![1, 2, 3, 4, 5, 6])
);
assert_eq!(
linetype_to_stroke_dash("12345678"),
Some(vec![1, 2, 3, 4, 5, 6, 7, 8])
);
}
#[test]
fn test_hex_linetype_ggplot2_standards() {
assert_eq!(linetype_to_stroke_dash("44"), Some(vec![4, 4])); assert_eq!(linetype_to_stroke_dash("13"), Some(vec![1, 3])); assert_eq!(linetype_to_stroke_dash("1343"), Some(vec![1, 3, 4, 3])); assert_eq!(linetype_to_stroke_dash("73"), Some(vec![7, 3])); assert_eq!(linetype_to_stroke_dash("2262"), Some(vec![2, 2, 6, 2])); }
#[test]
fn test_hex_linetype_with_letters() {
assert_eq!(linetype_to_stroke_dash("af"), Some(vec![10, 15]));
assert_eq!(linetype_to_stroke_dash("AF"), Some(vec![10, 15]));
assert_eq!(linetype_to_stroke_dash("1a2b"), Some(vec![1, 10, 2, 11]));
}
#[test]
fn test_hex_linetype_invalid() {
assert!(linetype_to_stroke_dash("123").is_none());
assert!(linetype_to_stroke_dash("1").is_none());
assert!(linetype_to_stroke_dash("1234567890").is_none());
assert!(linetype_to_stroke_dash("10").is_none());
assert!(linetype_to_stroke_dash("01").is_none());
assert!(linetype_to_stroke_dash("gg").is_none());
assert!(linetype_to_stroke_dash("1x").is_none());
}
}