1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
//! A library for handling [International Standard Book Number], or ISBNs.
//!
//! # Examples
//!
//! ```
//! use isbn2::{Isbn10, Isbn13};
//!
//! let isbn_10 = Isbn10::new([8, 9, 6, 6, 2, 6, 1, 2, 6, 4]).unwrap();
//! assert_eq!(isbn_10.hyphenate().unwrap().as_str(), "89-6626-126-4");
//! assert_eq!(isbn_10.registration_group(), Ok("Korea, Republic"));
//! assert_eq!("89-6626-126-4".parse(), Ok(isbn_10));
//!
//! let isbn_13 = Isbn13::new([9, 7, 8, 1, 4, 9, 2, 0, 6, 7, 6, 6, 5]).unwrap();
//! assert_eq!(isbn_13.hyphenate().unwrap().as_str(), "978-1-4920-6766-5");
//! assert_eq!(isbn_13.registration_group(), Ok("English language"));
//! assert_eq!("978-1-4920-6766-5".parse(), Ok(isbn_13));
//! ```
//!
//! [International Standard Book Number]: https://www.isbn-international.org/
#![cfg_attr(not(feature = "runtime-ranges"), no_std)]
#![deny(clippy::missing_errors_doc)]
#![deny(clippy::if_not_else)]

#[cfg(feature = "runtime-ranges")]
pub mod range;
#[cfg(feature = "runtime-ranges")]
pub use range::IsbnRange;
#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};

use core::char;
use core::fmt;
use core::num::ParseIntError;
use core::str::FromStr;

use arrayvec::{ArrayString, ArrayVec, CapacityError};

pub type IsbnResult<T> = Result<T, IsbnError>;

include!(concat!(env!("OUT_DIR"), "/generated.rs"));

struct Group<'a> {
    name: &'a str,
    segment_length: usize,
}

trait IsbnObject {
    fn ean_ucc_group(&self) -> Result<Group, IsbnError> {
        Isbn::get_ean_ucc_group(self.prefix_element(), self.segment(0))
    }

    fn hyphenate_with(&self, hyphen_at: [usize; 2]) -> ArrayString<[u8; 17]>;

    fn trait_hyphenate(&self) -> Result<ArrayString<[u8; 17]>, IsbnError> {
        let registration_group_segment_length = self.ean_ucc_group()?.segment_length;
        let registrant_segment_length = Isbn::get_registration_group(
            self.prefix_element(),
            self.group_prefix(registration_group_segment_length),
            self.segment(registration_group_segment_length),
        )?
        .segment_length;

        let hyphen_at = [
            registration_group_segment_length,
            registration_group_segment_length + registrant_segment_length,
        ];
        Ok(self.hyphenate_with(hyphen_at))
    }

    fn trait_registration_group(&self) -> Result<&str, IsbnError> {
        let registration_group_segment_length = self.ean_ucc_group()?.segment_length;

        Ok(Isbn::get_registration_group(
            self.prefix_element(),
            self.group_prefix(registration_group_segment_length),
            self.segment(registration_group_segment_length),
        )?
        .name)
    }

    fn prefix_element(&self) -> u16;

    fn segment(&self, base: usize) -> u32;

    fn group_prefix(&self, length: usize) -> u32;
}

/// An International Standard Book Number, either ISBN10 or ISBN13.
///
/// # Examples
///
/// ```
/// use isbn2::{Isbn, Isbn10, Isbn13};
///
/// let isbn_10 = Isbn::_10(Isbn10::new([8, 9, 6, 6, 2, 6, 1, 2, 6, 4]).unwrap());
/// let isbn_13 = Isbn::_13(Isbn13::new([9, 7, 8, 1, 4, 9, 2, 0, 6, 7, 6, 6, 5]).unwrap());
///
/// assert_eq!("89-6626-126-4".parse(), Ok(isbn_10));
/// assert_eq!("978-1-4920-6766-5".parse(), Ok(isbn_13));
/// ```
#[derive(Debug, PartialEq, Clone, Eq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub enum Isbn {
    _10(Isbn10),
    _13(Isbn13),
}

