Advertisement
Wuelfhis

PrintServer

Nov 7th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 5.36 KB | None | 0 0
  1. #lib.rs
  2.  
  3. use std::str::FromStr;
  4. use escpos::printer::Printer;
  5. use escpos::driver::*;
  6. use escpos::utils::JustifyMode;
  7. use escpos::utils::UnderlineMode;
  8.  
  9. pub enum Operation {
  10.     Smoothing(bool),
  11.     Bold(bool),
  12.     Justify(JustifyMode),
  13.     Underline(UnderlineMode),
  14.     Writeln(String),
  15.     Feed,
  16.     Print,
  17.     Close,
  18.     Unknown
  19. }
  20.  
  21. // Implement FromStr for Operation to handle string inputs
  22. impl FromStr for Operation {
  23.     type Err = String; // Define the error type
  24.  
  25.     fn from_str(input: &str) -> Result<Self, Self::Err> {
  26.         match input.to_lowercase().as_str() {
  27.             "feed" => Ok(Operation::Feed),
  28.             "writeln" => Ok(Operation::Writeln("default data".to_string())), // default argument
  29.             "justify_left" => Ok(Operation::Justify(JustifyMode::LEFT)), // default to Left justify
  30.             "justify_center" => Ok(Operation::Justify(JustifyMode::CENTER)),
  31.             "justify_right" => Ok(Operation::Justify(JustifyMode::RIGHT)),
  32.             "underline_single" => Ok(Operation::Underline(UnderlineMode::Single)), // default argument
  33.             "bold" => Ok(Operation::Bold(true)),
  34.             "smoothing" => Ok(Operation::Smoothing(true)),
  35.             "close" => Ok(Operation::Close),
  36.             _ => Err(format!("Invalid operation: {}", input)),
  37.         }
  38.     }
  39. }
  40.  
  41. impl From<(&str, &str)> for Operation {
  42.     fn from(tuple: (&str, &str)) -> Self {
  43.         match tuple.0 {
  44.             "bold" => Operation::Bold(tuple.1.parse().unwrap_or(false)),
  45.             "writeln" => Operation::Writeln(tuple.1.to_string()),
  46.             "feed" => Operation::Feed,
  47.             "smoothing" => Operation::Smoothing(tuple.1.parse().unwrap_or(false)),
  48.             "justify_left" => Operation::Justify(JustifyMode::LEFT), // default to Left justify
  49.             "justify_center" => Operation::Justify(JustifyMode::CENTER),
  50.             "justify_right" => Operation::Justify(JustifyMode::RIGHT),
  51.             "underline_single" => Operation::Underline(UnderlineMode::Single), // default argument
  52.             "print" => Operation::Print,
  53.             "close" => Operation::Close,
  54.             _ => Operation::Unknown,
  55.         }
  56.     }
  57. }
  58.  
  59. pub struct PrinterWrapper<'a> {
  60.    printer: &'a mut Printer<UsbDriver>,
  61. }
  62.  
  63. impl<'a> PrinterWrapper<'a> {
  64.     pub fn new(printer: &'a mut Printer<UsbDriver>) -> Self {
  65.            Self { printer }
  66.        }
  67.  
  68.    pub fn execute(&mut self, operation: Operation) {
  69.            match operation {
  70.                Operation::Bold(data) => {
  71.                    if let Err(e) = self.printer.bold(data) {
  72.                        eprintln!("Error executing Bold: {:?}", e);
  73.                    }
  74.                },
  75.                Operation::Writeln(data) => {
  76.                    if let Err(e) = self.printer.writeln(data.as_str()) {
  77.                        eprintln!("Error executing Writeln: {:?}", e);
  78.                    }
  79.                },
  80.                Operation::Feed => {
  81.                    if let Err(e) = self.printer.feed() {
  82.                        eprintln!("Error executing Feed: {:?}", e);
  83.                    }
  84.                },
  85.                Operation::Smoothing(data) => {
  86.                    if let Err(e) = self.printer.smoothing(data) {
  87.                        eprintln!("Error executing Smoothing: {:?}", e);
  88.                    }
  89.                }
  90.                Operation::Justify(mode) => {
  91.                    if let Err(e) = self.printer.justify(mode) {
  92.                        eprintln!("Error executing Justify: {:?}", e);
  93.                    }                
  94.                },
  95.                Operation::Underline(data) => {
  96.                    if let Err(e) = self.printer.underline(data) {
  97.                        eprintln!("Error executing Underline: {:?}", e);
  98.                    }
  99.                },
  100.                Operation::Print => {
  101.                    if let Err(e) = self.printer.print() {
  102.                        eprintln!("Error executing Print: {:?}", e);
  103.                    }
  104.                },
  105.                Operation::Close => {
  106.                    if let Err(e) = self.printer.cut() {
  107.                        eprintln!("Error executing Close: {:?}", e);
  108.                    }
  109.                },
  110.                Operation::Unknown => {
  111.                    eprintln!("Unknown operation");
  112.                }
  113.            }
  114.    }
  115. }
  116.  
  117. #main.rs
  118.  
  119. use std::time::Duration;
  120. use escpos::printer::Printer;
  121. use escpos::printer_options::PrinterOptions;
  122. use escpos::utils::*;
  123. use escpos::{driver::*, errors::Result};
  124. use escpos_test::{PrinterWrapper, Operation};
  125.  
  126.  
  127.  
  128. fn main() -> Result<()> {
  129.    let operations_received = vec![
  130.        ("bold", "true"),
  131.        ("writeln", "Hello world new new"),
  132.        ("feed", ""),
  133.        ("smoothing", "true"),
  134.        ("justify_center", ""),
  135.        ("underline_single", ""),
  136.        ("print", ""),
  137.    ];
  138.    let driver = UsbDriver::open(0x0416,0x5011, None).unwrap();
  139.    let mut binding = Printer::new(driver, Protocol::default(), Some(PrinterOptions::default()));
  140.    let printer = binding
  141.        .debug_mode(Some(DebugMode::Dec))
  142.        .init();
  143.    let mut printer = PrinterWrapper::new(printer?);
  144.    for (operation, data) in operations_received {
  145.        let operation = Operation::from((operation, data));
  146.        printer.execute(operation);
  147.    }
  148.    printer.execute(Operation::Close);
  149.  
  150.    Ok(())
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement