1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use std::time::Duration;
use regex::Regex;
use crate::TmuxInterfaceError;


pub const SESSION_VARS_SEPARATOR: &str = ":";
// XXX: mb make all fields optional
// FIXME: regex name can be anything, and other keys should be checked better
// NOTE: no colons or periods (ref: int session_check_name(const char *name))
pub const SESSION_VARS_REGEX_VEC: [(&str, &str); 15] = [
    ("session_alerts",          r"(\w+)?"),
    ("session_attached",        r"(\d+)?"),
    ("session_activity",        r"(\d+)?"),
    ("session_created",         r"(\d+)?"),
    ("session_format",          r"(\w+)?"),
    ("session_last_attached",   r"(\d+)?"),
    ("session_group",           r"(\w+)?"),
    ("session_group_size",      r"(\w+)?"),
    ("session_group_list",      r"(\w+)?"),
    ("session_grouped",         r"(\w+)?"),
    ("session_id",              r"\$(\d+)?"),
    ("session_many_attached",   r"(\w+)?"),
    ("session_name",            r"(\w+)?"),
    ("session_stack",           r"([\w,]*)?"),
    ("session_windows",         r"(\d+)?"),
];


//struct asdf<'a> {
    //vec: Vec<(&'a str, &'a str)>,
    //separator: &'a str
//}


//impl<'a> asdf<'a> {
    //fn new(vec: &Vec<(&str, &str)>, separator: &str) {
    //}


    //fn get_format(&self) -> String {
        //self.vec.iter()
            //.map(|t| format!("#{{{}}}", t.0))
            //.collect::<Vec<String>>()
            //.join(self.separator)
    //}


    //fn get_regex(&self) -> String {
        //format!("^{}$", self.vec.iter()
                //.map(|t| t.1)
                //.collect::<Vec<&str>>()
                //.join(self.separator)
        //)
    //}
//}


// accordingly to tmux.h: Formats
// XXX: check all types
#[derive(Clone, Debug)]
pub struct Session {
    pub alerts: Option<String>,
    pub attached: Option<usize>,
    pub activity: Option<Duration>,
    pub created: Option<Duration>,
    pub format: Option<String>,
    pub last_attached: Option<Duration>,
    pub group: Option<String>,
    pub group_size: Option<String>,
    pub group_list: Option<String>,
    pub grouped: Option<String>,
    pub id: Option<usize>,
    pub many_attached: Option<String>,
    pub name: Option<String>,
    pub stack: Option<String>,
    pub windows: Option<usize>,
}


impl Default for Session {
    fn default() -> Self {
        Session {
            alerts: None,
            attached: None,
            activity: None,
            created: None,
            format: None,
            last_attached: None,
            group: None,
            group_size: None,
            group_list: None,
            grouped: None,
            id: None,
            many_attached: None,
            name: None,
            stack: None,
            windows: None,
        }
    }
}


impl Session {
    pub fn new() -> Session {
        Default::default()
    }


    // XXX: mb deserialize?
    // XXX: mb callback
    pub fn parse(session_str: &str) -> Result<Session, TmuxInterfaceError> {
        let regex_str = format!("^{}$", SESSION_VARS_REGEX_VEC.iter()
                                .map(|t| t.1).collect::<Vec<&str>>().join(SESSION_VARS_SEPARATOR));
        let regex = Regex::new(&regex_str)?;
        let caps = regex.captures(session_str).unwrap();
        let mut session = Session::new();

        // XXX: optimize?
        if let Some(alerts) = caps.get(1) {
            session.alerts = Some(alerts.as_str().parse()?);
        }
        if let Some(attached) = caps.get(2) {
            session.attached = Some(attached.as_str().parse()?);
        }
        if let Some(activity) = caps.get(3) {
            session.activity = Some(Duration::from_millis(activity.as_str().parse()?));
        }
        if let Some(created) = caps.get(4) {
            session.created = Some(Duration::from_millis(created.as_str().parse()?));
        }
        if let Some(format) = caps.get(5) {
            session.format = Some(format.as_str().parse()?);
        }
        if let Some(last_attached) = caps.get(6) {
            session.last_attached = Some(Duration::from_millis(last_attached.as_str().parse()?));
        }
        if let Some(group) = caps.get(7) {
            session.group = Some(group.as_str().parse()?);
        }
        if let Some(group_size) = caps.get(8) {
            session.group_size = Some(group_size.as_str().parse()?);
        }
        if let Some(group_list) = caps.get(9) {
            session.group_list = Some(group_list.as_str().parse()?);
        }
        if let Some(grouped) = caps.get(10) {
            session.grouped = Some(grouped.as_str().parse()?);
        }
        if let Some(id) = caps.get(11) {
            session.id = Some(id.as_str().parse()?);
        }
        if let Some(many_attached) = caps.get(12) {
            session.many_attached = Some(many_attached.as_str().parse()?);
        }
        if let Some(name) = caps.get(13) {
            session.name = Some(name.as_str().parse()?);
        }
        if let Some(stack) = caps.get(14) {
            session.stack = Some(stack.as_str().parse()?);
        }
        if let Some(windows) = caps.get(15) {
            session.windows = Some(windows.as_str().parse()?);
        }
        Ok(session)
    }
}