Skip to content

Commit

Permalink
Add TryFrom to convert from number to Version
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenperera committed Sep 25, 2024
1 parent fd92fe4 commit b7c1b53
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [Unreleased]

## [0.3.4] - 2024-07-24

- Add `TryFrom` to convert from number to `Version`
- Add `VersionError` type

## [0.3.3] - 2024-07-22

- Expose `Header::try_from_str` publicly
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bbqr"
version = "0.3.3"
version = "0.3.4"
edition = "2021"
license = "MIT"
rust-version = "1.68"
Expand Down
95 changes: 95 additions & 0 deletions src/qr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ use std::cmp::Ordering;

use crate::consts::QR_DATA_CAPACITY;

/// Errors that can occur when creating a `Version`
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum VersionError {
#[error("Invalid version number, must be between 1 and 40, got {0}")]
InvalidVersionNumber(usize),
}

/// Enum containing all possible `QRCode` versions, taken from fast_qr crate
/// The version of the QR code, from V01 to V40
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
Expand Down Expand Up @@ -199,6 +206,94 @@ impl Version {
}
}

impl TryFrom<usize> for Version {
type Error = VersionError;

fn try_from(version_number: usize) -> Result<Self, Self::Error> {
try_from_number(version_number)
}
}

impl TryFrom<u8> for Version {
type Error = VersionError;

fn try_from(version_number: u8) -> Result<Self, Self::Error> {
try_from_number(version_number as usize)
}
}

impl TryFrom<u16> for Version {
type Error = VersionError;

fn try_from(version_number: u16) -> Result<Self, Self::Error> {
try_from_number(version_number as usize)
}
}

impl TryFrom<u32> for Version {
type Error = VersionError;

fn try_from(version_number: u32) -> Result<Self, Self::Error> {
try_from_number(version_number as usize)
}
}

impl TryFrom<u64> for Version {
type Error = VersionError;

fn try_from(version_number: u64) -> Result<Self, Self::Error> {
try_from_number(version_number as usize)
}
}

impl TryFrom<i8> for Version {
type Error = VersionError;

fn try_from(version_number: i8) -> Result<Self, Self::Error> {
try_from_number(version_number as usize)
}
}

impl TryFrom<i16> for Version {
type Error = VersionError;

fn try_from(version_number: i16) -> Result<Self, Self::Error> {
try_from_number(version_number as usize)
}
}

impl TryFrom<i32> for Version {
type Error = VersionError;

fn try_from(version_number: i32) -> Result<Self, Self::Error> {
try_from_number(version_number as usize)
}
}

impl TryFrom<i64> for Version {
type Error = VersionError;

fn try_from(version_number: i64) -> Result<Self, Self::Error> {
try_from_number(version_number as usize)
}
}

impl TryFrom<isize> for Version {
type Error = VersionError;

fn try_from(version_number: isize) -> Result<Self, Self::Error> {
try_from_number(version_number as usize)
}
}

fn try_from_number(version_number: usize) -> Result<Version, VersionError> {
if (1..=40).contains(&version_number) {
return Ok(Version::from_number(version_number));
}

Err(VersionError::InvalidVersionNumber(version_number))
}

/// Convert between our type and fast-qr version type
#[cfg(feature = "qr-codes")]
impl From<Version> for fast_qr::Version {
Expand Down

0 comments on commit b7c1b53

Please sign in to comment.