#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum LineStylePreset {
#[default]
Solid,
Dash,
Dot,
DashDot,
DashDotDot,
ShortDash,
ShortDot,
ShortDashDot,
LongDash,
LongDashDot,
LongDashDotDot,
}
impl LineStylePreset {
pub fn pattern(&self) -> &'static [f32] {
match self {
LineStylePreset::Solid => &[],
LineStylePreset::Dash => &[8.0, 4.0],
LineStylePreset::Dot => &[2.0, 3.0],
LineStylePreset::DashDot => &[8.0, 4.0, 2.0, 4.0],
LineStylePreset::DashDotDot => &[8.0, 4.0, 2.0, 4.0, 2.0, 4.0],
LineStylePreset::ShortDash => &[4.0, 3.0],
LineStylePreset::ShortDot => &[1.0, 2.0],
LineStylePreset::ShortDashDot => &[4.0, 3.0, 1.0, 3.0],
LineStylePreset::LongDash => &[14.0, 4.0],
LineStylePreset::LongDashDot => &[14.0, 4.0, 2.0, 4.0],
LineStylePreset::LongDashDotDot => &[14.0, 4.0, 2.0, 4.0, 2.0, 4.0],
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn solid_pattern_is_empty() {
assert!(LineStylePreset::Solid.pattern().is_empty());
}
#[test]
fn dash_presets_are_positive_on_off_pairs() {
for preset in [
LineStylePreset::Dash,
LineStylePreset::Dot,
LineStylePreset::DashDot,
LineStylePreset::DashDotDot,
LineStylePreset::ShortDash,
LineStylePreset::ShortDot,
LineStylePreset::ShortDashDot,
LineStylePreset::LongDash,
LineStylePreset::LongDashDot,
LineStylePreset::LongDashDotDot,
] {
let pattern = preset.pattern();
assert!(!pattern.is_empty());
assert_eq!(pattern.len() % 2, 0, "{preset:?}");
assert!(pattern.iter().all(|length| *length > 0.0), "{preset:?}");
}
}
}