Compare commits

..

No commits in common. "20da7322f72fc8d40f3266c5fdad72915396af62" and "8f7e61225d64f41fbc7a277c2e190e2e26738508" have entirely different histories.

4 changed files with 13 additions and 27 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "oink" name = "oink"
version = "0.0.2" version = "0.0.1"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -1,4 +0,0 @@
[toolchain]
channel = "nightly"

View file

@ -1,4 +1,3 @@
#![feature(error_in_core)]
use std::process::exit; use std::process::exit;
use pico_args::Arguments; use pico_args::Arguments;

View file

@ -5,7 +5,6 @@ use std::{
io::Write, io::Write,
path::Path path::Path
}; };
use core::error::Error;
use tera::{ Context, Tera }; use tera::{ Context, Tera };
use termion::{ use termion::{
@ -14,11 +13,6 @@ use termion::{
}; };
use toml::{ map::Map, Value }; use toml::{ map::Map, Value };
static FAIL: Fg<color::LightRed> = Fg(color::LightRed);
static SUCCESS: Fg<color::LightGreen> = Fg(color::LightGreen);
static WARN: Fg<color::Yellow> = Fg(color::Yellow);
static RESET: Fg<color::Reset> = Fg(color::Reset);
pub fn apply(targets: &Vec<Map<String, Value>>) { pub fn apply(targets: &Vec<Map<String, Value>>) {
let home = var("HOME").unwrap(); let home = var("HOME").unwrap();
println!("{}running apply{}", style::Bold, style::Reset); println!("{}running apply{}", style::Bold, style::Reset);
@ -31,18 +25,18 @@ pub fn apply(targets: &Vec<Map<String, Value>>) {
if path.is_none() { if path.is_none() {
if i_name.is_some() { if i_name.is_some() {
let name = i_name.unwrap().as_str().unwrap(); let name = i_name.unwrap().as_str().unwrap();
println!(" {WARN}\"{name}\" is missing its path property; skipping{RESET}"); println!(" \"{name}\" is missing its path property; skipping");
} else { println!(" {WARN}skipping empty target{RESET}"); } } else { println!(" skipping empty target"); }
continue; continue;
} }
if i_name.is_none() { if i_name.is_none() {
println!(" {WARN}target missing name; skipping{RESET}"); println!(" target missing name; skipping");
continue; continue;
} }
// print applying text // print applying text
let name = i_name.unwrap().as_str().unwrap(); let name = i_name.unwrap().as_str().unwrap();
println!(" applying \"{name}\":"); println!(" applying \"{name}\":");
// set up paths // set up paths
let destination = Path::new(path.unwrap().as_str().unwrap()); let destination = Path::new(path.unwrap().as_str().unwrap());
@ -51,8 +45,8 @@ pub fn apply(targets: &Vec<Map<String, Value>>) {
// copy and print // copy and print
let result = fs::copy(source, destination); let result = fs::copy(source, destination);
if result.is_err() { println!(" {FAIL}failed to copy!{RESET}"); } if result.is_err() { println!(" failed to copy!"); }
else { println!(" {SUCCESS}completed successfully{RESET}"); } else { println!(" completed successfully"); }
} }
} }
@ -64,22 +58,19 @@ pub fn build(targets: &Vec<Map<String, Value>>, tera: &mut Tera, context: &Conte
let i_name = target.get("name"); let i_name = target.get("name");
// handle empty names gracefully // handle empty names gracefully
if i_name.is_none() { if i_name.is_none() {
println!(" {WARN}target missing name; skipping{RESET}"); println!(" {}target missing name; skipping{}", Fg(color::Yellow), Fg(color::Reset));
continue; continue;
} }
// print building text // print building text
let name = i_name.unwrap().as_str().unwrap(); let name = i_name.unwrap().as_str().unwrap();
println!(" building \"{name}\":"); println!(" building \"{name}\":");
// render template // render template
let render = tera.render(name, context); let render = tera.render(name, context);
// handle rendering errors gracefully // handle rendering errors gracefully
if render.is_err() { if render.is_err() {
let error = render.err().unwrap(); println!(" failed to render template");
let message = error.source().unwrap();
println!(" {FAIL}failed to render template:\n {message}{RESET}");
continue; continue;
} }
@ -89,14 +80,14 @@ pub fn build(targets: &Vec<Map<String, Value>>, tera: &mut Tera, context: &Conte
let path = Path::new(&destination); let path = Path::new(&destination);
let i_file = File::create(path); let i_file = File::create(path);
if i_file.is_err() { if i_file.is_err() {
println!(" {FAIL}failed to create destination file at {path:?}{RESET}"); println!(" failed to create destination file at {path:?}");
continue; continue;
} }
let mut file = i_file.unwrap(); let mut file = i_file.unwrap();
// write to destination file // write to destination file
let written = write!(&mut file, "{output}"); let written = write!(&mut file, "{output}");
if written.is_err() { println!(" {FAIL}failed to write to destination file at {path:?}{RESET}"); } if written.is_err() { println!(" failed to write to destination file at {path:?}"); }
else { println!(" {SUCCESS}completed successfully{RESET}"); } else { println!(" completed successfully"); }
} }
} }