[qr] QR: payloads, PNG and SVG, centre logo at level H, round-trip tests
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
use crate::Level;
|
||||
|
||||
/// Everything that can go wrong. `Display` is a plain sentence for the user.
|
||||
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
|
||||
#[non_exhaustive]
|
||||
pub enum QrError {
|
||||
#[error("There is nothing to put in the QR code yet. Type some text first.")]
|
||||
Empty,
|
||||
|
||||
/// A required field of a payload is empty; `field` is its plain name ("Wi-Fi network name", ...).
|
||||
#[error("The {field} is empty. Fill it in first.")]
|
||||
EmptyField { field: &'static str },
|
||||
|
||||
/// The content does not fit in a QR code at `level`; `logo` says the level was forced to H by a logo.
|
||||
#[error("{}", too_long(*level, *logo))]
|
||||
TooLong { level: Level, logo: bool },
|
||||
|
||||
#[error(
|
||||
"The two colours are too similar for a scanner to tell apart (contrast {ratio:.1}:1, it needs at least 3:1). \
|
||||
Make the code colour darker or the background lighter."
|
||||
)]
|
||||
LowContrast { ratio: f64 },
|
||||
|
||||
#[error(
|
||||
"The code colour is lighter than the background. Many scanners only read dark codes on a light background, \
|
||||
so swap the two colours."
|
||||
)]
|
||||
Inverted,
|
||||
|
||||
#[error("The code colour is fully transparent, so the code would be invisible. Choose a solid colour.")]
|
||||
TransparentDark,
|
||||
|
||||
#[error(
|
||||
"The image is too small for this much content: it needs at least {min_px} pixels across. \
|
||||
Make it larger or shorten the content."
|
||||
)]
|
||||
SizeTooSmall { min_px: u32 },
|
||||
|
||||
#[error("The image can be at most {max_px} pixels across. Choose a smaller size.")]
|
||||
SizeTooLarge { max_px: u32 },
|
||||
|
||||
#[error("The quiet zone can be at most {max} modules wide. Choose a narrower one.")]
|
||||
QuietZoneTooLarge { max: u32 },
|
||||
|
||||
/// The logo could not be used; the text says why.
|
||||
#[error("The logo cannot be used: {0}")]
|
||||
Logo(String),
|
||||
|
||||
/// An internal encoder failed (should not happen); the text is the encoder's.
|
||||
#[error("The QR code could not be made: {0}")]
|
||||
Encode(String),
|
||||
}
|
||||
|
||||
fn too_long(level: Level, logo: bool) -> String {
|
||||
if logo {
|
||||
"This is too much content for a QR code with a logo. A logo needs the highest error-correction level (H), \
|
||||
which holds the least. Remove the logo or shorten the content."
|
||||
.to_string()
|
||||
} else if level == Level::L {
|
||||
"This is too much content for one QR code, even at the lowest error-correction level (L). Shorten it."
|
||||
.to_string()
|
||||
} else {
|
||||
format!(
|
||||
"This is too much content for one QR code at error-correction level {level}. \
|
||||
Shorten it or choose a lower level."
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user