impl Isbn {
    /// Hyphenate an ISBN into its parts:
    ///
    /// * GS1 Prefix (ISBN-13 only)
    /// * Registration group
    /// * Registrant
    /// * Publication
    /// * Check digit
    ///
    /// ```
    /// use isbn2::{Isbn, Isbn10, Isbn13};
    ///
    /// let isbn_10 = Isbn::_10(Isbn10::new([8, 9, 6, 6, 2, 6, 1, 2, 6, 4]).unwrap());
    /// let isbn_13 = Isbn::_13(Isbn13::new([9, 7, 8, 1, 4, 9, 2, 0, 6, 7, 6, 6, 5]).unwrap());
    ///
    /// assert_eq!(isbn_10.hyphenate().unwrap().as_str(), "89-6626-126-4");
    /// assert_eq!(isbn_13.hyphenate().unwrap().as_str(), "978-1-4920-6766-5");
    /// ```
    /// # Errors
    /// If the ISBN is not valid, as determined by the current ISBN rules, an error will be
    /// returned.
    pub fn hyphenate(&self) -> Result<ArrayString<[u8; 17]>, IsbnError> {
        match self {
            Isbn::_10(ref c) => c.hyphenate(),
            Isbn::_13(ref c) => c.hyphenate(),
        }
    }

    /// Retrieve the name of the registration group.
    ///
    /// ```
    /// use isbn2::{Isbn, Isbn10, Isbn13};
    ///
    /// let isbn_10 = Isbn::_10(Isbn10::new([8, 9, 6, 6, 2, 6, 1, 2, 6, 4]).unwrap());
    /// let isbn_13 = Isbn::_13(Isbn13::new([9, 7, 8, 1, 4, 9, 2, 0, 6, 7, 6, 6, 5]).unwrap());
    ///
    /// assert_eq!(isbn_10.registration_group(), Ok("Korea, Republic"));
    /// assert_eq!(isbn_13.registration_group(), Ok("English language"));
    /// ```
    ///
    /// # Errors
    /// If the ISBN is not valid, as determined by the current ISBN rules, an error will be
    /// returned.
    pub fn registration_group(&self) -> Result<&str, IsbnError> {
        match self {
            Isbn::_10(ref c) => c.registration_group(),
            Isbn::_13(ref c) => c.registration_group(),
        }
    }
}

impl fmt::Display for Isbn {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Isbn::_10(ref c) => c.fmt(f),
            Isbn::_13(ref c) => c.fmt(f),
        }
    }
}

impl From<Isbn10> for Isbn {
    fn from(isbn10: Isbn10) -> Isbn {
        Isbn::_10(isbn10)
    }
}

impl From<Isbn13> for Isbn {
    fn from(isbn13: Isbn13) -> Isbn {
        Isbn::_13(isbn13)
    }
}

impl FromStr for Isbn {
    type Err = IsbnError;
    fn from_str(s: &str) -> Result<Isbn, IsbnError> {
        Parser::new(s)?.read_isbn()
    }
}

/// Used to convert ISBN digits into chars, excluding the last digit of ISBN10.
fn convert_isbn_body(d: u8) -> char {
    char::from_digit(d.into(), 10).unwrap()
}

/// Used to convert ISBN digits into chars, including the last digit of ISBN10.
fn convert_isbn10_check(d: u8) -> char {
    if d < 11 {
        ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'X'][d as usize]
    } else {
        'X'
    }
}

/// 10-digit ISBN format.
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct Isbn10 {
    digits: [u8; 10],
}

impl IsbnObject for Isbn10 {
    /// Applies two hyphens in the middle.
    fn hyphenate_with(&self, hyphen_at: [usize; 2]) -> ArrayString<[u8; 17]> {
        let mut hyphenated = ArrayString::new();

        for (i, &digit) in self.digits[0..9].iter().enumerate() {
            if hyphen_at.contains(&i) {
                hyphenated.push('-')
            }
            hyphenated.push(convert_isbn_body(digit));
        }

        hyphenated.push('-');
        hyphenated.push(convert_isbn10_check(self.digits[9]));

        hyphenated
    }

    fn prefix_element(&self) -> u16 {
        0x978
    }

    fn segment(&self, base: usize) -> u32 {
        (0..7).fold(0, |s, i| {
            s + u32::from(*self.digits.get(base + i).unwrap_or(&0)) * 10_u32.pow(6 - i as u32)
        })
    }

