Expose the maximum length for a EC-level via Bits::max_len. Fixes #3.

This commit is contained in:
kennytm 2016-06-05 00:17:41 +08:00
parent 673ee09ef9
commit 5a7a1c65f0

View file

@ -82,7 +82,7 @@ impl Bits {
self.data self.data
} }
/// Total number of bits. /// Total number of bits currently pushed.
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
if self.bit_offset == 0 { if self.bit_offset == 0 {
self.data.len() * 8 self.data.len() * 8
@ -91,6 +91,12 @@ impl Bits {
} }
} }
/// The maximum number of bits allowed by the provided QR code version and
/// error correction level.
pub fn max_len(&self, ec_level: EcLevel) -> QrResult<usize> {
self.version.fetch(ec_level, &DATA_LENGTHS)
}
/// Version of the QR code. /// Version of the QR code.
pub fn version(&self) -> Version { pub fn version(&self) -> Version {
self.version self.version
@ -657,7 +663,7 @@ impl Bits {
}; };
let cur_length = self.len(); let cur_length = self.len();
let data_length = try!(self.version.fetch(ec_level, &DATA_LENGTHS)); let data_length = try!(self.max_len(ec_level));
if cur_length > data_length { if cur_length > data_length {
return Err(QrError::DataTooLong); return Err(QrError::DataTooLong);
} }