#[derive(Debug, Clone)]
pub struct Print {
pub printable: bool,
pub copies: Option<u32>,
}
impl Print {
#[must_use]
pub fn new() -> Self {
Self {
printable: true,
copies: None,
}
}
#[must_use]
pub fn disabled() -> Self {
Self {
printable: false,
copies: None,
}
}
#[must_use]
pub fn with_copies(mut self, copies: u32) -> Self {
self.copies = Some(copies);
self
}
#[must_use]
pub fn to_xml_string(&self) -> String {
let copies_attr = match self.copies {
Some(c) => format!(" Copies=\"{c}\""),
None => String::new(),
};
format!("<Print Printable=\"{}\"{copies_attr}/>", self.printable)
}
}
impl Default for Print {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_print_new() {
let p = Print::new();
assert!(p.printable);
assert!(p.copies.is_none());
let p2 = Print::default();
assert!(p2.printable);
}
#[test]
fn test_print_disabled_and_xml() {
let p = Print::disabled();
let xml = p.to_xml_string();
assert!(xml.contains("Printable=\"false\""));
let p3 = Print::new().with_copies(3);
let xml3 = p3.to_xml_string();
assert!(xml3.contains("Copies=\"3\""));
assert!(xml3.contains("Printable=\"true\""));
}
}