imporved error handling
This commit is contained in:
33
src/app/builder/asciidoctor.rs
Normal file
33
src/app/builder/asciidoctor.rs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
use std::process;
|
||||||
|
|
||||||
|
use super::Builder;
|
||||||
|
|
||||||
|
pub struct AsciiDoctorBuilder;
|
||||||
|
|
||||||
|
impl Builder for AsciiDoctorBuilder {
|
||||||
|
fn build(&self, in_path: &str, out_path: &str) -> Result<(), String> {
|
||||||
|
let result = process::Command::new("asciidoctor")
|
||||||
|
.arg(format!("{in_path}"))
|
||||||
|
.arg(format!("--out-file={out_path}"))
|
||||||
|
.output();
|
||||||
|
|
||||||
|
if let Ok(success) = result {
|
||||||
|
if success.stderr.len() == 0 {
|
||||||
|
return Ok(());
|
||||||
|
} else {
|
||||||
|
return Err(AsciiDoctorBuilder::from_utf8(success.stderr));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err("command failed to execute".to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsciiDoctorBuilder {
|
||||||
|
fn from_utf8(input: Vec<u8>) -> String {
|
||||||
|
return match String::from_utf8(input) {
|
||||||
|
Ok(m) => m,
|
||||||
|
Err(e) => panic!("could not print error message: {}", e),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
5
src/app/builder/mod.rs
Normal file
5
src/app/builder/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
pub mod asciidoctor;
|
||||||
|
|
||||||
|
pub trait Builder {
|
||||||
|
fn build(&self, in_path: &str, out_path: &str) -> Result<(), String>;
|
||||||
|
}
|
||||||
@@ -1,45 +1,47 @@
|
|||||||
use std::{collections::HashMap, env, fs, process};
|
use std::{collections::HashMap, env, fs};
|
||||||
|
|
||||||
|
use crate::app::builder::{Builder, asciidoctor::AsciiDoctorBuilder};
|
||||||
|
|
||||||
use super::traits::Command;
|
use super::traits::Command;
|
||||||
|
|
||||||
pub struct Build;
|
pub struct Build {
|
||||||
|
builder: Box<dyn Builder>
|
||||||
|
}
|
||||||
|
|
||||||
impl Build {
|
impl Build {
|
||||||
fn build_dir(&self, path: String) -> Result<(), String> {
|
fn build_dir(&self, path: String) -> Vec<Result<(), String>> {
|
||||||
|
let mut results = vec![];
|
||||||
let Ok(dirs) = fs::read_dir(path) else {
|
let Ok(dirs) = fs::read_dir(path) else {
|
||||||
return Err("could not read file system".to_string());
|
return vec![Err("could not read file system".to_string())]
|
||||||
};
|
};
|
||||||
|
|
||||||
for result in dirs {
|
for result in dirs {
|
||||||
let Ok(entry) = result else {
|
let Ok(entry) = result else {
|
||||||
return Err("could not read entry".to_string());
|
return vec![Err("could not read entry".to_string())];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let path = entry.path().to_str()
|
||||||
|
.expect("could not get text path");
|
||||||
|
|
||||||
if entry.path().is_dir() {
|
if entry.path().is_dir() {
|
||||||
self.build_dir(entry.path().to_str().unwrap().to_string());
|
results = [
|
||||||
|
results, vec![self.build_file(&path)]
|
||||||
|
].concat()
|
||||||
} else {
|
} else {
|
||||||
self.build_file(entry.path().to_str().unwrap().to_string());
|
results.push(self.build_file(&path));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Err("No files to build".to_string());
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_file(&self, path: String) -> Result<(), String> {
|
fn build_file(&self, path: &str) -> Result<(), String> {
|
||||||
let out_path = path
|
let out_path = path
|
||||||
.clone()
|
.clone()
|
||||||
.replace("docs", "dist")
|
.replace("docs", "dist")
|
||||||
.replace(".adoc", ".html");
|
.replace(".adoc", ".html");
|
||||||
|
|
||||||
println!("asciidoctor {path} --out-file={out_path}");
|
return self.builder.build(&path, &out_path);
|
||||||
let _result = process::Command::new("asciidoctor")
|
|
||||||
.arg(format!("{path}"))
|
|
||||||
.arg(format!("--out-file={out_path}"))
|
|
||||||
.output()
|
|
||||||
.expect("fuck");
|
|
||||||
|
|
||||||
dbg!(_result);
|
|
||||||
|
|
||||||
return Ok(());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,14 +56,31 @@ impl Command for Build {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let path = format!("{project_cwd}/docs/");
|
let path = format!("{project_cwd}/docs/");
|
||||||
|
let mut error_count = 0;
|
||||||
|
|
||||||
return self.build_dir(path);
|
for result in self.build_dir(path) {
|
||||||
|
match result {
|
||||||
|
Err(e) => {
|
||||||
|
error_count += 1;
|
||||||
|
println!("{e}");
|
||||||
|
},
|
||||||
|
Ok(()) => println!("success")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if error_count > 0{
|
||||||
|
return Err(format!("failed with {} errors", error_count))
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new() -> Self
|
fn new() -> Self
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
return Build {};
|
return Build {
|
||||||
|
builder: Box::new(AsciiDoctorBuilder {})
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
mod commands;
|
mod commands;
|
||||||
|
pub mod builder;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user