Skip to main content

Bits

Struct Bits 

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

The Bits structure stores the encoded data for a QR code.

Implementations§

Source§

impl Bits

Source

pub const fn new(version: Version) -> Self

Constructs a new, empty bits structure.

Source

pub fn push_number_checked(&mut self, n: usize, number: usize) -> QrResult<()>

Pushes an N-bit big-endian integer to the end of the bits, and check that the number does not overflow the bits.

Writing zero bits with a zero value leaves the stream unchanged.

Returns Err(QrError::DataTooLong) on overflow.

Source

pub fn reserve(&mut self, n: usize)

Reserves n extra bits of space for pushing.

Space remaining in the current partial byte is used first. This does not change the bit stream, recorded modes, or payload length.

Source

pub fn into_bytes(self) -> Vec<u8> ⓘ

Convert the bits into a byte vector.

Source

pub fn len(&self) -> usize

Total number of bits currently pushed.

Source

pub fn is_empty(&self) -> bool

Whether there are any bits pushed.

Source

pub fn max_len(&self, ec_level: EcLevel) -> QrResult<usize>

The maximum number of bits allowed by the provided QR code version and error correction level.

§Errors

Returns Err(QrError::InvalidVersion) if it is not valid to use the ec_level for the given version (e.g. Version::Micro(1) with EcLevel::H).

Source

pub fn version(&self) -> Version

Version of the QR code.

Source

pub const fn encoding_modes(&self) -> EncodingModes

Data modes recorded while payload segments were pushed.

Source

pub fn payload_bits_len(&self) -> usize

Number of payload bits before terminator and padding bits were added.

If push_terminator has not been called, this returns the current bit length.

Source

pub fn remaining_capacity_bits(&self, ec_level: EcLevel) -> QrResult<usize>

Remaining data capacity, in bits, before terminator and padding bits.

§Errors

Returns QrError::InvalidVersion when the stored version is incompatible with ec_level.

Source§

impl Bits

Source

pub fn push_mode_indicator(&mut self, mode: ExtendedMode) -> QrResult<()>

Push the mode indicator to the end of the bits.

§Errors

If the mode is not supported in the provided version, this method returns Err(QrError::UnsupportedCharacterSet).

Source§

impl Bits

Source

pub fn push_eci_designator(&mut self, eci_designator: u32) -> QrResult<()>

Push an ECI (Extended Channel Interpretation) designator to the bits.

An ECI designator is a 6-digit number to specify the character set of the following binary data. After calling this method, one could call .push_byte_data() or similar methods to insert the actual data, e.g.

#![allow(unused_must_use)]

use qrcode_core::bits::Bits;
use qrcode_core::types::Version;

let mut bits = Bits::new(Version::Normal(1));
bits.push_eci_designator(9); // 9 = ISO-8859-7 (Greek).
bits.push_byte_data(b"\xa1\xa2\xa3\xa4\xa5"); // ΑΒΓΔΕ

The full list of ECI designator values can be found from http://strokescribe.com/en/ECI.html. Some example values are:

ECI #Character set
3ISO-8859-1 (Western European)
20Shift JIS (Japanese)
23Windows 1252 (Latin 1) (Western European)
25UTF-16 Big Endian
26UTF-8
28Big 5 (Traditional Chinese)
29GB-18030 (Simplified Chinese)
30EUC-KR (Korean)
§Errors

If the QR code version does not support ECI, this method will return Err(QrError::UnsupportedCharacterSet).

If the designator is outside the expected range, this method will return Err(QrError::InvalidECIDesignator).

Source§

impl Bits

Source

pub fn push_numeric_data(&mut self, data: &[u8]) -> QrResult<()>

Encodes a numeric string to the bits.

The data should only contain the characters 0 to 9.

§Errors

Returns Err(QrError::DataTooLong) on overflow. Returns QrError::InvalidCharacter if data contains a non-digit.

Source§

impl Bits

Source

pub fn push_alphanumeric_data(&mut self, data: &[u8]) -> QrResult<()>

Encodes an alphanumeric string to the bits.

The data should only contain the characters A to Z (excluding lowercase), 0 to 9, space, $, %, *, +, -, ., / or :.

§Errors

Returns Err(QrError::DataTooLong) on overflow. Returns QrError::InvalidCharacter if data contains a byte outside the QR alphanumeric alphabet.

Source§

impl Bits

Source

pub fn push_byte_data(&mut self, data: &[u8]) -> QrResult<()>

Encodes 8-bit byte data to the bits.

§Errors

Returns Err(QrError::DataTooLong) on overflow.

Source§

impl Bits

Source

pub fn push_kanji_data(&mut self, data: &[u8]) -> QrResult<()>

Encodes Shift JIS double-byte data to the bits.

§Errors

