1use std::str::FromStr;
4
5use nom::{
6 character::complete::{char, digit1},
7 combinator::all_consuming,
8 sequence::preceded,
9 IResult, Parser,
10};
11use serde::{Deserialize, Serialize};
12
13use crate::error::{map_add_intent, Error};
14
15#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
19pub struct WindowId(String);
20
21impl FromStr for WindowId {
22 type Err = Error;
23
24 fn from_str(input: &str) -> std::result::Result<Self, Self::Err> {
27 let desc = "WindowId";
28 let intent = "#{window_id}";
29
30 let (_, window_id) = all_consuming(parse::window_id)
31 .parse(input)
32 .map_err(|e| map_add_intent(desc, intent, e))?;
33
34 Ok(window_id)
35 }
36}
37
38impl WindowId {
39 pub fn as_str(&self) -> &str {
41 &self.0
42 }
43}
44
45pub(crate) mod parse {
46 use super::*;
47
48 pub(crate) fn window_id(input: &str) -> IResult<&str, WindowId> {
49 let (input, digit) = preceded(char('@'), digit1).parse(input)?;
50 let id = format!("@{digit}");
51 Ok((input, WindowId(id)))
52 }
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_parse_window_id_fn() {
61 let actual = parse::window_id("@43");
62 let expected = Ok(("", WindowId("@43".into())));
63 assert_eq!(actual, expected);
64
65 let actual = parse::window_id("@4");
66 let expected = Ok(("", WindowId("@4".into())));
67 assert_eq!(actual, expected);
68 }
69
70 #[test]
71 fn test_parse_window_id_struct() {
72 let actual = WindowId::from_str("@43");
73 assert!(actual.is_ok());
74 assert_eq!(actual.unwrap(), WindowId("@43".into()));
75
76 let actual = WindowId::from_str("4:38");
77 assert!(matches!(
78 actual,
79 Err(Error::ParseError {
80 desc: "WindowId",
81 intent: "#{window_id}",
82 err: _
83 })
84 ));
85 }
86}