tmux_lib/
session_id.rs

1//! Session Id.
2
3use 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/// The id of a Tmux session.
16///
17/// This wraps the raw tmux representation (`$11`).
18#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
19pub struct SessionId(String);
20
21impl FromStr for SessionId {
22    type Err = Error;
23
24    /// Parse into SessionId. The `&str` must start with '$' followed by a
25    /// `u16`.
26    fn from_str(input: &str) -> std::result::Result<Self, Self::Err> {
27        let desc = "SessionId";
28        let intent = "#{session_id}";
29
30        let (_, sess_id) = all_consuming(parse::session_id)
31            .parse(input)
32            .map_err(|e| map_add_intent(desc, intent, e))?;
33
34        Ok(sess_id)
35    }
36}
37
38// impl SessionId {
39//     pub fn as_str(&self) -> &str {
40//         &self.0
41//     }
42// }
43
44pub(crate) mod parse {
45    use super::*;
46
47    pub fn session_id(input: &str) -> IResult<&str, SessionId> {
48        let (input, digit) = preceded(char('$'), digit1).parse(input)?;
49        let id = format!("${digit}");
50        Ok((input, SessionId(id)))
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_parse_session_id_fn() {
60        let actual = parse::session_id("$43");
61        let expected = Ok(("", SessionId("$43".into())));
62        assert_eq!(actual, expected);
63
64        let actual = parse::session_id("$4");
65        let expected = Ok(("", SessionId("$4".into())));
66        assert_eq!(actual, expected);
67    }
68
69    #[test]
70    fn test_parse_session_id_struct() {
71        let actual = SessionId::from_str("$43");
72        assert!(actual.is_ok());
73        assert_eq!(actual.unwrap(), SessionId("$43".into()));
74
75        let actual = SessionId::from_str("4:38");
76        assert!(matches!(
77            actual,
78            Err(Error::ParseError {
79                desc: "SessionId",
80                intent: "#{session_id}",
81                err: _
82            })
83        ));
84    }
85}