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
use std::path::PathBuf;

mod serial;
pub use serial::*;


#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
	/// DATA / COMM
	Data,
	/// MASS_STORAGE
	Storage,
}

impl std::fmt::Display for Mode {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "{}", match self {
			Mode::Data => "D",
			Mode::Storage => "S",
		})
	}
}


pub struct Device {
	pub(crate) serial: SerialNumber,

	/// Detected interface
	pub(crate) mode: Mode,

	/// Mount point
	pub(crate) volume: Option<PathBuf>,

	/// Path of fd, cu or tty
	pub(crate) tty: Option<PathBuf>,
}

impl std::fmt::Debug for Device {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		let mut res = f.debug_struct(&self.serial.to_string());
		res.field("mode", &self.mode);
		res.field("dev", &self.tty);
		res.field("path", &self.volume);

		match &self.mode {
			Mode::Storage => res.field("mounted", &self.mounted()),
			Mode::Data => &mut res,
		};

		res.finish()
	}
}

impl std::fmt::Display for Device {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "{}({})", self.serial, self.mode)
	}
}


struct DeviceFd {
	cu: PathBuf,
}

impl std::fmt::Debug for DeviceFd {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		let mut res = f.debug_struct("Fd");
		res.field("cu", &self.cu);
		res.finish()
	}
}

impl std::fmt::Display for DeviceFd {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.cu.display()) }
}