<commit_before>use std::path::PathBuf;
use std::fs::File;
use std::io::{Read, Result, Error, ErrorKind};
use book::bookitem::{BookItem, Chapter};

pub fn construct_bookitems(path: &PathBuf) -> Result<Vec<BookItem>> {
    debug!("[fn]: construct_bookitems");
    let mut summary = String::new();
    try!(try!(File::open(path)).read_to_string(&mut summary));

    debug!("[*]: Parse SUMMARY.md");
    let top_items = try!(parse_level(&mut summary.split('\n').collect(), 0, vec![0]));
    debug!("[*]: Done parsing SUMMARY.md");
    Ok(top_items)
}

fn parse_level(summary: &mut Vec<&str>, current_level: i32, mut section: Vec<i32>) -> Result<Vec<BookItem>> {
    debug!("[fn]: parse_level");
    let mut items: Vec<BookItem> = vec![];

    // Construct the book recursively
    while !summary.is_empty() {
        let item: BookItem;
        // Indentation level of the line to parse
        let level = try!(level(summary[0], 4));

        // if level < current_level we remove the last digit of section, exit the current function,
        // and return the parsed level to the calling function.
        if level < current_level {
            break;
        }

        // if level > current_level we call ourselves to go one level deeper
        if level > current_level {
            // Level can not be root level !!
            // Add a sub-number to section
            section.push(0);
            let last = items.pop().expect("There should be at least one item since this can't be the root level");

            item = if let BookItem::Chapter(ref s, ref ch) = last {
                let mut ch = ch.clone();
                ch.sub_items = try!(parse_level(summary, level, section.clone()));
                items.push(BookItem::Chapter(s.clone(), ch));

                // Remove the last number from the section, because we got back to our level..
                section.pop();
                continue;
            } else {
                return Err(Error::new(ErrorKind::Other,
                                      format!("Your summary.md is messed up\n\n
                        Prefix, \
                                               Suffix and Spacer elements can only exist on the root level.\n
                        \
                                               Prefix elements can only exist before any chapter and there can be \
                                               no chapters after suffix elements.")));
            };
<commit_msg>Fix unreachable code warning in parse::summary::parse_level<commit_after>use std::path::PathBuf;
use std::fs::File;
use std::io::{Read, Result, Error, ErrorKind};
use book::bookitem::{BookItem, Chapter};

pub fn construct_bookitems(path: &PathBuf) -> Result<Vec<BookItem>> {
    debug!("[fn]: construct_bookitems");
    let mut summary = String::new();
    try!(try!(File::open(path)).read_to_string(&mut summary));

    debug!("[*]: Parse SUMMARY.md");
    let top_items = try!(parse_level(&mut summary.split('\n').collect(), 0, vec![0]));
    debug!("[*]: Done parsing SUMMARY.md");
    Ok(top_items)
}

fn parse_level(summary: &mut Vec<&str>, current_level: i32, mut section: Vec<i32>) -> Result<Vec<BookItem>> {
    debug!("[fn]: parse_level");
    let mut items: Vec<BookItem> = vec![];

    // Construct the book recursively
    while !summary.is_empty() {
        let item: BookItem;
        // Indentation level of the line to parse
        let level = try!(level(summary[0], 4));

        // if level < current_level we remove the last digit of section, exit the current function,
        // and return the parsed level to the calling function.
        if level < current_level {
            break;
        }

        // if level > current_level we call ourselves to go one level deeper
        if level > current_level {
            // Level can not be root level !!
            // Add a sub-number to section
            section.push(0);
            let last = items.pop().expect("There should be at least one item since this can't be the root level");

            if let BookItem::Chapter(ref s, ref ch) = last {
                let mut ch = ch.clone();
                ch.sub_items = try!(parse_level(summary, level, section.clone()));
                items.push(BookItem::Chapter(s.clone(), ch));

                // Remove the last number from the section, because we got back to our level..
                section.pop();
                continue;
            } else {
                return Err(Error::new(ErrorKind::Other,
                                      format!("Your summary.md is messed up\n\n
                        Prefix, \
                                               Suffix and Spacer elements can only exist on the root level.\n
                        \
                                               Prefix elements can only exist before any chapter and there can be \
                                               no chapters after suffix elements.")));
            };

{'fraction_non_alphanumeric': 0.1182493475205782, 'fraction_numerical': 0.0036137321822927123, 'mean_word_length': 1.8897911832946637, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4361539', 'n_tokens_mistral': 1312, 'n_tokens_neox': 1257, 'n_words': 436}