From 5a7a1c65f034325a23a327d1e570852c6ddafa24 Mon Sep 17 00:00:00 2001 From: kennytm Date: Sun, 5 Jun 2016 00:17:41 +0800 Subject: [PATCH] Expose the maximum length for a EC-level via Bits::max_len. Fixes #3. --- src/bits.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/bits.rs b/src/bits.rs index c940bf9..cc33fb8 100644 --- a/src/bits.rs +++ b/src/bits.rs @@ -82,7 +82,7 @@ impl Bits { self.data } - /// Total number of bits. + /// Total number of bits currently pushed. pub fn len(&self) -> usize { if self.bit_offset == 0 { 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 { + self.version.fetch(ec_level, &DATA_LENGTHS) + } + /// Version of the QR code. pub fn version(&self) -> Version { self.version @@ -657,7 +663,7 @@ impl Bits { }; 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 { return Err(QrError::DataTooLong); }