Skip to main content

Barcode

Struct Barcode 

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

An encoded barcode, ready to render.

This is the type most callers work with. It pairs an encoded Symbol with convenience methods for the built-in renderers, while Barcode::render stays open to any Renderer implementation.

§Examples

use smart_package_tracker::{Barcode, RenderOptions, TrackingId};

let id = TrackingId::generate()?;
let barcode = Barcode::code128(&id)?;
let options = RenderOptions::default();

barcode.to_png_file("label.png", &options)?;
barcode.to_svg_file("label.svg", &options)?;

Implementations§

Source§

impl Barcode

Source

pub fn code128(data: impl AsRef<str>) -> Result<Self>

Available on crate feature code128 only.

Encode data as Code 128.

Accepts anything that borrows as a string, including TrackingId.

§Errors

Returns Error::EmptyPayload or Error::Unencodable.

Source

pub fn qr(data: impl AsRef<str>) -> Result<Self>

Available on crate feature qr only.

Encode data as a QR Code with default settings: medium error correction, and the smallest version that fits.

Use Barcode::qr_with to choose the error correction level or pin the version.

§Errors

Returns Error::EmptyPayload or Error::Unencodable.

Source

pub fn qr_with(encoder: Qr, data: impl AsRef<str>) -> Result<Self>

Available on crate feature qr only.

Encode data as a QR Code with an explicitly configured encoder.

§Examples
use smart_package_tracker::{Barcode, symbology::{Ecc, Qr, QrVersion}};

let barcode = Barcode::qr_with(
    Qr::new().ecc(Ecc::High).version(QrVersion::Fixed(6)),
    "PKG-9ED9285C",
)?;
assert_eq!(barcode.symbol().modules().width(), 41);
§Errors

Returns Error::EmptyPayload or Error::Unencodable.

Source

pub fn encode_with<S: Symbology + ?Sized>( symbology: &S, data: &str, ) -> Result<Self>

Encode data with any symbology.

§Errors

Propagates whatever the symbology reports.

Source

pub fn from_symbol(symbol: Symbol) -> Self

Wrap an already-encoded symbol.

Source

pub fn symbol(&self) -> &Symbol

The underlying symbol.

Source

pub fn payload(&self) -> &str

The payload this barcode encodes.

Source

pub fn kind(&self) -> SymbologyKind

Which symbology encoded it.

Source

pub fn render<R: Renderer>( &self, renderer: &R, options: &RenderOptions, ) -> Result<R::Output>

Render with an explicit renderer.

§Errors

Propagates renderer failures.

Source

pub fn to_png(&self, options: &RenderOptions) -> Result<Vec<u8>>

Available on crate feature png only.

Render to PNG bytes.

§Errors

Propagates renderer failures.

Source

pub fn to_svg(&self, options: &RenderOptions) -> Result<String>

Available on crate feature svg only.

Render to an SVG document.

§Errors

Propagates renderer failures.

Source

pub fn to_png_file( &self, path: impl AsRef<Path>, options: &RenderOptions, ) -> Result<()>

Available on crate features png and std only.

Render to PNG and write it to path.

§Errors

Propagates renderer failures and Error::Io.

Source

pub fn to_svg_file( &self, path: impl AsRef<Path>, options: &RenderOptions, ) -> Result<()>

Available on crate features std and svg only.

Render to SVG and write it to path.

§Errors

Propagates renderer failures and Error::Io.

Source

pub fn decode(&self) -> Result<String>

Available on crate feature code128 only.

Decode the barcode back into its payload from the module grid.

This does not simply return the stored payload: it reconstructs bar and space runs from the rendered module pattern and decodes those. A successful round trip is therefore evidence that the encoded geometry is correct.

This reads the symbol in memory. To read one out of an image, see scan.

§Errors

Returns Error::Decode if this is not a Code 128 barcode, or if the pattern is not valid Code 128. There is no QR decoder in this crate.

Trait Implementations§

Source§

impl Clone for Barcode

Source§

fn clone(&self) -> Barcode

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Barcode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Barcode

Source§

impl PartialEq for Barcode

Source§

fn eq(&self, other: &Barcode) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Barcode

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.