1
//! SSH keys tab: the user's public keys, from `list-ssh-keys`.
2

            
3
use ratatui::layout::Constraint;
4
use scripting::nomiscript::{Value, format_value};
5

            
6
use crate::tabs::plist::plist_text;
7
use crate::tabs::select_list::{SelectList, SelectRow};
8

            
9
/// One registered public key.
10
#[derive(Debug, Clone, PartialEq, Eq)]
11
pub struct SshKeyRow {
12
    pub id: String,
13
    pub key_type: String,
14
    pub fingerprint: String,
15
    pub comment: String,
16
    /// The day the key was added.
17
    pub added: String,
18
}
19

            
20
/// The `list-ssh-keys` view: exactly the keys the user holds.
21
pub type SshKeysTab = SelectList<SshKeyRow>;
22

            
23
impl SelectRow for SshKeyRow {
24
    const HEADERS: &'static [&'static str] = &["Type", "Fingerprint", "Comment", "Added"];
25
    const WIDTHS: &'static [Constraint] = &[
26
        Constraint::Length(12),
27
        Constraint::Length(52),
28
        Constraint::Fill(1),
29
        Constraint::Length(10),
30
    ];
31

            
32
32
    fn parse(plist: &Value) -> Result<Self, String> {
33
1
        let malformed = || format!("malformed ssh key: {}", format_value(plist));
34
156
        let text = |name| plist_text(plist, name).ok_or_else(malformed);
35
32
        let created = text(":created-at")?;
36
        Ok(Self {
37
31
            id: text(":id")?,
38
31
            key_type: text(":key-type")?,
39
31
            fingerprint: text(":fingerprint")?,
40
31
            comment: text(":name")?,
41
31
            added: created.get(..10).unwrap_or(&created).to_string(),
42
        })
43
32
    }
44

            
45
12
    fn key(&self) -> &str {
46
12
        &self.fingerprint
47
12
    }
48

            
49
11
    fn has_key(&self, key: &str) -> bool {
50
11
        self.fingerprint == key || self.id == key
51
11
    }
52

            
53
1
    fn cells(&self) -> Vec<String> {
54
1
        vec![
55
1
            self.key_type.clone(),
56
1
            self.fingerprint.clone(),
57
1
            self.comment.clone(),
58
1
            self.added.clone(),
59
        ]
60
1
    }
61
}
62

            
63
#[cfg(test)]
64
mod tests;