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
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::str::FromStr;

/// Application property to copy to clipboard.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum CopyType {
	/// Selected row of the keys table.
	TableRow(usize),
	/// Exported key.
	Key,
	/// ID of the selected key.
	KeyId,
	/// Fingerprint of the selected key.
	KeyFingerprint,
	/// User ID of the selected key.
	KeyUserId,
}

impl Display for CopyType {
	fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
		write!(
			f,
			"{}",
			match self {
				Self::TableRow(i) => format!("table row ({})", i),
				Self::Key => String::from("exported key"),
				Self::KeyId => String::from("key ID"),
				Self::KeyFingerprint => String::from("key fingerprint"),
				Self::KeyUserId => String::from("user ID"),
			}
		)
	}
}

impl FromStr for CopyType {
	type Err = ();
	fn from_str(s: &str) -> Result<Self, Self::Err> {
		match s {
			"row1" | "1" => Ok(Self::TableRow(1)),
			"row2" | "2" => Ok(Self::TableRow(2)),
			"key" => Ok(Self::Key),
			"key_id" | "id" => Ok(Self::KeyId),
			"key_fingerprint" | "fingerprint" => Ok(Self::KeyFingerprint),
			"key_user_id" | "user" => Ok(Self::KeyUserId),
			_ => Err(()),
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use pretty_assertions::assert_eq;
	#[test]
	fn test_app_clipboard() {
		let copy_type = CopyType::from_str("row1").unwrap();
		assert_eq!(CopyType::TableRow(1), copy_type);
		assert_eq!(String::from("table row (1)"), copy_type.to_string());
		let copy_type = CopyType::from_str("key").unwrap();
		assert_eq!(CopyType::Key, copy_type);
		assert_eq!(String::from("exported key"), copy_type.to_string());
		let copy_type = CopyType::from_str("key_id").unwrap();
		assert_eq!(CopyType::KeyId, copy_type);
		assert_eq!(String::from("key ID"), copy_type.to_string());
		let copy_type = CopyType::from_str("key_fingerprint").unwrap();
		assert_eq!(CopyType::KeyFingerprint, copy_type);
		assert_eq!(String::from("key fingerprint"), copy_type.to_string());
		let copy_type = CopyType::from_str("key_user_id").unwrap();
		assert_eq!(CopyType::KeyUserId, copy_type);
		assert_eq!(String::from("user ID"), copy_type.to_string());
	}
}