    fn group_prefix(&self, length: usize) -> u32 {
        let mut digits = 0;
        for &digit in &self.digits[..length] {
            digits = (digits << 4) | digit as u32;
        }
        digits
    }
}

impl Isbn10 {
    /// Creates a new ISBN10 code from 10 digits. Verifies that the checksum is correct,
    /// and that no digits are out of bounds.
    ///
    /// # Examples
    ///
    /// ```
    /// use isbn2::Isbn10;
    ///
    /// let isbn10 = Isbn10::new([8, 9, 6, 6, 2, 6, 1, 2, 6, 4]).unwrap();
    /// ```
    /// # Errors
    /// If any of the first nine digits exceed nine, or the tenth digit exceeds 10, an error
    /// will be returned. If the check digit is not correct for the ISBN, an error will also
    /// be returned.
    pub fn new(digits: [u8; 10]) -> IsbnResult<Isbn10> {
        if digits[..9].iter().any(|&digit| digit > 9) || digits[9] > 10 {
            Err(IsbnError::DigitTooLarge)
        } else if Isbn10::calculate_check_digit(&digits) == digits[9] {
            Ok(Isbn10 { digits })
        } else {
            Err(IsbnError::InvalidChecksum)
        }
    }

    /// Convert ISBN-13 to ISBN-10, if applicable.
    ///
    /// ```
    /// use isbn2::{Isbn10, Isbn13};
    ///
    /// let isbn_13 = Isbn13::new([9, 7, 8, 1, 4, 9, 2, 0, 6, 7, 6, 6, 5]).unwrap();
    /// assert_eq!(Isbn10::try_from(isbn_13), "1-4920-6766-0".parse());
    /// ```
    /// # Errors
    /// If the ISBN13 does not have a 978 prefix, it can not be downcast to an ISBN10, and an
    /// error will be returned.
    pub fn try_from(isbn13: Isbn13) -> IsbnResult<Self> {
        if isbn13.digits[..3] == [9, 7, 8] {
            let mut a = [0; 10];
            a[..9].clone_from_slice(&isbn13.digits[3..12]);
            a[9] = Isbn10::calculate_check_digit(&a);
            Ok(Isbn10 { digits: a })
        } else {
            Err(IsbnError::InvalidConversion)
        }
    }

    fn calculate_check_digit(digits: &[u8; 10]) -> u8 {
        let sum: usize = digits[..9]
            .iter()
            .enumerate()
            .map(|(i, &d)| d as usize * (10 - i))
            .sum();
        let sum_m = (sum % 11) as u8;
        if sum_m == 0 {
            0
        } else {
            11 - sum_m
        }
    }

    /// Hyphenate an ISBN-10 into its parts:
    ///
    /// * Registration group
    /// * Registrant
    /// * Publication
    /// * Check digit
    ///
    /// ```
    /// use isbn2::Isbn10;
    ///
    /// let isbn_10 = Isbn10::new([8, 9, 6, 6, 2, 6, 1, 2, 6, 4]).unwrap();
    /// assert_eq!(isbn_10.hyphenate().unwrap().as_str(), "89-6626-126-4");
    /// ```
    /// # Errors
    /// If the ISBN is not valid, as determined by the current ISBN rules, an error will be
    /// returned.
    pub fn hyphenate(&self) -> Result<ArrayString<[u8; 17]>, IsbnError> {
        self.trait_hyphenate()
    }

    /// Retrieve the name of the registration group.
    ///
    /// ```
    /// use isbn2::Isbn10;
    ///
    /// let isbn_10 = Isbn10::new([8, 9, 6, 6, 2, 6, 1, 2, 6, 4]).unwrap();
    /// assert_eq!(isbn_10.registration_group(), Ok("Korea, Republic"));
    /// ```
    /// # Errors
    /// If the ISBN is not valid, as determined by the current ISBN rules, an error will be
    /// returned.
    pub fn registration_group(&self) -> Result<&str, IsbnError> {
        self.trait_registration_group()
    }
}

impl fmt::Display for Isbn10 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut s = ArrayString::<[u8; 10]>::new();
        self.digits[0..9]
            .iter()
            .for_each(|&digit| s.push(convert_isbn_body(digit)));
        s.push(convert_isbn10_check(self.digits[9]));
        write!(f, "{}", s)
    }
}

