Skip to main content

gpg_tui/app/
selection.rs

1use clap::ValueEnum;
2use std::fmt::{Display, Formatter, Result as FmtResult};
3
4/// Application property to copy to clipboard.
5#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
6pub enum Selection {
7	/// One of the selected rows of the keys table
8	#[clap(aliases = ["row_1", "row1", "1"])]
9	Row1,
10	/// The other selected row of the keys table
11	#[clap(aliases = ["row_2", "row2", "2"])]
12	Row2,
13	/// Exported key.
14	Key,
15	/// ID of the selected key.
16	#[clap(aliases = ["id", "key_id", "keyid"])]
17	KeyId,
18	/// Fingerprint of the selected key.
19	#[clap(aliases = ["fingerprint", "key_fingerprint", "keyfingerprint", "fpr", "key_fpr", "keyfpr"])]
20	KeyFingerprint,
21	/// User ID of the selected key.
22	#[clap(aliases = ["user", "user_id", "userid", "user-id", "key_user_id", "keyuserid"])]
23	UserId,
24}
25
26impl Display for Selection {
27	fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
28		write!(
29			f,
30			"{}",
31			match self {
32				Self::Row1 => "table row (1)".to_string(),
33				Self::Row2 => "table row (2)".to_string(),
34				Self::Key => String::from("exported key"),
35				Self::KeyId => String::from("key ID"),
36				Self::KeyFingerprint => String::from("key fingerprint"),
37				Self::UserId => String::from("user ID"),
38			}
39		)
40	}
41}
42
43#[cfg(test)]
44mod tests {
45	use super::*;
46	use pretty_assertions::assert_eq;
47	#[test]
48	fn test_app_clipboard() -> Result<(), String> {
49		let copy_type = Selection::from_str("row1", true)?;
50		assert_eq!(Selection::Row1, copy_type);
51		assert_eq!(String::from("table row (1)"), copy_type.to_string());
52		let copy_type = Selection::from_str("row2", true)?;
53		assert_eq!(Selection::Row2, copy_type);
54		assert_eq!(String::from("table row (2)"), copy_type.to_string());
55		let copy_type = Selection::from_str("key", true)?;
56		assert_eq!(Selection::Key, copy_type);
57		assert_eq!(String::from("exported key"), copy_type.to_string());
58		let copy_type = Selection::from_str("key_id", true)?;
59		assert_eq!(Selection::KeyId, copy_type);
60		assert_eq!(String::from("key ID"), copy_type.to_string());
61		let copy_type = Selection::from_str("key_fingerprint", true)?;
62		assert_eq!(Selection::KeyFingerprint, copy_type);
63		assert_eq!(String::from("key fingerprint"), copy_type.to_string());
64		let copy_type = Selection::from_str("key_user_id", true)?;
65		assert_eq!(Selection::UserId, copy_type);
66		assert_eq!(String::from("user ID"), copy_type.to_string());
67		Ok(())
68	}
69}