use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct DeviceId(String);
impl DeviceId {
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for DeviceId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum DeviceKind {
Mount,
Camera,
Focuser,
FilterWheel,
Switch,
Other,
}
impl fmt::Display for DeviceKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Mount => "mount",
Self::Camera => "camera",
Self::Focuser => "focuser",
Self::FilterWheel => "filter wheel",
Self::Switch => "switch",
Self::Other => "other",
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct DeviceInfo {
pub id: DeviceId,
pub kind: DeviceKind,
pub name: String,
pub model: Option<String>,
pub firmware: Option<String>,
pub driver: Option<String>,
}
impl DeviceInfo {
pub fn new(id: DeviceId, kind: DeviceKind, name: impl Into<String>) -> Self {
Self {
id,
kind,
name: name.into(),
model: None,
firmware: None,
driver: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DeviceName(String);
impl DeviceName {
pub fn new(name: impl Into<String>) -> Self {
Self(name.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for DeviceName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct FirmwareVersion(pub u32);
impl FirmwareVersion {
pub const VERIFY_THRESHOLD: u32 = 2582;
pub fn requires_verify(&self) -> bool {
self.0 > Self::VERIFY_THRESHOLD
}
}
impl fmt::Display for FirmwareVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::*;
#[test]
fn device_kind_roundtrip() {
for kind in [
DeviceKind::Mount,
DeviceKind::Camera,
DeviceKind::Focuser,
DeviceKind::FilterWheel,
DeviceKind::Switch,
DeviceKind::Other,
] {
let json = serde_json::to_string(&kind).unwrap();
let back: DeviceKind = serde_json::from_str(&json).unwrap();
assert_eq!(kind, back);
}
}
proptest! {
#[test]
fn device_id_roundtrip(s in ".{0,64}") {
let id = DeviceId::new(s.clone());
prop_assert_eq!(id.as_str(), &s);
let json = serde_json::to_string(&id).unwrap();
let back: DeviceId = serde_json::from_str(&json).unwrap();
prop_assert_eq!(id, back);
}
#[test]
fn device_name_roundtrip(s in ".{0,64}") {
let name = DeviceName::new(s.clone());
let json = serde_json::to_string(&name).unwrap();
let back: DeviceName = serde_json::from_str(&json).unwrap();
prop_assert_eq!(name, back);
}
#[test]
fn device_info_roundtrip(id in ".{0,32}", name in ".{0,32}", model in proptest::option::of(".{0,16}")) {
let mut info = DeviceInfo::new(DeviceId::new(id), DeviceKind::Camera, name);
info.model = model;
let json = serde_json::to_string(&info).unwrap();
let back: DeviceInfo = serde_json::from_str(&json).unwrap();
prop_assert_eq!(info, back);
}
#[test]
fn firmware_version_roundtrip(v in any::<u32>()) {
let fw = FirmwareVersion(v);
let json = serde_json::to_string(&fw).unwrap();
let back: FirmwareVersion = serde_json::from_str(&json).unwrap();
prop_assert_eq!(fw, back);
}
#[test]
fn requires_verify_matches_threshold(v in any::<u32>()) {
prop_assert_eq!(
FirmwareVersion(v).requires_verify(),
v > FirmwareVersion::VERIFY_THRESHOLD
);
}
}
}