impl FromStr for Isbn10 {
    type Err = IsbnError;
    fn from_str(s: &str) -> Result<Isbn10, IsbnError> {
        let mut p = Parser::new(s)?;
        if p.digits.len() == 10 {
            p.read_isbn10()
        } else {
            Err(IsbnError::InvalidLength)
        }
    }
}

/// 13-digit ISBN format.
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct Isbn13 {
    digits: [u8; 13],
}

impl IsbnObject for Isbn13 {
    fn hyphenate_with(&self, hyphen_at: [usize; 2]) -> ArrayString<[u8; 17]> {
        let mut hyphenated = ArrayString::new();

        for &digit in &self.digits[0..3] {
            hyphenated.push(convert_isbn_body(digit))
        }
        hyphenated.push('-');

        for (i, &digit) in self.digits[3..12].iter().enumerate() {
            if hyphen_at.contains(&i) {
                hyphenated.push('-')
            }
            hyphenated.push(convert_isbn_body(digit));
        }

        hyphenated.push('-');
        hyphenated.push(convert_isbn_body(self.digits[12]));

        hyphenated
    }

    fn prefix_element(&self) -> u16 {
        ((self.digits[0] as u16) << 8) | ((self.digits[1] as u16) << 4) | (self.digits[2] as u16)
    }

    fn segment(&self, base: usize) -> u32 {
        (3..9).fold(0, |s, i| {
            s + u32::from(*self.digits.get(base + i).unwrap_or(&0)) * 10_u32.pow(9 - i as u32)
        })
    }

    fn group_prefix(&self, length: usize) -> u32 {
        let mut digits = 0;
        for &digit in &self.digits[3..length + 3] {
            digits = (digits << 4) | digit as u32;
        }
        digits
    }
}

impl Isbn13 {
    /// Creates a new ISBN13 code from 13 digits. Verifies that the checksum is correct,
    /// and that no digits are out of bounds.
    ///
    /// # Examples
    ///
    /// ```
    /// use isbn2::Isbn13;
    ///
    /// let isbn13 = Isbn13::new([9, 7, 8, 1, 4, 9, 2, 0, 6, 7, 6, 6, 5]).unwrap();
    /// ```
    /// # Errors
    /// If any of the digits exceed nine, an error will be returned. If the check digit is not
    /// correct for the ISBN, an error will also be returned.
    pub fn new(digits: [u8; 13]) -> IsbnResult<Isbn13> {
        if digits.iter().any(|&digit| digit > 9) {
            Err(IsbnError::DigitTooLarge)
        } else if Isbn13::calculate_check_digit(&digits) == digits[12] {
            Ok(Isbn13 { digits })
        } else {
            Err(IsbnError::InvalidChecksum)
        }
    }

    fn calculate_check_digit(digits: &[u8; 13]) -> u8 {
        let mut sum = 0;
        for i in 0..6 {
            sum += u16::from(digits[i * 2] + 3 * digits[i * 2 + 1]);
        }
        let sum_m = (sum % 10) as u8;
        if sum_m == 0 {
            0
        } else {
            10 - sum_m
        }
    }

    /// Hyphenate an ISBN-13 into its parts:
    ///
    /// * GS1 Prefix
    /// * Registration group
    /// * Registrant
    /// * Publication
    /// * Check digit
    ///
    /// ```
    /// use isbn2::Isbn13;
    ///
    /// let isbn_13 = Isbn13::new([9, 7, 8, 1, 4, 9, 2, 0, 6, 7, 6, 6, 5]).unwrap();
    /// assert_eq!(isbn_13.hyphenate().unwrap().as_str(), "978-1-4920-6766-5");
    /// ```
    /// # Errors
    /// If the ISBN is not valid, as determined by the current ISBN rules, an error will be
    /// returned.
    pub fn hyphenate(&self) -> Result<ArrayString<[u8; 17]>, IsbnError> {
        self.trait_hyphenate()
    }

    /// Retrieve the name of the registration group.
    ///
    /// ```
    /// use isbn2::Isbn13;
    ///
    /// let isbn_13 = Isbn13::new([9, 7, 8, 1, 4, 9, 2, 0, 6, 7, 6, 6, 5]).unwrap();
    /// assert_eq!(isbn_13.registration_group(), Ok("English language"));
    /// ```
    /// # Errors
    /// If the ISBN is not valid, as determined by the current ISBN rules, an error will be
    /// returned.
    pub fn registration_group(&self) -> Result<&str, IsbnError> {
        self.trait_registration_group()
    }
}

