use smol_str::SmolStr;
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(default)
)]
#[cfg_attr(
feature = "quickcheck",
derive(::quickcheck_richderive::Arbitrary),
quickcheck(arbitrary = "crate::quickcheck_helpers::composite::capture_device")
)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Device {
make: SmolStr,
model: SmolStr,
}
impl Default for Device {
#[cfg_attr(not(tarpaulin), inline(always))]
fn default() -> Self {
Self::new()
}
}
impl Device {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new() -> Self {
Self {
make: SmolStr::new_static(""),
model: SmolStr::new_static(""),
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn make(&self) -> &str {
self.make.as_str()
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn model(&self) -> &str {
self.model.as_str()
}
#[must_use]
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_make(mut self, make: impl Into<SmolStr>) -> Self {
self.make = make.into();
self
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_make(&mut self, make: impl Into<SmolStr>) -> &mut Self {
self.make = make.into();
self
}
#[must_use]
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_model(mut self, model: impl Into<SmolStr>) -> Self {
self.model = model.into();
self
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_model(&mut self, model: impl Into<SmolStr>) -> &mut Self {
self.model = model.into();
self
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn is_empty(&self) -> bool {
self.make.is_empty() && self.model.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_is_all_empty() {
let d = Device::new();
assert_eq!(d.make(), "");
assert_eq!(d.model(), "");
assert!(d.is_empty());
}
#[test]
fn default_matches_new() {
assert_eq!(Device::default(), Device::new());
}
#[test]
fn builder_chain_populates() {
let d = Device::new().with_make("Apple").with_model("iPhone 15 Pro");
assert_eq!(d.make(), "Apple");
assert_eq!(d.model(), "iPhone 15 Pro");
assert!(!d.is_empty());
}
#[test]
fn setters_mutate_in_place() {
let mut d = Device::new();
d.set_make("Sony");
d.set_model("ILCE-7M4");
assert_eq!(d.make(), "Sony");
assert_eq!(d.model(), "ILCE-7M4");
assert!(!d.is_empty());
}
#[test]
fn is_empty_partial() {
let m = Device::new().with_make("Apple");
assert!(!m.is_empty());
let n = Device::new().with_model("ILCE-7M4");
assert!(!n.is_empty());
}
}