index : livejrnl-rs.git

ascending towards madness

use std::str::FromStr;

use gumdrop::Options;
use crate::data::formats::LaunchArgs;
use crate::util::journal::Journal;

mod data {
	pub mod formats;
}
mod util {
	pub mod templating;
	pub mod filters;
	pub mod functions;
	pub mod journal;
}

const APP_NAME: &str = "livejrnl-rs";
const ERR_JOURNAL_NOT_FOUND: &str = "ERROR: No journal data specified.\n\nUsage:\n\tjrnl --format json | livejrnl-rs";

fn main() {
	let config_data = load_config();
	let args: LaunchArgs = LaunchArgs::parse_args_default_or_exit();
	//println!("{:#?}", args);

	let mut jrnl_json: String = String::new();
	if let Some(pipe) = read_pipe::read_pipe() {
		jrnl_json.push_str(&pipe);
	}
	else {
		println!("{}", ERR_JOURNAL_NOT_FOUND);
		return;
	}

	let journal: Journal = util::journal::parse_journal_json(&jrnl_json);
	//println!("{:#?}", journal);

	match config_data {
		Ok(config_data) => {
			println!("{:#?}", config_data);

			let template_files = util::templating::get_templates(config_data.template.clone());
			//println!("{:?}", template_files);

			let html = util::templating::render_html(config_data, journal, &template_files);

			let out_dir = std::path::PathBuf::from_str(&args.out_dir).unwrap_or_default();
			let out_file = out_dir.join("index.html");
			let out_path = out_file.as_path();

			match std::fs::create_dir_all(&out_dir) {
				Ok(()) => {
					let html: Vec<&str> = html.lines().filter(|&line| !line.trim().is_empty()).collect();
					let html = html.join("\n");
					let write = std::fs::write(out_path, html);
					match write {
						Ok(()) => println!("All done!"),
						Err(e) => println!("{:?}", e)
					}
				},
				Err(e) => println!("{:?}", e)
			}
		},
		Err(error) => println!("{:#?}", error)
	};
}

fn load_config() -> Result<data::formats::RenderConfig, confy::ConfyError> {
    let cfg: data::formats::RenderConfig = confy::load(APP_NAME, "config")?;
    Ok(cfg)
}