Made toml serialization a bit easier
This commit is contained in:
parent
ada105fa6f
commit
208f1eb254
12
config.toml
12
config.toml
@ -1,17 +1,17 @@
|
|||||||
[website]
|
[website]
|
||||||
website_name = "Test Website"
|
website_name = "TestWebsite"
|
||||||
built_pages = []
|
built_pages = ["test_page/otus_test.toml"]
|
||||||
use_default_css = true
|
use_default_css = true
|
||||||
javascript = []
|
javascript = []
|
||||||
css = []
|
css = []
|
||||||
|
|
||||||
[navbar]
|
[navbar]
|
||||||
items = ["test", "other"]
|
items = ["test", "other"]
|
||||||
|
[navbar.item.other]
|
||||||
|
title = "Other"
|
||||||
|
link = "/other.html"
|
||||||
|
|
||||||
[navbar.item.test]
|
[navbar.item.test]
|
||||||
title = "Home"
|
title = "Home"
|
||||||
link = "/home.html"
|
link = "/home.html"
|
||||||
image_url = "/img/image.png"
|
image_url = "/img/image.png"
|
||||||
|
|
||||||
[navbar.item.other]
|
|
||||||
title = "Other"
|
|
||||||
link = "/other.html"
|
|
||||||
|
@ -27,7 +27,7 @@ pub fn build(logger: &Logger, ops: &BuildOps) -> Result<(), Error> {
|
|||||||
if config.global_config.website.use_default_css {
|
if config.global_config.website.use_default_css {
|
||||||
let css_path = file_writer::add_to_beginning(PathBuf::from("css/default.css"), "public")?;
|
let css_path = file_writer::add_to_beginning(PathBuf::from("css/default.css"), "public")?;
|
||||||
logger.log(LogLevel::DETAIL, format!("Adding {:?}", css_path));
|
logger.log(LogLevel::DETAIL, format!("Adding {:?}", css_path));
|
||||||
file_writer::write_file(css_path, DEFAULT_CSS, ops.overwrite)?;
|
file_writer::write_file(&css_path, DEFAULT_CSS, ops.overwrite)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.log(LogLevel::INFO, "Generating page templates");
|
logger.log(LogLevel::INFO, "Generating page templates");
|
||||||
@ -62,7 +62,7 @@ pub fn build(logger: &Logger, ops: &BuildOps) -> Result<(), Error> {
|
|||||||
let html_path = file_writer::add_to_beginning(PathBuf::from(html_path), "public")?;
|
let html_path = file_writer::add_to_beginning(PathBuf::from(html_path), "public")?;
|
||||||
logger.log(LogLevel::DETAILER, format!("Writing {:?}", html_path));
|
logger.log(LogLevel::DETAILER, format!("Writing {:?}", html_path));
|
||||||
if let Some(render) = renders.get(idx) {
|
if let Some(render) = renders.get(idx) {
|
||||||
file_writer::write_file(html_path, render.clone(), ops.overwrite)?;
|
file_writer::write_file(&html_path, render.clone(), ops.overwrite)?;
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
LogLevel::SEVERE,
|
LogLevel::SEVERE,
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
use std::fs::{create_dir_all, File};
|
use std::fs::{create_dir_all, File};
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::error::Error as STDError;
|
||||||
|
|
||||||
|
use serde::Serialize;
|
||||||
|
use toml;
|
||||||
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use logger::LogLevel;
|
use logger::LogLevel;
|
||||||
@ -17,8 +21,32 @@ pub fn add_to_beginning<T: Into<String>>(path: PathBuf, to_add: T) -> Result<Pat
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn write_toml<T: Serialize>(path: &PathBuf, data: &T, overwrite: bool) -> Result<(), Error> {
|
||||||
|
match toml::ser::to_string(data) {
|
||||||
|
Ok(text) => match write_file(&path, text, overwrite) {
|
||||||
|
Ok(_) => Ok(()),
|
||||||
|
Err(err) => Err(Error::new(
|
||||||
|
LogLevel::SEVERE,
|
||||||
|
format!(
|
||||||
|
"Failed to write {:?}: {}",
|
||||||
|
path,
|
||||||
|
err.description().to_owned()
|
||||||
|
),
|
||||||
|
)),
|
||||||
|
},
|
||||||
|
Err(err) => Err(Error::new(
|
||||||
|
LogLevel::SEVERE,
|
||||||
|
format!(
|
||||||
|
"Failed to serialize {:?}: {}",
|
||||||
|
path,
|
||||||
|
err.description().to_owned()
|
||||||
|
),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn write_file<T: Into<String>>(
|
pub fn write_file<T: Into<String>>(
|
||||||
path: PathBuf,
|
path: &PathBuf,
|
||||||
content: T,
|
content: T,
|
||||||
overwrite: bool,
|
overwrite: bool,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
|
26
src/main.rs
26
src/main.rs
@ -2,6 +2,7 @@ extern crate ansi_term;
|
|||||||
extern crate pathdiff;
|
extern crate pathdiff;
|
||||||
extern crate pulldown_cmark;
|
extern crate pulldown_cmark;
|
||||||
extern crate regex;
|
extern crate regex;
|
||||||
|
extern crate serde;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
@ -20,7 +21,6 @@ mod new_page;
|
|||||||
mod file_writer;
|
mod file_writer;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::error::Error;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
@ -80,6 +80,7 @@ fn main() {
|
|||||||
item_map: hashmap,
|
item_map: hashmap,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
let config = GlobalConfigToml {
|
let config = GlobalConfigToml {
|
||||||
website: WebsiteConfig {
|
website: WebsiteConfig {
|
||||||
website_name: ops.name,
|
website_name: ops.name,
|
||||||
@ -98,26 +99,11 @@ fn main() {
|
|||||||
navbar: navbar,
|
navbar: navbar,
|
||||||
};
|
};
|
||||||
|
|
||||||
match toml::ser::to_string(&config) {
|
match file_writer::write_toml(&PathBuf::from("config.toml"), &config, ops.overwrite) {
|
||||||
Ok(text) => {
|
Ok(_) => logger.log(LogLevel::INFO, "Done!"),
|
||||||
match file_writer::write_file(PathBuf::from("config.toml"), text, ops.overwrite)
|
|
||||||
{
|
|
||||||
Ok(_) => logger.log(LogLevel::INFO, "config.toml successfully generated"),
|
|
||||||
Err(err) => logger.log(
|
Err(err) => logger.log(
|
||||||
LogLevel::SEVERE,
|
err.severity(),
|
||||||
format!(
|
format!("Initialization failed: {}", err.description()),
|
||||||
"Failed to write config.toml: {}",
|
|
||||||
err.description().to_owned()
|
|
||||||
),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => logger.log(
|
|
||||||
LogLevel::SEVERE,
|
|
||||||
format!(
|
|
||||||
"Failed to serialize config.toml: {}",
|
|
||||||
err.description().to_owned()
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::error::Error as STDError;
|
|
||||||
|
|
||||||
use options::NewOps;
|
use options::NewOps;
|
||||||
use logger::{LogLevel, Logger};
|
use logger::{LogLevel, Logger};
|
||||||
@ -7,7 +6,6 @@ use file_writer;
|
|||||||
use error::Error;
|
use error::Error;
|
||||||
use config_toml::{GlobalConfigToml, PageConfig, PageConfigToml};
|
use config_toml::{GlobalConfigToml, PageConfig, PageConfigToml};
|
||||||
|
|
||||||
use toml;
|
|
||||||
use pathdiff;
|
use pathdiff;
|
||||||
|
|
||||||
const PLACEHOLDER_MARKDOWN: &'static str =
|
const PLACEHOLDER_MARKDOWN: &'static str =
|
||||||
@ -89,15 +87,15 @@ pub fn generate_new_page(ops: NewOps, logger: &Logger) -> Result<(), Error> {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
match toml::ser::to_string_pretty(&page_config) {
|
match file_writer::write_toml(&toml_path, &page_config, ops.overwrite) {
|
||||||
Ok(text) => file_writer::write_file(toml_path.clone(), text, ops.overwrite)?,
|
Ok(_) => logger.log(
|
||||||
|
LogLevel::INFO,
|
||||||
|
format!("{:?} created successfully", toml_path),
|
||||||
|
),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
LogLevel::SEVERE,
|
err.severity(),
|
||||||
format!(
|
format!("Failed to serialize page config: {}", err.description()),
|
||||||
"Failed to serialize page config: {}",
|
|
||||||
err.description().to_owned()
|
|
||||||
),
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -107,7 +105,7 @@ pub fn generate_new_page(ops: NewOps, logger: &Logger) -> Result<(), Error> {
|
|||||||
format!("Creating a new .md file at {:?}", markdown_path),
|
format!("Creating a new .md file at {:?}", markdown_path),
|
||||||
);
|
);
|
||||||
|
|
||||||
file_writer::write_file(markdown_path, PLACEHOLDER_MARKDOWN, ops.overwrite)?;
|
file_writer::write_file(&markdown_path, PLACEHOLDER_MARKDOWN, ops.overwrite)?;
|
||||||
|
|
||||||
if !ops.no_modify_config {
|
if !ops.no_modify_config {
|
||||||
logger.log(
|
logger.log(
|
||||||
@ -125,15 +123,12 @@ pub fn generate_new_page(ops: NewOps, logger: &Logger) -> Result<(), Error> {
|
|||||||
format!("Re-serializing config and writing it"),
|
format!("Re-serializing config and writing it"),
|
||||||
);
|
);
|
||||||
|
|
||||||
match toml::ser::to_string(&config) {
|
match file_writer::write_toml(&PathBuf::from("config.toml"), &config, ops.overwrite) {
|
||||||
Ok(text) => file_writer::write_file(PathBuf::from("config.toml"), text, ops.overwrite)?,
|
Ok(_) => {}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
LogLevel::SEVERE,
|
err.severity(),
|
||||||
format!(
|
format!("Failed to write to config.toml: {}", err.description()),
|
||||||
"Failed to serialize config.toml: {}",
|
|
||||||
err.description().to_owned()
|
|
||||||
),
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user