]>
git.proxmox.com Git - rustc.git/blob - src/vendor/mdbook/src/book/bookconfig.rs
3 use std
::process
::exit
;
6 use std
::path
::{Path, PathBuf}
;
7 use std
::collections
::BTreeMap
;
11 #[derive(Debug, Clone)]
12 pub struct BookConfig
{
16 pub theme_path
: PathBuf
,
20 pub description
: String
,
22 pub indent_spaces
: i32,
27 pub fn new(root
: &Path
) -> Self {
29 root
: root
.to_owned(),
30 dest
: root
.join("book"),
31 src
: root
.join("src"),
32 theme_path
: root
.join("theme"),
35 author
: String
::new(),
36 description
: String
::new(),
38 indent_spaces
: 4, // indentation used for SUMMARY.md
43 pub fn read_config(&mut self, root
: &Path
) -> &mut Self {
45 debug
!("[fn]: read_config");
47 let read_file
= |path
: PathBuf
| -> String
{
48 let mut data
= String
::new();
49 let mut f
: File
= match File
::open(&path
) {
52 error
!("[*]: Failed to open {:?}", &path
);
56 if f
.read_to_string(&mut data
).is_err() {
57 error
!("[*]: Failed to read {:?}", &path
);
63 // Read book.toml or book.json if exists
65 if root
.join("book.toml").exists() {
67 debug
!("[*]: Reading config");
68 let data
= read_file(root
.join("book.toml"));
69 self.parse_from_toml_string(&data
);
71 } else if root
.join("book.json").exists() {
73 debug
!("[*]: Reading config");
74 let data
= read_file(root
.join("book.json"));
75 self.parse_from_json_string(&data
);
78 debug
!("[*]: No book.toml or book.json was found, using defaults.");
84 pub fn parse_from_toml_string(&mut self, data
: &str) -> &mut Self {
85 let config
= match toml
::from_str(data
) {
88 error
!("[*]: Toml parse errors in book.toml: {:?}", e
);
93 self.parse_from_btreemap(&config
);
98 /// Parses the string to JSON and converts it to BTreeMap<String, toml::Value>.
99 pub fn parse_from_json_string(&mut self, data
: &str) -> &mut Self {
101 let c
: serde_json
::Value
= match serde_json
::from_str(data
) {
104 error
!("[*]: JSON parse errors in book.json: {:?}", e
);
109 let config
= json_object_to_btreemap(c
.as_object().unwrap());
110 self.parse_from_btreemap(&config
);
115 pub fn parse_from_btreemap(&mut self, config
: &BTreeMap
<String
, toml
::Value
>) -> &mut Self {
117 // Title, author, description
118 if let Some(a
) = config
.get("title") {
119 self.title
= a
.to_string().replace("\"", "");
121 if let Some(a
) = config
.get("author") {
122 self.author
= a
.to_string().replace("\"", "");
124 if let Some(a
) = config
.get("description") {
125 self.description
= a
.to_string().replace("\"", "");
128 // Destination folder
129 if let Some(a
) = config
.get("dest") {
130 let mut dest
= PathBuf
::from(&a
.to_string().replace("\"", ""));
132 // If path is relative make it absolute from the parent directory of src
133 if dest
.is_relative() {
134 dest
= self.get_root().join(&dest
);
136 self.set_dest(&dest
);
140 if let Some(a
) = config
.get("src") {
141 let mut src
= PathBuf
::from(&a
.to_string().replace("\"", ""));
142 if src
.is_relative() {
143 src
= self.get_root().join(&src
);
149 if let Some(a
) = config
.get("theme_path") {
150 let mut theme_path
= PathBuf
::from(&a
.to_string().replace("\"", ""));
151 if theme_path
.is_relative() {
152 theme_path
= self.get_root().join(&theme_path
);
154 self.set_theme_path(&theme_path
);
160 pub fn get_root(&self) -> &Path
{
164 pub fn set_root(&mut self, root
: &Path
) -> &mut Self {
165 self.root
= root
.to_owned();
169 pub fn get_dest(&self) -> &Path
{
173 pub fn set_dest(&mut self, dest
: &Path
) -> &mut Self {
174 self.dest
= dest
.to_owned();
178 pub fn get_src(&self) -> &Path
{
182 pub fn set_src(&mut self, src
: &Path
) -> &mut Self {
183 self.src
= src
.to_owned();
187 pub fn get_theme_path(&self) -> &Path
{
191 pub fn set_theme_path(&mut self, theme_path
: &Path
) -> &mut Self {
192 self.theme_path
= theme_path
.to_owned();
197 pub fn json_object_to_btreemap(json
: &serde_json
::Map
<String
, serde_json
::Value
>) -> BTreeMap
<String
, toml
::Value
> {
198 let mut config
: BTreeMap
<String
, toml
::Value
> = BTreeMap
::new();
200 for (key
, value
) in json
.iter() {
202 String
::from_str(key
).unwrap(),
203 json_value_to_toml_value(value
.to_owned())
210 pub fn json_value_to_toml_value(json
: serde_json
::Value
) -> toml
::Value
{
212 serde_json
::Value
::Null
=> toml
::Value
::String("".to_string()),
213 serde_json
::Value
::Bool(x
) => toml
::Value
::Boolean(x
),
214 serde_json
::Value
::Number(ref x
) if x
.is_i64() => toml
::Value
::Integer(x
.as_i64().unwrap()),
215 serde_json
::Value
::Number(ref x
) if x
.is_u64() => toml
::Value
::Integer(x
.as_i64().unwrap()),
216 serde_json
::Value
::Number(x
) => toml
::Value
::Float(x
.as_f64().unwrap()),
217 serde_json
::Value
::String(x
) => toml
::Value
::String(x
),
218 serde_json
::Value
::Array(x
) => {
219 toml
::Value
::Array(x
.iter().map(|v
| json_value_to_toml_value(v
.to_owned())).collect())
221 serde_json
::Value
::Object(x
) => {
222 toml
::Value
::Table(json_object_to_btreemap(&x
))