impl fmt::Display for Isbn13 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut s = ArrayString::<[u8; 13]>::new();
        self.digits
            .iter()
            .for_each(|&digit| s.push(convert_isbn_body(digit)));
        write!(f, "{}", s)
    }
}

impl From<Isbn10> for Isbn13 {
    fn from(isbn10: Isbn10) -> Isbn13 {
        let mut digits = [0; 13];
        digits[..3].clone_from_slice(&[9, 7, 8]);
        digits[3..12].clone_from_slice(&isbn10.digits[0..9]);
        digits[12] = Isbn13::calculate_check_digit(&digits);
        Isbn13 { digits }
    }
}

impl FromStr for Isbn13 {
    type Err = IsbnError;
    fn from_str(s: &str) -> Result<Isbn13, IsbnError> {
        let mut p = Parser::new(s)?;
        if p.digits.len() == 13 {
            p.read_isbn13()
        } else {
            Err(IsbnError::InvalidLength)
        }
    }
}

/// An error which can be returned when parsing an ISBN.
#[derive(Debug, PartialEq)]
pub enum IsbnError {
    /// The given string is too short or too long to be an ISBN.
    InvalidLength,
    /// Encountered an invalid digit while parsing.
    InvalidDigit,
    /// Encountered an invalid ISBN registration group.
    InvalidGroup,
    /// Encountered a range not defined for use at this time.
    UndefinedRange,
    /// Failed to validate checksum.
    InvalidChecksum,
    /// Failed to convert to ISBN10.
    InvalidConversion,
    /// One or supplied more digits were too large.
    DigitTooLarge,
}

impl fmt::Display for IsbnError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            IsbnError::InvalidLength => write!(
                f,
                "The given string is too short or too long to be an ISBN."
            ),
            IsbnError::InvalidDigit => write!(f, "Encountered an invalid digit while parsing."),
            IsbnError::InvalidGroup => write!(f, "Encountered an invalid ISBN registration group."),
            IsbnError::UndefinedRange => {
                write!(f, "Encountered a range not defined for use at this time.")
            }
            IsbnError::InvalidChecksum => write!(f, "Failed to validate checksum."),
            IsbnError::InvalidConversion => write!(f, "Failed to convert to ISBN10."),
            IsbnError::DigitTooLarge => write!(
                f,
                "A supplied digit was larger than 9, or the ISBN10 check digit was larger than 10."
            ),
        }
    }
}

impl From<ParseIntError> for IsbnError {
    fn from(_: ParseIntError) -> Self {
        IsbnError::InvalidDigit
    }
}

impl From<CapacityError<u8>> for IsbnError {
    fn from(_: CapacityError<u8>) -> Self {
        IsbnError::InvalidLength
    }
}

#[derive(Debug, Clone)]
struct Parser {
    digits: ArrayVec<[u8; 13]>,
}

impl Parser {
    pub fn new<S: AsRef<str>>(s: S) -> Result<Parser, IsbnError> {
        let mut digits = ArrayVec::new();
        let mut has_x = false;

        for c in s.as_ref().chars() {
            match c {
                '-' | ' ' => {}
                'X' => {
                    if digits.len() == 9 {
                        has_x = true;
                        digits.push(10);
                    } else {
                        return Err(IsbnError::InvalidDigit);
                    }
                }
                '0'..='9' => {
                    if has_x {
                        return Err(IsbnError::InvalidDigit);
                    } else {
                        digits.try_push(c.to_digit(10).unwrap() as u8)?
                    }
                }
                _ => return Err(IsbnError::InvalidDigit),
            }
        }
        Ok(Parser { digits })
    }

    fn read_isbn(&mut self) -> Result<Isbn, IsbnError> {
        match self.digits.len() {
            10 => self.read_isbn10().map(Isbn::_10),
            13 => self.read_isbn13().map(Isbn::_13),
            _ => Err(IsbnError::InvalidLength),
        }
    }

