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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::borrow::Cow;
use std::ops::Deref;
use std::ops::DerefMut;
use std::path::Path;

use crate::device::Device;
use crate::error::Error;

#[cfg(any(all(target_os = "macos", target_os = "linux"),
          all(target_os = "linux", target_os = "windows"),
          all(target_os = "windows", target_os = "macos")))]
compile_error!("cfg.target_os conflict");

#[cfg_attr(target_os = "macos", path = "mac.rs")]
#[cfg_attr(target_os = "linux", path = "linux.rs")]
#[cfg_attr(target_os = "windows", path = "win.rs")]
pub mod volume;

mod methods;
pub use methods::*;


// TODO: MountError for this module


pub trait Volume {
	/// This volume's path.
	fn path(&self) -> Cow<'_, Path>;
}

pub trait Unmount {
	/// Unmount this volume. Blocking.
	fn unmount_blocking(&self) -> Result<(), Error>;
}

pub trait UnmountAsync {
	/// Unmount this volume.
	fn unmount(&self) -> impl std::future::Future<Output = Result<(), Error>>;
}

pub trait Mount {
	/// Mount this volume. Blocking.
	fn mount_blocking(&self) -> Result<(), Error>;
}

pub trait MountAsync {
	fn mount(&self) -> impl std::future::Future<Output = Result<(), Error>>;
}


impl Mount for Device {
	fn mount_blocking(&self) -> Result<(), Error> {
		use crate::interface::blocking::Out;
		use crate::device::command::Command;

		self.interface()?.send_cmd(Command::Datadisk)?;
		Ok(())
	}
}

impl MountAsync for Device {
	async fn mount(&self) -> Result<(), Error> { self.interface()?.mount().await }
}


impl<T> MountAsync for T where T: crate::interface::r#async::Out {
	async fn mount(&self) -> Result<(), Error> {
		self.send_cmd(crate::device::command::Command::Datadisk).await?;
		Ok(())
	}
}

impl<T> Mount for T where T: crate::interface::blocking::Out {
	fn mount_blocking(&self) -> Result<(), Error> {
		self.send_cmd(crate::device::command::Command::Datadisk)?;
		Ok(())
	}
}


impl<T> UnmountAsync for T where T: crate::interface::r#async::Out {
	async fn unmount(&self) -> Result<(), Error> {
		self.send_cmd(crate::device::command::Command::Datadisk).await?;
		Ok(())
	}
}

impl<T> Unmount for T where T: crate::interface::blocking::Out {
	fn unmount_blocking(&self) -> Result<(), Error> {
		self.send_cmd(crate::device::command::Command::Datadisk)?;
		Ok(())
	}
}


pub struct MountedDevice {
	pub device: Device,
	pub handle: MountHandle,
}

impl Unmount for MountedDevice {
	#[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(dev = self.info().serial_number(), mount = self.handle.volume().path().as_ref().display().to_string())))]
	fn unmount_blocking(&self) -> Result<(), Error> {
		<volume::Volume as Unmount>::unmount_blocking(&self.handle.volume)
	}
}

impl UnmountAsync for MountedDevice where volume::Volume: UnmountAsync {
	#[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(dev = self.info().serial_number(), mount = self.handle.volume().path().as_ref().display().to_string())))]
	fn unmount(&self) -> impl std::future::Future<Output = Result<(), Error>> {
		<volume::Volume as UnmountAsync>::unmount(&self.handle.volume)
	}
}

impl MountedDevice {
	pub fn new(device: Device, handle: MountHandle) -> Self { Self { device, handle } }
}

impl Deref for MountedDevice {
	type Target = Device;
	fn deref(&self) -> &Self::Target { &self.device }
}

impl DerefMut for MountedDevice {
	fn deref_mut(&mut self) -> &mut Self::Target { &mut self.device }
}


pub struct MountHandle {
	volume: volume::Volume,
	pub unmount_on_drop: bool,
}

impl MountHandle {
	pub fn new(volume: volume::Volume, unmount_on_drop: bool) -> Self {
		Self { volume,
		       unmount_on_drop }
	}

	pub fn path(&self) -> Cow<'_, Path> { self.volume.path() }
	pub fn volume(&self) -> &volume::Volume { &self.volume }

	pub fn unmount(mut self) {
		self.unmount_on_drop = true;
		drop(self)
	}
}

impl Drop for MountHandle {
	fn drop(&mut self) {
		if self.unmount_on_drop {
			trace!("Unmounting {} by drop", self.volume);
			let _ = self.volume
			            .unmount_blocking()
			            .map_err(|err| {
				            error!("{err}");
				            info!("Please press 'A' on the Playdate to exit Data Disk mode.");
			            })
			            .ok();
		}
	}
}