Changed Rust API to move the version min/max values into associated constants for its type.

pull/106/head
Project Nayuki 4 years ago
parent bafd258293
commit 8cbd1f506a

@ -29,8 +29,7 @@ use qrcodegen::Mask;
use qrcodegen::QrCode; use qrcodegen::QrCode;
use qrcodegen::QrCodeEcc; use qrcodegen::QrCodeEcc;
use qrcodegen::QrSegment; use qrcodegen::QrSegment;
use qrcodegen::QrCode_MAX_VERSION; use qrcodegen::Version;
use qrcodegen::QrCode_MIN_VERSION;
// The main application program. // The main application program.
@ -143,20 +142,20 @@ fn do_segment_demo() {
fn do_mask_demo() { fn do_mask_demo() {
// Project Nayuki URL // Project Nayuki URL
let segs = QrSegment::make_segments(&to_chars("https://www.nayuki.io/")); let segs = QrSegment::make_segments(&to_chars("https://www.nayuki.io/"));
let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::High, QrCode_MIN_VERSION, QrCode_MAX_VERSION, None, true).unwrap(); // Automatic mask let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::High, Version::MIN, Version::MAX, None, true).unwrap(); // Automatic mask
print_qr(&qr); print_qr(&qr);
let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::High, QrCode_MIN_VERSION, QrCode_MAX_VERSION, Some(Mask::new(3)), true).unwrap(); // Force mask 3 let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::High, Version::MIN, Version::MAX, Some(Mask::new(3)), true).unwrap(); // Force mask 3
print_qr(&qr); print_qr(&qr);
// Chinese text as UTF-8 // Chinese text as UTF-8
let segs = QrSegment::make_segments(&to_chars("維基百科Wikipedia聆聽i/ˌwɪkᵻˈpiːdi.ə/)是一個自由內容、公開編輯且多語言的網路百科全書協作計畫")); let segs = QrSegment::make_segments(&to_chars("維基百科Wikipedia聆聽i/ˌwɪkᵻˈpiːdi.ə/)是一個自由內容、公開編輯且多語言的網路百科全書協作計畫"));
let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, QrCode_MIN_VERSION, QrCode_MAX_VERSION, Some(Mask::new(0)), true).unwrap(); // Force mask 0 let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, Version::MIN, Version::MAX, Some(Mask::new(0)), true).unwrap(); // Force mask 0
print_qr(&qr); print_qr(&qr);
let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, QrCode_MIN_VERSION, QrCode_MAX_VERSION, Some(Mask::new(1)), true).unwrap(); // Force mask 1 let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, Version::MIN, Version::MAX, Some(Mask::new(1)), true).unwrap(); // Force mask 1
print_qr(&qr); print_qr(&qr);
let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, QrCode_MIN_VERSION, QrCode_MAX_VERSION, Some(Mask::new(5)), true).unwrap(); // Force mask 5 let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, Version::MIN, Version::MAX, Some(Mask::new(5)), true).unwrap(); // Force mask 5
print_qr(&qr); print_qr(&qr);
let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, QrCode_MIN_VERSION, QrCode_MAX_VERSION, Some(Mask::new(7)), true).unwrap(); // Force mask 7 let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, Version::MIN, Version::MAX, Some(Mask::new(7)), true).unwrap(); // Force mask 7
print_qr(&qr); print_qr(&qr);
} }

