Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lib.rs
- use std::str::FromStr;
- use escpos::printer::Printer;
- use escpos::driver::*;
- use escpos::utils::JustifyMode;
- use escpos::utils::UnderlineMode;
- pub enum Operation {
- Smoothing(bool),
- Bold(bool),
- Justify(JustifyMode),
- Underline(UnderlineMode),
- Writeln(String),
- Feed,
- Print,
- Close,
- Unknown
- }
- // Implement FromStr for Operation to handle string inputs
- impl FromStr for Operation {
- type Err = String; // Define the error type
- fn from_str(input: &str) -> Result<Self, Self::Err> {
- match input.to_lowercase().as_str() {
- "feed" => Ok(Operation::Feed),
- "writeln" => Ok(Operation::Writeln("default data".to_string())), // default argument
- "justify_left" => Ok(Operation::Justify(JustifyMode::LEFT)), // default to Left justify
- "justify_center" => Ok(Operation::Justify(JustifyMode::CENTER)),
- "justify_right" => Ok(Operation::Justify(JustifyMode::RIGHT)),
- "underline_single" => Ok(Operation::Underline(UnderlineMode::Single)), // default argument
- "bold" => Ok(Operation::Bold(true)),
- "smoothing" => Ok(Operation::Smoothing(true)),
- "close" => Ok(Operation::Close),
- _ => Err(format!("Invalid operation: {}", input)),
- }
- }
- }
- impl From<(&str, &str)> for Operation {
- fn from(tuple: (&str, &str)) -> Self {
- match tuple.0 {
- "bold" => Operation::Bold(tuple.1.parse().unwrap_or(false)),
- "writeln" => Operation::Writeln(tuple.1.to_string()),
- "feed" => Operation::Feed,
- "smoothing" => Operation::Smoothing(tuple.1.parse().unwrap_or(false)),
- "justify_left" => Operation::Justify(JustifyMode::LEFT), // default to Left justify
- "justify_center" => Operation::Justify(JustifyMode::CENTER),
- "justify_right" => Operation::Justify(JustifyMode::RIGHT),
- "underline_single" => Operation::Underline(UnderlineMode::Single), // default argument
- "print" => Operation::Print,
- "close" => Operation::Close,
- _ => Operation::Unknown,
- }
- }
- }
- pub struct PrinterWrapper<'a> {
- printer: &'a mut Printer<UsbDriver>,
- }
- impl<'a> PrinterWrapper<'a> {
- pub fn new(printer: &'a mut Printer<UsbDriver>) -> Self {
- Self { printer }
- }
- pub fn execute(&mut self, operation: Operation) {
- match operation {
- Operation::Bold(data) => {
- if let Err(e) = self.printer.bold(data) {
- eprintln!("Error executing Bold: {:?}", e);
- }
- },
- Operation::Writeln(data) => {
- if let Err(e) = self.printer.writeln(data.as_str()) {
- eprintln!("Error executing Writeln: {:?}", e);
- }
- },
- Operation::Feed => {
- if let Err(e) = self.printer.feed() {
- eprintln!("Error executing Feed: {:?}", e);
- }
- },
- Operation::Smoothing(data) => {
- if let Err(e) = self.printer.smoothing(data) {
- eprintln!("Error executing Smoothing: {:?}", e);
- }
- }
- Operation::Justify(mode) => {
- if let Err(e) = self.printer.justify(mode) {
- eprintln!("Error executing Justify: {:?}", e);
- }
- },
- Operation::Underline(data) => {
- if let Err(e) = self.printer.underline(data) {
- eprintln!("Error executing Underline: {:?}", e);
- }
- },
- Operation::Print => {
- if let Err(e) = self.printer.print() {
- eprintln!("Error executing Print: {:?}", e);
- }
- },
- Operation::Close => {
- if let Err(e) = self.printer.cut() {
- eprintln!("Error executing Close: {:?}", e);
- }
- },
- Operation::Unknown => {
- eprintln!("Unknown operation");
- }
- }
- }
- }
- #main.rs
- use std::time::Duration;
- use escpos::printer::Printer;
- use escpos::printer_options::PrinterOptions;
- use escpos::utils::*;
- use escpos::{driver::*, errors::Result};
- use escpos_test::{PrinterWrapper, Operation};
- fn main() -> Result<()> {
- let operations_received = vec![
- ("bold", "true"),
- ("writeln", "Hello world new new"),
- ("feed", ""),
- ("smoothing", "true"),
- ("justify_center", ""),
- ("underline_single", ""),
- ("print", ""),
- ];
- let driver = UsbDriver::open(0x0416,0x5011, None).unwrap();
- let mut binding = Printer::new(driver, Protocol::default(), Some(PrinterOptions::default()));
- let printer = binding
- .debug_mode(Some(DebugMode::Dec))
- .init();
- let mut printer = PrinterWrapper::new(printer?);
- for (operation, data) in operations_received {
- let operation = Operation::from((operation, data));
- printer.execute(operation);
- }
- printer.execute(Operation::Close);
- Ok(())
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement