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
|
use std::{
collections::{hash_map::Entry, HashMap},
fmt::Display,
iter::IntoIterator,
time::Duration,
};
use serde::{Deserialize, Serialize};
pub use config::Config;
pub use history::History;
pub use tmux::Tmux;
mod config;
mod history;
mod ssh;
mod tmux;
pub trait SessionSource {
type Error: std::error::Error;
type Iter: IntoIterator<Item = Session>;
fn sessions(&self) -> Result<Self::Iter, Self::Error>;
fn update(
&self,
mut hash_map: HashMap<String, Session>,
) -> Result<HashMap<String, Session>, Self::Error> {
for session in self.sessions()? {
match hash_map.entry(session.name.to_owned()) {
Entry::Occupied(mut o) if &session > o.get() => {
o.insert(session);
}
Entry::Vacant(v) => {
v.insert(session);
}
_ => {}
}
}
Ok(hash_map)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum State {
Discovered,
Connected,
Updated,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Session {
pub state: State,
#[serde(with = "epoch_timestamp")]
pub timestamp: Duration,
pub name: String,
}
mod epoch_timestamp {
use std::time::Duration;
use serde::{self, Deserialize, Deserializer, Serializer};
pub fn serialize<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u64(duration.as_secs())
}
pub fn deserialize<'de, D>(d: D) -> Result<Duration, D::Error>
where
D: Deserializer<'de>,
{
u64::deserialize(d).map(Duration::from_secs)
}
}
impl Display for Session {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
|