use proton_sdk::error::{ProtonError, Result};
use proton_sdk::ids::{DeviceUid, NodeUid, ShareId};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum DeviceType {
Windows = 1,
MacOs = 2,
Linux = 3,
}
impl DeviceType {
pub fn as_i32(self) -> i32 {
self as i32
}
pub(crate) fn from_raw(value: i32) -> Result<Self> {
match value {
1 => Ok(Self::Windows),
2 => Ok(Self::MacOs),
3 => Ok(Self::Linux),
other => Err(ProtonError::invalid_operation(format!(
"unsupported device type {other}"
))),
}
}
}
#[derive(Debug)]
pub struct Device {
pub uid: DeviceUid,
pub device_type: DeviceType,
pub name: Result<String>,
pub root_folder_uid: NodeUid,
pub creation_time: i64,
pub last_sync_time: Option<i64>,
pub share_id: ShareId,
}
#[derive(Debug, Clone)]
pub(crate) struct DeviceMetadata {
pub uid: DeviceUid,
pub device_type: DeviceType,
pub root_folder_uid: NodeUid,
pub creation_time: i64,
pub last_sync_time: Option<i64>,
pub has_deprecated_name: bool,
pub share_id: ShareId,
}
impl DeviceMetadata {
pub(crate) fn into_device(self, name: Result<String>) -> Device {
Device {
uid: self.uid,
device_type: self.device_type,
name,
root_folder_uid: self.root_folder_uid,
creation_time: self.creation_time,
last_sync_time: self.last_sync_time,
share_id: self.share_id,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn device_type_round_trips_through_the_wire_value() {
for device_type in [DeviceType::Windows, DeviceType::MacOs, DeviceType::Linux] {
assert_eq!(
DeviceType::from_raw(device_type.as_i32()).expect("known device type"),
device_type
);
}
assert!(DeviceType::from_raw(0).is_err());
assert!(DeviceType::from_raw(4).is_err());
}
}