@ -59,9 +59,9 @@ fn main() {
let mask = read_int(); let mask = read_int();
let boostecl = read_int(); let boostecl = read_int();
assert!(0 <= errcorlvl && errcorlvl <= 3); assert!(0 <= errcorlvl && errcorlvl <= 3);
assert!(i16::from(qrcodegen::QrCode_MIN_VERSION.value()) <= minversion assert!(i16::from(Version::MIN.value()) <= minversion
&& minversion <= maxversion && minversion <= maxversion
&& maxversion <= i16::from(qrcodegen::QrCode_MAX_VERSION.value())); && maxversion <= i16::from(Version::MAX.value()));
assert!(-1 <= mask && mask <= 7); assert!(-1 <= mask && mask <= 7);
assert!(boostecl >> 1 == 0); assert!(boostecl >> 1 == 0);

@ -184,7 +184,7 @@ impl QrCode {
/// Returns a wrapped `QrCode` if successful, or `Err` if the /// Returns a wrapped `QrCode` if successful, or `Err` if the
/// data is too long to fit in any version at the given ECC level. /// data is too long to fit in any version at the given ECC level.
pub fn encode_segments(segs: &[QrSegment], ecl: QrCodeEcc) -> Result<Self,DataTooLong> { pub fn encode_segments(segs: &[QrSegment], ecl: QrCodeEcc) -> Result<Self,DataTooLong> {
QrCode::encode_segments_advanced(segs, ecl, QrCode_MIN_VERSION, QrCode_MAX_VERSION, None, true) QrCode::encode_segments_advanced(segs, ecl, Version::MIN, Version::MAX, None, true)
} }
@ -883,13 +883,6 @@ impl FinderPenalty {
/*---- Constants and tables ----*/ /*---- Constants and tables ----*/
/// The minimum version number supported in the QR Code Model 2 standard.
pub const QrCode_MIN_VERSION: Version = Version( 1);
/// The maximum version number supported in the QR Code Model 2 standard.
pub const QrCode_MAX_VERSION: Version = Version(40);
// For use in get_penalty_score(), when evaluating which mask is best. // For use in get_penalty_score(), when evaluating which mask is best.
const PENALTY_N1: i32 = 3; const PENALTY_N1: i32 = 3;
const PENALTY_N2: i32 = 3; const PENALTY_N2: i32 = 3;
@ -1252,8 +1245,8 @@ impl BitBuffer {
/// ///
/// - Decrease the error correction level if it was greater than `QrCodeEcc::Low`. /// - Decrease the error correction level if it was greater than `QrCodeEcc::Low`.
/// - If the `encode_segments_advanced()` function was called, then increase the maxversion /// - If the `encode_segments_advanced()` function was called, then increase the maxversion
/// argument if it was less than `QrCode_MAX_VERSION`. (This advice does not apply to the /// argument if it was less than `Version::MAX`. (This advice does not apply to the
/// other factory functions because they search all versions up to `QrCode_MAX_VERSION`.) /// other factory functions because they search all versions up to `Version::MAX`.)
/// - Split the text data into better or optimal segments in order to reduce the number of bits required. /// - Split the text data into better or optimal segments in order to reduce the number of bits required.
/// - Change the text or binary data to be shorter. /// - Change the text or binary data to be shorter.
/// - Change the text to fit the character set of a particular segment mode (e.g. alphanumeric). /// - Change the text to fit the character set of a particular segment mode (e.g. alphanumeric).
@ -1279,11 +1272,17 @@ impl std::fmt::Display for DataTooLong {
pub struct Version(u8); pub struct Version(u8);
impl Version { impl Version {
/// The minimum version number supported in the QR Code Model 2 standard.
pub const MIN: Version = Version( 1);
/// The maximum version number supported in the QR Code Model 2 standard.
pub const MAX: Version = Version(40);
/// Creates a version object from the given number. /// Creates a version object from the given number.
/// ///
/// Panics if the number is outside the range [1, 40]. /// Panics if the number is outside the range [1, 40].
pub fn new(ver: u8) -> Self { pub fn new(ver: u8) -> Self {
assert!(QrCode_MIN_VERSION.value() <= ver && ver <= QrCode_MAX_VERSION.value(), "Version number out of range"); assert!(Version::MIN.value() <= ver && ver <= Version::MAX.value(), "Version number out of range");
Self(ver) Self(ver)
} }

Loading…
Cancel
Save