remove .to_owned() calls on &str in exceptions

This commit is contained in:
Vukašin Stepanović
2023-02-15 11:03:27 +00:00
parent 722ce78fd0
commit 528ddea41c
47 changed files with 107 additions and 167 deletions

View File

@@ -101,16 +101,14 @@ impl Version {
dimension: u32,
) -> Result<&'static Version, Exceptions> {
if dimension % 4 != 1 {
return Err(Exceptions::format("dimension incorrect".to_owned()));
return Err(Exceptions::format("dimension incorrect"));
}
Self::getVersionForNumber((dimension - 17) / 4)
}
pub fn getVersionForNumber(versionNumber: u32) -> Result<&'static Version, Exceptions> {
if !(1..=40).contains(&versionNumber) {
return Err(Exceptions::illegalArgument(
"version out of spec".to_owned(),
));
return Err(Exceptions::illegalArgument("version out of spec"));
}
Ok(&VERSIONS[versionNumber as usize - 1])
}

View File

@@ -323,7 +323,7 @@ pub fn findMSBSet(value: u32) -> u32 {
// operations. We don't care if coefficients are positive or negative.
pub fn calculateBCHCode(value: u32, poly: u32) -> Result<u32, Exceptions> {
if poly == 0 {
return Err(Exceptions::illegalArgument("0 polynomial".to_owned()));
return Err(Exceptions::illegalArgument("0 polynomial"));
}
let mut value = value;
// If poly is "1 1111 0010 0101" (version info poly), msbSetInPoly is 13. We'll subtract 1
@@ -347,7 +347,7 @@ pub fn makeTypeInfoBits(
bits: &mut BitArray,
) -> Result<(), Exceptions> {
if !QRCode::isValidMaskPattern(maskPattern as i32) {
return Err(Exceptions::writer("Invalid mask pattern".to_owned()));
return Err(Exceptions::writer("Invalid mask pattern"));
}
let typeInfo = (ecLevel.get_value() << 3) as u32 | maskPattern;
bits.appendBits(typeInfo, 5)?;

View File

@@ -186,9 +186,7 @@ impl MinimalEncoder {
}
}
if smallestRXingResult < 0 {
return Err(Exceptions::writer(
"Data too big for any version".to_owned(),
));
return Err(Exceptions::writer("Data too big for any version"));
}
Ok(results[smallestRXingResult as usize].clone())
}

View File

@@ -180,9 +180,7 @@ pub fn encode_with_hints(
version = Version::getVersionForNumber(versionNumber)?;
let bitsNeeded = calculateBitsNeeded(mode, &header_bits, &data_bits, version);
if !willFit(bitsNeeded, version, &ec_level) {
return Err(Exceptions::writer(
"Data too big for requested version".to_owned(),
));
return Err(Exceptions::writer("Data too big for requested version"));
}
} else {
version = recommendVersion(&ec_level, mode, &header_bits, &data_bits)?;
@@ -446,9 +444,7 @@ pub fn terminateBits(num_data_bytes: u32, bits: &mut BitArray) -> Result<(), Exc
bits.appendBits(if (i & 0x01) == 0 { 0xEC } else { 0x11 }, 8)?;
}
if bits.getSize() != capacity as usize {
return Err(Exceptions::writer(
"Bits size does not equal capacity".to_owned(),
));
return Err(Exceptions::writer("Bits size does not equal capacity"));
}
Ok(())
}
@@ -467,7 +463,7 @@ pub fn getNumDataBytesAndNumECBytesForBlockID(
// numECBytesInBlock: &mut [u32],
) -> Result<(u32, u32), Exceptions> {
if block_id >= num_rsblocks {
return Err(Exceptions::writer("Block ID too large".to_owned()));
return Err(Exceptions::writer("Block ID too large"));
}
// numRsBlocksInGroup2 = 196 % 5 = 1
let num_rs_blocks_in_group2 = num_total_bytes % num_rsblocks;
@@ -488,18 +484,18 @@ pub fn getNumDataBytesAndNumECBytesForBlockID(
// Sanity checks.
// 26 = 26
if num_ec_bytes_in_group1 != numEcBytesInGroup2 {
return Err(Exceptions::writer("EC bytes mismatch".to_owned()));
return Err(Exceptions::writer("EC bytes mismatch"));
}
// 5 = 4 + 1.
if num_rsblocks != num_rs_blocks_in_group1 + num_rs_blocks_in_group2 {
return Err(Exceptions::writer("RS blocks mismatch".to_owned()));
return Err(Exceptions::writer("RS blocks mismatch"));
}
// 196 = (13 + 26) * 4 + (14 + 26) * 1
if num_total_bytes
!= ((num_data_bytes_in_group1 + num_ec_bytes_in_group1) * num_rs_blocks_in_group1)
+ ((num_data_bytes_in_group2 + numEcBytesInGroup2) * num_rs_blocks_in_group2)
{
return Err(Exceptions::writer("total bytes mismatch".to_owned()));
return Err(Exceptions::writer("total bytes mismatch"));
}
Ok(if block_id < num_rs_blocks_in_group1 {
@@ -522,7 +518,7 @@ pub fn interleaveWithECBytes(
// "bits" must have "getNumDataBytes" bytes of data.
if bits.getSizeInBytes() as u32 != num_data_bytes {
return Err(Exceptions::writer(
"Number of bits and data bytes does not match".to_owned(),
"Number of bits and data bytes does not match",
));
}
@@ -556,9 +552,7 @@ pub fn interleaveWithECBytes(
data_bytes_offset += numDataBytesInBlock as usize;
}
if num_data_bytes != data_bytes_offset as u32 {
return Err(Exceptions::writer(
"Data bytes does not match offset".to_owned(),
));
return Err(Exceptions::writer("Data bytes does not match offset"));
}
let mut result = BitArray::new();
@@ -757,7 +751,7 @@ pub fn appendKanjiBytes(content: &str, bits: &mut BitArray) -> Result<(), Except
.encode(content, encoding::EncoderTrap::Strict)
.map_err(|e| Exceptions::writer(format!("error {e}")))?;
if bytes.len() % 2 != 0 {
return Err(Exceptions::writer("Kanji byte size not even".to_owned()));
return Err(Exceptions::writer("Kanji byte size not even"));
}
let max_i = bytes.len() - 1; // bytes.length must be even
let mut i = 0;
@@ -772,7 +766,7 @@ pub fn appendKanjiBytes(content: &str, bits: &mut BitArray) -> Result<(), Except
subtracted = code as i32 - 0xc140;
}
if subtracted == -1 {
return Err(Exceptions::writer("Invalid byte sequence".to_owned()));
return Err(Exceptions::writer("Invalid byte sequence"));
}
let encoded = ((subtracted >> 8) * 0xc0) + (subtracted & 0xff);
bits.appendBits(encoded as u32, 13)?;

View File

@@ -58,9 +58,7 @@ impl Writer for QRCodeWriter {
hints: &crate::EncodingHintDictionary,
) -> Result<crate::common::BitMatrix, crate::Exceptions> {
if contents.is_empty() {
return Err(Exceptions::illegalArgument(
"found empty contents".to_owned(),
));
return Err(Exceptions::illegalArgument("found empty contents"));
}
if format != &BarcodeFormat::QR_CODE {
@@ -110,7 +108,7 @@ impl QRCodeWriter {
) -> Result<BitMatrix, Exceptions> {
let input = code.getMatrix();
if input.is_none() {
return Err(Exceptions::illegalState("matrix is empty".to_owned()));
return Err(Exceptions::illegalState("matrix is empty"));
}
let input = input.as_ref().ok_or(Exceptions::illegalStateEmpty())?;