Revamped the DataTooLong error type in Rust to change from string to enum and remove Error.description().

pull/134/head
Project Nayuki 3 years ago
parent fb9638d591
commit 8bd5ed46b5

@ -220,12 +220,10 @@ impl QrCode {
if dataused.map_or(false, |n| n <= datacapacitybits) { if dataused.map_or(false, |n| n <= datacapacitybits) {
break dataused.unwrap(); // This version number is found to be suitable break dataused.unwrap(); // This version number is found to be suitable
} else if version >= maxversion { // All versions in the range could not fit the given data } else if version >= maxversion { // All versions in the range could not fit the given data
let msg: String = match dataused { return Err(match dataused {
None => String::from("Segment too long"), None => DataTooLong::SegmentTooLong,
Some(n) => format!("Data length = {} bits, Max capacity = {} bits", Some(n) => DataTooLong::DataOverCapacity(n, datacapacitybits),
n, datacapacitybits), });
};
return Err(DataTooLong(msg));
} else { } else {
version = Version::new(version.value() + 1); version = Version::new(version.value() + 1);
} }
@ -1232,17 +1230,20 @@ impl BitBuffer {
/// - 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).
/// - Propagate the error upward to the caller/user. /// - Propagate the error upward to the caller/user.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct DataTooLong(String); pub enum DataTooLong {
SegmentTooLong,
impl std::error::Error for DataTooLong { DataOverCapacity(usize, usize),
fn description(&self) -> &str {
&self.0
}
} }
impl std::error::Error for DataTooLong {}
impl std::fmt::Display for DataTooLong { impl std::fmt::Display for DataTooLong {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(&self.0) match *self {
Self::SegmentTooLong => write!(f, "Segment too long"),
Self::DataOverCapacity(datalen, maxcapacity) =>
write!(f, "Data length = {} bits, Max capacity = {} bits", datalen, maxcapacity),
}
} }
} }

Loading…
Cancel
Save