Simplified and clarified various pieces of Rust code.

This commit is contained in:
Project Nayuki 2019-09-13 04:02:23 +00:00
parent 2136d88ac8
commit e83ac1cd62
1 changed files with 8 additions and 10 deletions

View File

@ -165,7 +165,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_binary(data: &[u8], ecl: QrCodeEcc) -> Result<Self,DataTooLong> { pub fn encode_binary(data: &[u8], ecl: QrCodeEcc) -> Result<Self,DataTooLong> {
let segs: Vec<QrSegment> = vec![QrSegment::make_bytes(data)]; let segs: [QrSegment; 1] = [QrSegment::make_bytes(data)];
QrCode::encode_segments(&segs, ecl) QrCode::encode_segments(&segs, ecl)
} }
@ -212,20 +212,18 @@ impl QrCode {
// Number of data bits available // Number of data bits available
let datacapacitybits: usize = QrCode::get_num_data_codewords(version, ecl) * 8; let datacapacitybits: usize = QrCode::get_num_data_codewords(version, ecl) * 8;
let dataused: Option<usize> = QrSegment::get_total_bits(segs, version); let dataused: Option<usize> = QrSegment::get_total_bits(segs, version);
if let Some(n) = dataused { if dataused.map_or(false, |n| n <= datacapacitybits) {
if n <= datacapacitybits { break dataused.unwrap(); // This version number is found to be suitable
break n; // This version number is found to be suitable } else if version.value() >= maxversion.value() { // All versions in the range could not fit the given data
}
}
if version.value() >= maxversion.value() { // All versions in the range could not fit the given data
let msg: String = match dataused { let msg: String = match dataused {
None => String::from("Segment too long"), None => String::from("Segment too long"),
Some(n) => format!("Data length = {} bits, Max capacity = {} bits", Some(n) => format!("Data length = {} bits, Max capacity = {} bits",
n, datacapacitybits), n, datacapacitybits),
}; };
return Err(DataTooLong(msg)); return Err(DataTooLong(msg));
} else {
version = Version::new(version.value() + 1);
} }
version = Version::new(version.value() + 1);
}; };
// Increase the error correction level while the data still fits in the current version number // Increase the error correction level while the data still fits in the current version number
@ -1262,7 +1260,7 @@ impl Version {
/// 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!(QrCode_MIN_VERSION.value() <= ver && ver <= QrCode_MAX_VERSION.value(), "Version number out of range");
Version(ver) Self(ver)
} }
/// Returns the value, which is in the range [1, 40]. /// Returns the value, which is in the range [1, 40].
@ -1282,7 +1280,7 @@ impl Mask {
/// Panics if the number is outside the range [0, 7]. /// Panics if the number is outside the range [0, 7].
pub fn new(mask: u8) -> Self { pub fn new(mask: u8) -> Self {
assert!(mask <= 7, "Mask value out of range"); assert!(mask <= 7, "Mask value out of range");
Mask(mask) Self(mask)
} }
/// Returns the value, which is in the range [0, 7]. /// Returns the value, which is in the range [0, 7].