    /// Reads an ISBN13 from self. Requires that length is checked beforehand.
    fn read_isbn13(&mut self) -> Result<Isbn13, IsbnError> {
        let mut digits = [0; 13];
        digits.clone_from_slice(&self.digits);
        let check_digit = Isbn13::calculate_check_digit(&digits);
        if check_digit == digits[12] {
            Ok(Isbn13 { digits })
        } else {
            Err(IsbnError::InvalidDigit)
        }
    }

    /// Reads an ISBN10 from self. Requires that length is checked beforehand.
    fn read_isbn10(&mut self) -> Result<Isbn10, IsbnError> {
        let mut digits = [0; 10];
        digits.clone_from_slice(&self.digits);
        let check_digit = Isbn10::calculate_check_digit(&digits);
        if check_digit == digits[9] {
            Ok(Isbn10 { digits })
        } else {
            Err(IsbnError::InvalidDigit)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_from_str_isbn10() {
        // Wikipedia ISBN-10 check digit calculation example
        assert!(Isbn::from_str("0-306-40615-2").is_ok());

        // Wikipedia ISBN-10 check digit calculation invalid example
        assert!(Isbn::from_str("99999-999-9-X").is_err());

        // Wikipedia Registrant element examples
        assert!(Isbn::from_str("99921-58-10-7").is_ok());
        assert!(Isbn::from_str("9971-5-0210-0").is_ok());
        assert!(Isbn::from_str("9971-5-0210-0").is_ok());
        assert!(Isbn::from_str("960-425-059-0").is_ok());
        assert!(Isbn::from_str("80-902734-1-6").is_ok());
        assert!(Isbn::from_str("85-359-0277-5").is_ok());
        assert!(Isbn::from_str("1-84356-028-3").is_ok());
        assert!(Isbn::from_str("0-684-84328-5").is_ok());
        assert!(Isbn::from_str("0-8044-2957-X").is_ok());
        assert!(Isbn::from_str("0-85131-041-9").is_ok());
        assert!(Isbn::from_str("0-943396-04-2").is_ok());
        assert!(Isbn::from_str("0-9752298-0-X").is_ok());
    }

    #[test]
    fn test_hyphens_no_panic() {
        assert!(Isbn::from_str("0-9752298-0-X").unwrap().hyphenate().is_ok());
        assert!(Isbn::from_str("978-3-16-148410-0")
            .unwrap()
            .hyphenate()
            .is_ok());
    }

    #[test]
    fn test_from_str_isbn13() {
        // Wikipedia Example
        assert!(Isbn13::from_str("978-3-16-148410-0").is_ok());

        // Wikipedia ISBN-13 check digit calculation example
        assert!(Isbn13::from_str("978-0-306-40615-7").is_ok());
    }

    #[test]
    fn test_invalid_isbn_strings_no_panic() {
        assert!(Isbn::from_str("L").is_err());
        assert!(Isbn::from_str("").is_err());
        assert!(Isbn::from_str("01234567890123456789").is_err());
        assert!(Isbn::from_str("ⱧňᚥɂᛢĞžᚪ©ᛟƚ¶G").is_err());

        assert!(Isbn10::from_str("").is_err());
        assert!(Isbn10::from_str("01234567890").is_err());
        assert!(Isbn10::from_str("01234567X9").is_err());
        assert!(Isbn10::from_str("012345678").is_err());

        assert!(Isbn13::from_str("").is_err());
        assert!(Isbn13::from_str("012345678901X").is_err());
        assert!(Isbn13::from_str("01234567890X2").is_err());
        assert!(Isbn13::from_str("012345678").is_err());
        assert!(Isbn13::from_str("0123456789012345").is_err());
    }

    #[test]
    fn test_isbns_do_not_accept_larger_digits() {
        let mut a = [10; 10];
        // Everything except check digit must be <= 9.
        a[9] = Isbn10::calculate_check_digit(&a);
        assert!(Isbn10::new(a).is_err());
        // Check digit can be 10.
        assert!(Isbn10::new([0, 9, 7, 5, 2, 2, 9, 8, 0, 10]).is_ok());
        // Check digits which are larger than 10 are implicitly handled by
        // the fact that calculate_check_digit returns a number from 0 to 10.
        let mut a = [10; 13];
        a[12] = Isbn13::calculate_check_digit(&a);
        assert!(Isbn13::new(a).is_err());
    }
}