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
use rustc_serialize::json::Json;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone)]
pub struct BookConfig {
    pub title: String,
    pub author: String,
    pub description: String,
    root: PathBuf,
    pub dest: PathBuf,
    pub src: PathBuf,
    pub indent_spaces: i32,
    multilingual: bool,
}


impl BookConfig {
    pub fn new(root: &Path) -> Self {
        BookConfig {
            title: String::new(),
            author: String::new(),
            description: String::new(),
            root: root.to_owned(),
            dest: root.join("book"),
            src: root.join("src"),
            indent_spaces: 4, // indentation used for SUMMARY.md
            multilingual: false,
        }
    }

    pub fn read_config(&mut self, root: &Path) -> &mut Self {

        debug!("[fn]: read_config");

        // If the file does not exist, return early
        let mut config_file = match File::open(root.join("book.json")) {
            Ok(f) => f,
            Err(_) => {
                debug!("[*]: Failed to open {:?}", root.join("book.json"));
                return self;
            },
        };

        debug!("[*]: Reading config");
        let mut data = String::new();

        // Just return if an error occured.
        // I would like to propagate the error, but I have to return `&self`
        if let Err(_) = config_file.read_to_string(&mut data) {
            return self;
        }

        // Convert to JSON
        if let Ok(config) = Json::from_str(&data) {
            // Extract data

            debug!("[*]: Extracting data from config");
            // Title, author, description
            if let Some(a) = config.find_path(&["title"]) {
                self.title = a.to_string().replace("\"", "")
            }
            if let Some(a) = config.find_path(&["author"]) {
                self.author = a.to_string().replace("\"", "")
            }
            if let Some(a) = config.find_path(&["description"]) {
                self.description = a.to_string().replace("\"", "")
            }

            // Destination
            if let Some(a) = config.find_path(&["dest"]) {
                let dest = PathBuf::from(&a.to_string().replace("\"", ""));

                // If path is relative make it absolute from the parent directory of src
                match dest.is_relative() {
                    true => {
                        let dest = self.get_root().join(&dest).to_owned();
                        self.set_dest(&dest);
                    },
                    false => {
                        self.set_dest(&dest);
                    },
                }
            }
        }

        self
    }

    pub fn get_root(&self) -> &Path {
        &self.root
    }

    pub fn set_root(&mut self, root: &Path) -> &mut Self {
        self.root = root.to_owned();
        self
    }

    pub fn get_dest(&self) -> &Path {
        &self.dest
    }

    pub fn set_dest(&mut self, dest: &Path) -> &mut Self {
        self.dest = dest.to_owned();
        self
    }

    pub fn get_src(&self) -> &Path {
        &self.src
    }

    pub fn set_src(&mut self, src: &Path) -> &mut Self {
        self.src = src.to_owned();
        self
    }
}