Skip to main content

QrCode

Struct QrCode 

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

A QR code as a renderable.

  • Charset::Blocks (the default; Charset::Braille draws the same, as Braille dots are too sparse to scan) draws two modules a cell with half blocks (▀▄█), so the code is square and size + 2 × quiet zone cells wide.
  • With colour, every cell is painted: the dark modules in #000000 and the light ones in #ffffff, whatever the terminal’s theme makes of its named colours (colours changes them), so the code reads on a dark or a light terminal.
  • Without colour, the glyphs draw the light modules in the terminal’s text colour, so on a dark background (the common case) the code reads dark on light. invert draws the dark modules instead, for a terminal with a light background.
  • Charset::Ascii (and an ASCII-only console) draws each module as two cells, ## or spaces, or as painted spaces with colour, so the code is twice as tall.
  • The quiet zone is 4 modules, as the specification asks (quiet_zone). Given less width, the quiet zone narrows to a module; a code that still does not fit is replaced by a note saying how many columns it needs, as a cut code cannot be read.
use rich::Console;
use rich_ext::qr::{ErrorCorrection, QrCode};

let code = QrCode::new("https://example.com").unwrap();
assert_eq!((code.size(), code.version()), (25, 2));
// Medium was asked for; quartile fits the same size, so it is used.
assert_eq!(code.error_correction(), ErrorCorrection::Quartile);
// 25 modules and 4 of quiet zone each side.
let wide = Console::builder().width(40).color_system(None).build();
assert!(wide.render_export(&code).lines().all(|l| l.chars().count() == 33));
// The quiet zone narrows to fit, down to a module.
let narrow = Console::builder().width(28).color_system(None).build();
assert!(narrow.render_export(&code).lines().all(|l| l.chars().count() == 27));
// Narrower still, a note.
let tiny = Console::builder().width(26).color_system(None).build();
assert_eq!(tiny.render_export(&code), "QR code needs 27 columns\n");

Implementations§

Source§

impl QrCode

Source

pub fn new(text: &str) -> Result<Self, QrError>

Source

pub fn with_error_correction( text: &str, ecc: ErrorCorrection, ) -> Result<Self, QrError>

text at ecc, or higher when the larger level fits the same size of code. Digits and upper-case text are packed tighter than other text.

Source

pub fn from_bytes(data: &[u8], ecc: ErrorCorrection) -> Result<Self, QrError>

Bytes at ecc (or higher, as with_error_correction).

Source

pub fn size(&self) -> usize

Modules along a side, without the quiet zone (21 to 177).

Source

pub fn version(&self) -> u8

The QR version, 1 to 40: the size is 17 + 4 × version.

Source

pub fn error_correction(&self) -> ErrorCorrection

The error correction the code was made with (it may be higher than asked for).

Source

pub fn module(&self, x: usize, y: usize) -> bool

Whether the module at column x, row y is dark; false outside the code.

Source

pub fn quiet_zone(self, modules: usize) -> Self

The light border around the code, in modules (default 4, as the specification asks; scanners often manage with 2).

Source

pub fn charset(self, charset: Charset) -> Self

Glyphs to draw with (default Charset::Auto: half blocks).

Source

pub fn invert(self, invert: bool) -> Self

Without colour, draw the dark modules rather than the light ones, for a terminal with a light background (default off).

Source

pub fn colours(self, dark: impl Into<String>, light: impl Into<String>) -> Self

The dark and light modules’ colours with colour on: theme keys or colour definitions (default #000000 and #ffffff, the nearest a console has: black and bright white on 16 colours).

Trait Implementations§

Source§

impl AccessibleText for QrCode

QR code, version 2, 25 by 25 modules, medium error correction, then what it encodes, when that is text.

Source§

fn accessible_text(&self, _width: usize) -> String

The content as plain lines, laid out for width where layout matters.
Source§

impl Clone for QrCode

Source§

fn clone(&self) -> Self

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 QrCode

Source§

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

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

impl PartialEq for QrCode

Source§

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

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

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

Inequality operator !=. Read more
Source§

impl Renderable for QrCode

Source§

fn rich_render( &self, console: &Console, options: &ConsoleOptions, ) -> Vec<Segment>

Source§

fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement

The (minimum, maximum) cell width this renderable wants. Port of __rich_measure__. The default is what upstream’s Measurement.get returns for a renderable without one: anything from nothing to the whole width, (0, max_width).
Source§

fn fit_to_measurement(&self) -> bool

Whether a top-level Console::print shrinks this renderable to its measured width. Upstream renders every top-level renderable at the full console width, so the default is false; an extension may opt in.
Source§

fn vertical(&self) -> Option<VerticalAlign>

The vertical alignment a Table cell holding this renderable uses in place of its column’s. Upstream reads getattr(renderable, "vertical", None); Align sets it.
Source§

fn print_end_line(&self, _options: &ConsoleOptions) -> bool

Whether a top-level print ends this renderable’s last line. This port’s renderables separate lines, where upstream’s end each line with "\n", so a print adds the final line break. A renderable whose upstream output stops without one (Screen, LiveRender, a Rule whose end has no newline) returns false, and the next output continues its last line. options are the print’s options.
Source§

impl StructuralPartialEq for QrCode

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more