Made toml serialization a bit easier

This commit is contained in:
Sofia 2018-04-18 20:27:00 +03:00
parent ada105fa6f
commit 208f1eb254
5 changed files with 55 additions and 46 deletions

View File

@ -1,17 +1,17 @@
[website]
website_name = "TestWebsite"
built_pages = []
built_pages = ["test_page/otus_test.toml"]
use_default_css = true
javascript = []
css = []
[navbar]
items = ["test", "other"]
[navbar.item.other]
title = "Other"
link = "/other.html"
[navbar.item.test]
title = "Home"
link = "/home.html"
image_url = "/img/image.png"
[navbar.item.other]
title = "Other"
link = "/other.html"

View File

@ -27,7 +27,7 @@ pub fn build(logger: &Logger, ops: &BuildOps) -> Result<(), Error> {
if config.global_config.website.use_default_css {
let css_path = file_writer::add_to_beginning(PathBuf::from("css/default.css"), "public")?;
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");
@ -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")?;
logger.log(LogLevel::DETAILER, format!("Writing {:?}", html_path));
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 {
return Err(Error::new(
LogLevel::SEVERE,

View File

@ -1,6 +1,10 @@
use std::fs::{create_dir_all, File};
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use std::error::Error as STDError;
use serde::Serialize;
use toml;
use error::Error;
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>>(
path: PathBuf,
path: &PathBuf,
content: T,
overwrite: bool,
) -> Result<(), Error> {

View File

@ -2,6 +2,7 @@ extern crate ansi_term;
extern crate pathdiff;
extern crate pulldown_cmark;
extern crate regex;
extern crate serde;
#[macro_use]
extern crate serde_derive;
#[macro_use]
@ -20,7 +21,6 @@ mod new_page;
mod file_writer;
use std::collections::HashMap;
use std::error::Error;
use std::path::PathBuf;
use structopt::StructOpt;
@ -80,6 +80,7 @@ fn main() {
item_map: hashmap,
})
}
let config = GlobalConfigToml {
website: WebsiteConfig {
website_name: ops.name,
@ -98,26 +99,11 @@ fn main() {
navbar: navbar,
};
match toml::ser::to_string(&config) {
Ok(text) => {
match file_writer::write_file(PathBuf::from("config.toml"), text, ops.overwrite)
{
Ok(_) => logger.log(LogLevel::INFO, "config.toml successfully generated"),
match file_writer::write_toml(&PathBuf::from("config.toml"), &config, ops.overwrite) {
Ok(_) => logger.log(LogLevel::INFO, "Done!"),
Err(err) => logger.log(
LogLevel::SEVERE,
format!(
"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()
),
err.severity(),
format!("Initialization failed: {}", err.description()),
),
}
}

View File

@ -1,5 +1,4 @@
use std::path::PathBuf;
use std::error::Error as STDError;
use options::NewOps;
use logger::{LogLevel, Logger};
@ -7,7 +6,6 @@ use file_writer;
use error::Error;
use config_toml::{GlobalConfigToml, PageConfig, PageConfigToml};
use toml;
use pathdiff;
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) {
Ok(text) => file_writer::write_file(toml_path.clone(), text, ops.overwrite)?,
match file_writer::write_toml(&toml_path, &page_config, ops.overwrite) {
Ok(_) => logger.log(
LogLevel::INFO,
format!("{:?} created successfully", toml_path),
),
Err(err) => {
return Err(Error::new(
LogLevel::SEVERE,
format!(
"Failed to serialize page config: {}",
err.description().to_owned()
),
err.severity(),
format!("Failed to serialize page config: {}", err.description()),
))
}
}
@ -107,7 +105,7 @@ pub fn generate_new_page(ops: NewOps, logger: &Logger) -> Result<(), Error> {
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 {
logger.log(
@ -125,15 +123,12 @@ pub fn generate_new_page(ops: NewOps, logger: &Logger) -> Result<(), Error> {
format!("Re-serializing config and writing it"),
);
match toml::ser::to_string(&config) {
Ok(text) => file_writer::write_file(PathBuf::from("config.toml"), text, ops.overwrite)?,
match file_writer::write_toml(&PathBuf::from("config.toml"), &config, ops.overwrite) {
Ok(_) => {}
Err(err) => {
return Err(Error::new(
LogLevel::SEVERE,
format!(
"Failed to serialize config.toml: {}",
err.description().to_owned()
),
err.severity(),
format!("Failed to write to config.toml: {}", err.description()),
))
}
}