This commit is contained in:
kennytm 2015-01-25 17:51:54 +08:00
parent 96e7894f9f
commit 18f9499918
5 changed files with 26 additions and 12 deletions

View file

@ -758,7 +758,7 @@ impl Bits {
where I: Iterator<Item=Segment> where I: Iterator<Item=Segment>
{ {
for segment in segments_iter { for segment in segments_iter {
let slice = data.slice(segment.begin, segment.end); let slice = &data[segment.begin .. segment.end];
try!(match segment.mode { try!(match segment.mode {
Mode::Numeric => self.push_numeric_data(slice), Mode::Numeric => self.push_numeric_data(slice),
Mode::Alphanumeric => self.push_alphanumeric_data(slice), Mode::Alphanumeric => self.push_alphanumeric_data(slice),

View file

@ -20,7 +20,7 @@ use types::{Version, EcLevel};
//{{{ Modules //{{{ Modules
/// The color of a module (pixel) in the QR code. /// The color of a module (pixel) in the QR code.
#[derive(PartialEq, Eq, Clone, Copy, Show)] #[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum Module { pub enum Module {
/// The module is empty. /// The module is empty.
Empty, Empty,
@ -177,8 +177,8 @@ mod basic_canvas_tests {
fn test_debug_str() { fn test_debug_str() {
let mut c = Canvas::new(Version::Normal(1), EcLevel::L); let mut c = Canvas::new(Version::Normal(1), EcLevel::L);
for i in 3 .. 20 { for i in 3i16 .. 20 {
for j in 3 .. 20 { for j in 3i16 .. 20 {
c.put(i, j, match ((i * 3) ^ j) % 5 { c.put(i, j, match ((i * 3) ^ j) % 5 {
0 => Module::Empty, 0 => Module::Empty,
1 => Module::Light, 1 => Module::Light,
@ -1372,7 +1372,7 @@ mod draw_codewords_test {
/// The mask patterns. Since QR code and Micro QR code do not use the same /// The mask patterns. Since QR code and Micro QR code do not use the same
/// pattern number, we name them according to their shape instead of the number. /// pattern number, we name them according to their shape instead of the number.
#[derive(Show, Copy)] #[derive(Debug, Copy)]
pub enum MaskPattern { pub enum MaskPattern {
/// QR code pattern 000: `(x + y) % 2 == 0`. /// QR code pattern 000: `(x + y) % 2 == 0`.
Checkerboard = 0b000, Checkerboard = 0b000,
@ -1811,7 +1811,7 @@ mod penalty_tests {
]; ];
let mut c = Canvas::new(Version::Micro(4), EcLevel::Q); let mut c = Canvas::new(Version::Micro(4), EcLevel::Q);
for i in 0 .. 17 { for i in 0i16 .. 17 {
c.put(i, -1, HORIZONTAL_SIDE[i as usize]); c.put(i, -1, HORIZONTAL_SIDE[i as usize]);
c.put(-1, i, VERTICAL_SIDE[i as usize]); c.put(-1, i, VERTICAL_SIDE[i as usize]);
} }

View file

@ -31,7 +31,7 @@ pub fn create_error_correction_code(data: &[u8], ec_code_size: usize) -> Vec<u8>
} }
let log_lead_coeff = LOG_TABLE[lead_coeff] as usize; let log_lead_coeff = LOG_TABLE[lead_coeff] as usize;
for (u, v) in res.slice_from_mut(i+1).iter_mut().zip(log_den.iter()) { for (u, v) in res[i+1 ..].iter_mut().zip(log_den.iter()) {
*u ^= EXP_TABLE[((*v as usize + log_lead_coeff) % 255) as usize]; *u ^= EXP_TABLE[((*v as usize + log_lead_coeff) % 255) as usize];
} }
} }

View file

@ -11,7 +11,7 @@ use test::Bencher;
//{{{ Segment //{{{ Segment
/// A segment of data committed to an encoding mode. /// A segment of data committed to an encoding mode.
#[derive(PartialEq, Eq, Show, Copy, Clone)] #[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub struct Segment { pub struct Segment {
/// The encoding mode of the segment of data. /// The encoding mode of the segment of data.
pub mode: Mode, pub mode: Mode,

View file

@ -2,13 +2,14 @@
use std::default::Default; use std::default::Default;
use std::cmp::{PartialOrd, Ordering}; use std::cmp::{PartialOrd, Ordering};
use std::fmt::{Display, Formatter, Error};
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
//{{{ QrResult //{{{ QrResult
/// `QrError` encodes the error encountered when generating a QR code. /// `QrError` encodes the error encountered when generating a QR code.
#[unstable] #[unstable]
#[derive(Show, PartialEq, Eq, Copy, Clone)] #[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum QrError { pub enum QrError {
/// The data is too long to encode into a QR code for the given version. /// The data is too long to encode into a QR code for the given version.
DataTooLong, DataTooLong,
@ -28,6 +29,19 @@ pub enum QrError {
InvalidCharacter, InvalidCharacter,
} }
impl Display for QrError {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
let msg = match *self {
QrError::DataTooLong => "data too long",
QrError::InvalidVersion => "invalid version",
QrError::UnsupportedCharacterSet => "unsupported character set",
QrError::InvalidEciDesignator => "invalid ECI designator",
QrError::InvalidCharacter => "invalid character",
};
fmt.write_str(msg)
}
}
/// `QrResult` is a convenient alias for a QR code generation result. /// `QrResult` is a convenient alias for a QR code generation result.
#[stable] #[stable]
pub type QrResult<T> = Result<T, QrError>; pub type QrResult<T> = Result<T, QrError>;
@ -38,7 +52,7 @@ pub type QrResult<T> = Result<T, QrError>;
/// The error correction level. It allows the original information be recovered /// The error correction level. It allows the original information be recovered
/// even if parts of the code is damaged. /// even if parts of the code is damaged.
#[derive(Show, PartialEq, Eq, Copy, Clone, PartialOrd, Ord)] #[derive(Debug, PartialEq, Eq, Copy, Clone, PartialOrd, Ord)]
#[unstable] #[unstable]
pub enum EcLevel { pub enum EcLevel {
/// Low error correction. Allows up to 7% of wrong blocks. /// Low error correction. Allows up to 7% of wrong blocks.
@ -65,7 +79,7 @@ pub enum EcLevel {
/// The smallest version is `Version::Normal(1)` of size 21×21, and the largest /// The smallest version is `Version::Normal(1)` of size 21×21, and the largest
/// is `Version::Normal(40)` of size 177×177. /// is `Version::Normal(40)` of size 177×177.
#[unstable] #[unstable]
#[derive(Show, PartialEq, Eq, Copy, Clone)] #[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Version { pub enum Version {
/// A normal QR code version. The parameter should be between 1 and 40. /// A normal QR code version. The parameter should be between 1 and 40.
Normal(i16), Normal(i16),
@ -138,7 +152,7 @@ impl Version {
/// The mode indicator, which specifies the character set of the encoded data. /// The mode indicator, which specifies the character set of the encoded data.
#[unstable] #[unstable]
#[derive(Show, PartialEq, Eq, Copy, Clone)] #[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Mode { pub enum Mode {
/// The data contains only characters 0 to 9. /// The data contains only characters 0 to 9.
Numeric, Numeric,