Skip to main content

gpg_tui/app/
mode.rs

1use std::fmt::{Display, Formatter, Result as FmtResult};
2use std::str::FromStr;
3
4/// Application mode.
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6pub enum Mode {
7	/// Normal mode.
8	Normal,
9	/// Visual mode.
10	/// (Disables the mouse capture)
11	Visual,
12	/// Copy mode.
13	/// (Makes it easier to copy values)
14	Copy,
15}
16
17impl Display for Mode {
18	fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
19		write!(f, "-- {} --", format!("{self:?}").to_lowercase())
20	}
21}
22
23impl FromStr for Mode {
24	type Err = ();
25	fn from_str(s: &str) -> Result<Self, Self::Err> {
26		match s.to_lowercase().as_str() {
27			"normal" | "n" => Ok(Self::Normal),
28			"visual" | "v" => Ok(Self::Visual),
29			"copy" | "c" => Ok(Self::Copy),
30			_ => Err(()),
31		}
32	}
33}
34
35#[cfg(test)]
36mod tests {
37	use super::*;
38	use pretty_assertions::assert_eq;
39	#[test]
40	fn test_app_mode() -> Result<(), ()> {
41		let mode = Mode::from_str("normal")?;
42		assert_eq!(Mode::Normal, mode);
43		assert_eq!(String::from("-- normal --"), mode.to_string());
44		let mode = Mode::from_str("visual")?;
45		assert_eq!(Mode::Visual, mode);
46		assert_eq!(String::from("-- visual --"), mode.to_string());
47		let mode = Mode::from_str("copy")?;
48		assert_eq!(Mode::Copy, mode);
49		assert_eq!(String::from("-- copy --"), mode.to_string());
50		Ok(())
51	}
52}