Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
refactored verbose printing
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard70NL committed Apr 15, 2019
1 parent 553c0aa commit 27058e2
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 35 deletions.
45 changes: 28 additions & 17 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
/************************************************************************************************/

use super::site::SiteConfig;
use super::util::verbose_println;
use super::yasg::YasgClass;
use super::yasg::YasgFile;
use crate::site::SiteConfig;
use crate::verbose::Verbose;
use crate::yasg::YasgClass;
use crate::yasg::YasgFile;
use std::fs::copy;
use std::fs::create_dir_all;
use std::path::PathBuf;

/************************************************************************************************/

pub fn perform_build(verbose: bool) {
pub fn perform_build(verbose: &mut Verbose) {
println!("building...");
verbose.increate_indent();

verbose_println(verbose, " Reading site configuration from Site.yaml.");
verbose.println("Reading site configuration from Site.yaml.");
verbose.increate_indent();
let config = SiteConfig::read_from_yaml(verbose).unwrap();
verbose.decrease_indent();

verbose_println(verbose, " Building file list.");
verbose.println("Building file list.");
verbose.increate_indent();
let file_list = build_file_list(&config);
verbose.decrease_indent();

verbose_println(verbose, " Processing files.");
verbose.println("Processing files.");
verbose.increate_indent();
process_files(verbose, &config, &file_list);
verbose.decrease_indent();

verbose.decrease_indent();
println!("done!");
}

Expand Down Expand Up @@ -53,7 +61,7 @@ fn scan_directory(config: &SiteConfig, file_list: &mut Vec<PathBuf>, dir: &PathB

/************************************************************************************************/

fn process_files(verbose: bool, config: &SiteConfig, file_list: &[PathBuf]) {
fn process_files(verbose: &mut Verbose, config: &SiteConfig, file_list: &[PathBuf]) {
let mut template_list = Vec::new();
let mut page_list = Vec::new();

Expand All @@ -74,18 +82,18 @@ fn process_files(verbose: bool, config: &SiteConfig, file_list: &[PathBuf]) {
}
}

verbose.println("Processing pages.");
verbose.increate_indent();
process_pages(verbose, config, &template_list, &page_list);
verbose.decrease_indent();
}

/************************************************************************************************/

fn copy_file(verbose: bool, config: &SiteConfig, from_path: &PathBuf) {
fn copy_file(verbose: &mut Verbose, config: &SiteConfig, from_path: &PathBuf) {
let relative = config.relative_to_input(from_path);

verbose_println(
verbose,
format!(" Copy {}.", relative.to_str().unwrap()).as_str(),
);
verbose.println(format!("Copy {}.", relative.to_str().unwrap()).as_str());

let mut to = config.output.clone();
to.push(relative);
Expand All @@ -101,9 +109,12 @@ fn copy_file(verbose: bool, config: &SiteConfig, from_path: &PathBuf) {

/************************************************************************************************/

fn process_pages(verbose: bool, _config: &SiteConfig, templates: &[YasgFile], pages: &[YasgFile]) {
verbose_println(verbose, " Processing pages.");

fn process_pages(
_verbose: &mut Verbose,
_config: &SiteConfig,
templates: &[YasgFile],
pages: &[YasgFile],
) {
dbg!(templates);
dbg!(pages);

Expand Down
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod builder;
mod error;
mod site;
mod util;
mod verbose;
mod yasg;

/************************************************************************************************/
Expand All @@ -17,6 +18,7 @@ use builder::perform_build;
use clap::Arg;
use clap::SubCommand;
use std::io;
use verbose::Verbose;

/************************************************************************************************/

Expand All @@ -39,8 +41,11 @@ fn main() {
}
Some(cmd) => {
if cmd.name == "build" {
let verbose = cmd.matches.is_present("verbose");
perform_build(verbose);
let mut verbose = Verbose::new();
if cmd.matches.is_present("verbose") {
verbose.enable();
}
perform_build(&mut verbose);
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/site.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/************************************************************************************************/

use super::error::Error;
use super::util::verbose_println;
use crate::error::Error;
use crate::verbose::Verbose;
use std::fs::create_dir_all;
use std::fs::File;
use std::io::prelude::*;
Expand Down Expand Up @@ -35,7 +35,7 @@ impl SiteConfig {

/*------------------------------------------------------------------------------------------*/

pub fn read_from_yaml(verbose: bool) -> Result<SiteConfig, Error> {
pub fn read_from_yaml(verbose: &mut Verbose) -> Result<SiteConfig, Error> {
let mut sc = SiteConfig::new();

sc.parse_yaml();
Expand Down Expand Up @@ -92,18 +92,17 @@ impl SiteConfig {

/*------------------------------------------------------------------------------------------*/

fn process_io_paths(&mut self, verbose: bool) {
fn process_io_paths(&mut self, verbose: &mut Verbose) {
if self.input.exists() {
self.input = self.input.canonicalize().unwrap();
};

if self.output.exists() {
self.output = self.output.canonicalize().unwrap();
} else {
verbose_println(
verbose,
verbose.println(
format!(
" Creating output directory '{}'.",
"Creating output directory '{}'.",
self.output.to_str().unwrap()
)
.as_str(),
Expand Down
8 changes: 0 additions & 8 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ use yaml_rust::yaml::Yaml;

/************************************************************************************************/

pub fn verbose_println(verbose: bool, message: &str) {
if verbose {
println!("{}", message);
}
}

/************************************************************************************************/

pub fn yaml_value_as_string(value: &Yaml) -> Option<String> {
match value {
Yaml::String(s) => Some(s.clone()),
Expand Down
64 changes: 64 additions & 0 deletions src/verbose.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/************************************************************************************************/

pub struct Verbose {
enabled: bool,
indent: usize,
}

/************************************************************************************************/

impl Verbose {
/*------------------------------------------------------------------------------------------*/

pub fn new() -> Verbose {
Verbose {
enabled: false,
indent: 0,
}
}

/*------------------------------------------------------------------------------------------*/

pub fn enable(&mut self) {
self.enabled = true;
}

/*------------------------------------------------------------------------------------------*/

pub fn _disable(&mut self) {
self.enabled = false;
}

/*------------------------------------------------------------------------------------------*/

pub fn _is_enabled(&self) -> bool {
return self.enabled;
}

/*------------------------------------------------------------------------------------------*/

pub fn increate_indent(&mut self) {
self.indent = self.indent + 1;
}

/*------------------------------------------------------------------------------------------*/

pub fn decrease_indent(&mut self) {
if self.indent > 0 {
self.indent = self.indent - 1;
}
}

/*------------------------------------------------------------------------------------------*/

pub fn println(&self, line: &str) {
if self.enabled {
let indent = " ".repeat(self.indent);
println!("{}{}", indent, line);
}
}

/*------------------------------------------------------------------------------------------*/
}

/************************************************************************************************/
2 changes: 1 addition & 1 deletion src/yasg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/************************************************************************************************/

use super::util::yaml_value_as_string;
use crate::util::yaml_value_as_string;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
Expand Down

0 comments on commit 27058e2

Please sign in to comment.