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
use std::path::{PathBuf, Path};

use super::tomlconfig::TomlPlaypenConfig;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PlaypenConfig {
    editor: PathBuf,
    editable: bool,
}

impl PlaypenConfig {
    /// Creates a new `PlaypenConfig` for playpen configuration.
    ///
    /// ```
    /// # use std::path::PathBuf;
    /// # use mdbook::config::PlaypenConfig;
    /// #
    /// let editor = PathBuf::from("root/editor");
    /// let config = PlaypenConfig::new(PathBuf::from("root"));
    ///
    /// assert_eq!(config.get_editor(), &editor);
    /// assert_eq!(config.is_editable(), false);
    /// ```
    pub fn new<T: Into<PathBuf>>(root: T) -> Self {
        PlaypenConfig {
            editor: root.into().join("editor"),
            editable: false,
        }
    }

    pub fn fill_from_tomlconfig<T: Into<PathBuf>>(&mut self, root: T, tomlplaypenconfig: TomlPlaypenConfig) -> &mut Self {
        let root = root.into();
        
        if let Some(editor) = tomlplaypenconfig.editor {
            if editor.is_relative() {
                self.editor = root.join(editor);
            } else {
                self.editor = editor;
            }
        }

        if let Some(editable) = tomlplaypenconfig.editable {
            self.editable = editable;
        }

        self
    }

    pub fn is_editable(&self) -> bool {
        self.editable
    }

    pub fn get_editor(&self) -> &Path {
        &self.editor
    }

    pub fn set_editor<T: Into<PathBuf>>(&mut self, root: T, editor: T) -> &mut Self {
        let editor = editor.into();

        if editor.is_relative() {
            self.editor = root.into().join(editor);
        } else {
            self.editor = editor;
        }

        self
    }
}