Returns Err(QrError::DataTooLong) on overflow.

Returns Err(QrError::InvalidCharacter) if the data is not Shift JIS double-byte data in the QR Kanji ranges (e.g. if the length of data is not an even number).

Source§

impl Bits

Source

pub fn push_mode_data<M: EncodingMode>(&mut self, data: &[u8]) -> QrResult<()>

Encodes data with a type-level QR encoding mode.

This is the type-safe counterpart to calling one of push_numeric_data, push_alphanumeric_data, push_byte_data, or push_kanji_data directly.

§Errors

Returns QrError::InvalidCharacter when data is not valid for M. Returns the same length or version errors as the mode-specific push method after validation succeeds.

Source§

impl Bits

Source

pub fn push_fnc1_first_position(&mut self) -> QrResult<()>

Encodes an indicator that the following data are formatted according to the UCC/EAN Application Identifiers standard.

#![allow(unused_must_use)]

use qrcode_core::bits::Bits;
use qrcode_core::types::Version;

let mut bits = Bits::new(Version::Normal(1));
bits.push_fnc1_first_position();
bits.push_numeric_data(b"01049123451234591597033130128");
bits.push_alphanumeric_data(b"%10ABC123");

In QR code, the character % is used as the data field separator (0x1D).

§Errors

If the mode is not supported in the provided version, this method returns Err(QrError::UnsupportedCharacterSet).

Source

pub fn push_fnc1_second_position( &mut self, application_indicator: u8, ) -> QrResult<()>

Encodes an indicator that the following data are formatted in accordance with specific industry or application specifications previously agreed with AIM International.

#![allow(unused_must_use)]

use qrcode_core::bits::Bits;
use qrcode_core::types::Version;

let mut bits = Bits::new(Version::Normal(1));
bits.push_fnc1_second_position(37);
bits.push_alphanumeric_data(b"AA1234BBB112");
bits.push_byte_data(b"text text text text\r");

If the application indicator is a single Latin alphabet (a–z / A–Z), please pass in its ASCII value + 100:

ⓘ
bits.push_fnc1_second_position(b'A' + 100);
§Errors

If the mode is not supported in the provided version, this method returns Err(QrError::UnsupportedCharacterSet).

Source§

impl Bits

Source

pub fn push_structured_append_header( &mut self, position: u8, total: u8, parity: u8, ) -> QrResult<()>

Pushes a Structured Append header (ISO/IEC 18004 §7.4) to the front of the bit stream.

Structured Append splits one logical message across 2..=16 QR symbols. Every symbol in the sequence carries this 20-bit header as the very first thing in its bit stream, before the data mode indicator:

  • 4-bit mode indicator 0011,
  • an 8-bit symbol-sequence indicator whose high nibble is this symbol’s zero-based index (position - 1) and whose low nibble is total - 1,
  • an 8-bit parity byte (the XOR of every byte of the original, un-split message — identical in every symbol).

Structured Append is not valid for Micro QR; this method returns Err(QrError::UnsupportedCharacterSet) for a Micro QR version.

§Errors

Returns QrError::UnsupportedCharacterSet on a Micro QR version, and QrError::InvalidStructuredAppend if total is not 2..=16 or position is not 1..=total.

use qrcode_core::bits::Bits;
use qrcode_core::types::Version;

let mut bits = Bits::new(Version::Normal(1));
bits.push_structured_append_header(1, 3, 0x5a);
// First symbol of a 3-symbol sequence; parity 0x5a.
Source§

impl Bits

Source

pub fn push_terminator(&mut self, ec_level: EcLevel) -> QrResult<()>

Pushes the ending bits to indicate no more data.

§Errors

Returns Err(QrError::DataTooLong) on overflow.

Returns Err(QrError::InvalidVersion) if it is not valid to use the ec_level for the given version (e.g. Version::Micro(1) with EcLevel::H).

Source§

impl Bits

Source

pub fn push_segments<I>( &mut self, data: &[u8], segments_iter: I, ) -> QrResult<()>
where I: Iterator<Item = Segment>,

Push a segmented data to the bits, and then terminate it.

§Errors

Returns Err(QrError::DataTooLong) on overflow.

Returns Err(QrError::InvalidData) if the segment refers to incorrectly encoded byte sequences.

Source

pub fn push_optimal_data(&mut self, data: &[u8]) -> QrResult<()>

Pushes the data the bits, using the optimal encoding.

§Errors

Returns Err(QrError::DataTooLong) on overflow.

Auto Trait Implementations§

§

impl Freeze for Bits

§

impl RefUnwindSafe for Bits

§

impl Send for Bits

§

impl Sync for Bits

§

impl Unpin for Bits

§

impl UnsafeUnpin for Bits

§

impl UnwindSafe for Bits

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, 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.