Skip to main content

qrcode_parse/
lib.rs

1//! Content parsing for QR payloads.
2//!
3//! Parsers that turn a decoded QR payload (the raw bytes a scanner recovers)
4//! back into structured values. These are the symmetric decode-side counterpart
5//! to the `qrcode-rs` facade's `QrCode::for_wifi`, `QrCode::for_vcard`, and
6//! `QrCode::for_gs1` constructors, which encode in the opposite direction.
7//!
8//! Encoding is one-way: a QR code symbol does not retain its input payload, so
9//! these parsers operate on a `&str`/`&[u8]` the caller supplies.
10
11#![cfg_attr(not(feature = "std"), no_std)]
12#![deny(missing_docs)]
13
14#[cfg(not(feature = "std"))]
15extern crate alloc;
16
17pub mod gs1;
18pub mod vcard;
19pub mod wifi;
20
21use core::fmt::{Display, Error, Formatter};
22
23/// Errors returned by the content parsers in this module.
24///
25/// Each parser returns `Result<T, ParseError>`. The enum is `#[non_exhaustive]`:
26/// future versions may add variants, so external callers should match with a `_`
27/// arm.
28#[non_exhaustive]
29#[derive(Debug, Clone, PartialEq, Eq)]
30pub enum ParseError {
31    /// The input did not match the expected wire format (e.g. missing the
32    /// `WIFI:` prefix or the `BEGIN:VCARD` sentinel).
33    InvalidFormat,
34    /// A required field was missing. Carries the field name.
35    MissingField(&'static str),
36    /// A field was present but its value was invalid (e.g. an unrecognized
37    /// WiFi security mode).
38    InvalidValue,
39}
40
41impl Display for ParseError {
42    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
43        match self {
44            Self::InvalidFormat => f.write_str("invalid QR payload format"),
45            Self::MissingField(name) => write!(f, "missing required field: {name}"),
46            Self::InvalidValue => f.write_str("invalid field value"),
47        }
48    }
49}
50
51impl ::core::error::Error for ParseError {}