gmaps_static/
marker_label.rs1use std::fmt;
2use std::ops::RangeInclusive;
3use std::str::FromStr;
4
5#[derive(Clone)]
6pub struct MarkerLabel(char);
7
8impl MarkerLabel {
9 pub fn new(label: char) -> Result<Self, String> {
10 let label = label.to_ascii_uppercase();
11 if RangeInclusive::new('A', 'Z').contains(&label)
12 || RangeInclusive::new('0', '9').contains(&label)
13 {
14 return Ok(MarkerLabel(label));
15 }
16
17 Err(format!(
18 "Invalid Label '{}'. Only a char matching [A-Z0-9] is accepted",
19 label
20 ))
21 }
22}
23
24impl FromStr for MarkerLabel {
25 type Err = String;
26
27 fn from_str(label: &str) -> Result<Self, Self::Err> {
28 if label.len() != 1 {
29 return Err(format!(
30 "Invalid label '{}'. Only a string made of a char matching [A-Z0-9] is accepted",
31 label
32 ));
33 }
34
35 MarkerLabel::new(label.chars().next().unwrap())
36 }
37}
38
39impl From<char> for MarkerLabel {
40 fn from(c: char) -> Self {
41 MarkerLabel::new(c).unwrap()
42 }
43}
44
45impl fmt::Display for MarkerLabel {
46 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47 write!(f, "{}", self.0)
48 }
49}