Struct CustomPrinter

Source
pub struct CustomPrinter { /* private fields */ }
Expand description

The main struct to construct printing commands and accomplish actual printing.

The APIs are designed to be able to concatenate one after the other.

§Examples

let mut printer = CustomPrinter::new("/dev/usb/lp0").unwrap();
printer
    .bit_image(
        "logo.bmp",
        BitImageMode::Dots24DoubleDensity
    )
    .unwrap()
    .cut_paper(CutType::PartialCut)
    .run()
    .unwrap()
    .bit_image(
        "greeting.bmp",
        BitImageMode::Dots24DoubleDensity
    )
    .unwrap()
    .cut_paper(CutType::TotalCut)
    .run()
    .unwrap();

Implementations§

Source§

impl CustomPrinter

Source

pub fn new(dev: &str) -> Result<Self, Error>

Create a new CustomPrinter with the device node dev.

NOTE: Device node dev must be readable and writable by current user.

§Examples
CustomPrinter::new("/dev/usb/lp0")
Examples found in repository?
examples/bitimage.rs (line 6)
3fn main() {
4    // Replace /dev/null with actual device node when the printer is connected
5    // e.g.: /dev/usb/lp0
6    let mut printer = CustomPrinter::new("/dev/null").unwrap();
7    printer
8        .bit_image(
9            "tests/data/Thermal_Test_Image.png",
10            BitImageMode::Dots24DoubleDensity,
11        )
12        .unwrap()
13        .cut_paper(CutType::TotalCut)
14        .run()
15        .unwrap();
16}
Source

pub fn bit_image( &mut self, path: &str, mode: BitImageMode, ) -> Result<&mut Self, Error>

Append commands for printing a bit image from path in mode. See BitImageMode for supported modes.

NOTE: Because opening and reading the image file may fail, so the return Self is wrapped in a Result and needs to be unwrapped before concatenating with other constructing functions.

§Examples
printer
    .bit_image(
        "tests/data/Thermal_Test_Image.png",
        BitImageMode::Dots24DoubleDensity
    )
    .unwrap();
Examples found in repository?
examples/bitimage.rs (lines 8-11)
3fn main() {
4    // Replace /dev/null with actual device node when the printer is connected
5    // e.g.: /dev/usb/lp0
6    let mut printer = CustomPrinter::new("/dev/null").unwrap();
7    printer
8        .bit_image(
9            "tests/data/Thermal_Test_Image.png",
10            BitImageMode::Dots24DoubleDensity,
11        )
12        .unwrap()
13        .cut_paper(CutType::TotalCut)
14        .run()
15        .unwrap();
16}
Source

pub fn cut_paper(&mut self, cut_type: CutType) -> &mut Self

Append a command for cutting the paper totally (CutType::TotalCut) or partially (CutType::PartialCut).

§Examples
printer.cut_paper(CutType::TotalCut);
Examples found in repository?
examples/bitimage.rs (line 13)
3fn main() {
4    // Replace /dev/null with actual device node when the printer is connected
5    // e.g.: /dev/usb/lp0
6    let mut printer = CustomPrinter::new("/dev/null").unwrap();
7    printer
8        .bit_image(
9            "tests/data/Thermal_Test_Image.png",
10            BitImageMode::Dots24DoubleDensity,
11        )
12        .unwrap()
13        .cut_paper(CutType::TotalCut)
14        .run()
15        .unwrap();
16}
Source

pub fn run(&mut self) -> Result<&mut Self, Error>

Run the constructed commands in the CustomPrinter.

The constructed commands will be cleared if the printing succeeds.

NOTE: Because writing to the device node may fail, so the return Self is wrapped in a Result and needs to be unwrapped before concatenating with other constructing functions.

§Examples
printer
    .bit_image(
        "tests/data/Thermal_Test_Image.png",
        BitImageMode::Dots24DoubleDensity
    )
    .unwrap()
    .cut_paper(CutType::TotalCut)
    .run()
    .unwrap();
Examples found in repository?
examples/bitimage.rs (line 14)
3fn main() {
4    // Replace /dev/null with actual device node when the printer is connected
5    // e.g.: /dev/usb/lp0
6    let mut printer = CustomPrinter::new("/dev/null").unwrap();
7    printer
8        .bit_image(
9            "tests/data/Thermal_Test_Image.png",
10            BitImageMode::Dots24DoubleDensity,
11        )
12        .unwrap()
13        .cut_paper(CutType::TotalCut)
14        .run()
15        .unwrap();
16}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.