cyberstorm/src/main.rs

73 lines
1.6 KiB
Rust

use std::path::PathBuf;
use std::str::FromStr;
use anyhow::{Context, Error};
use structopt::StructOpt;
use cyberstorm::generator::mdbook::MDBookEngine;
use cyberstorm::generator::Generator;
use cyberstorm::registry::{DirectoryRegistry, SupportedRegistry};
#[derive(Debug, StructOpt)]
#[structopt(name = "cyberstorm")]
struct Opt {
/// Registry to use.
registry: RegistryOpt,
#[structopt(subcommand)]
cmd: Cmd,
}
#[derive(Debug, StructOpt)]
enum Cmd {
BuildMdbook(BuildMdbookOpt),
}
#[derive(Debug, StructOpt)]
struct BuildMdbookOpt {
/// Path to the output MDBook directory.
#[structopt(parse(from_os_str))]
book: PathBuf,
}
#[derive(Debug)]
enum RegistryOpt {
Directory(PathBuf),
}
impl FromStr for RegistryOpt {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::Directory(PathBuf::from(s)))
}
}
async fn run(opt: Opt) -> Result<(), Error> {
let registry = match &opt.registry {
RegistryOpt::Directory(path) => SupportedRegistry::Directory(DirectoryRegistry::new(path)),
};
match &opt.cmd {
Cmd::BuildMdbook(build_opt) => build_mdbook(&opt, build_opt, &registry).await,
}
}
async fn build_mdbook(
_opt: &Opt,
build_opt: &BuildMdbookOpt,
registry: &SupportedRegistry,
) -> Result<(), Error> {
let engine = MDBookEngine::new(&build_opt.book).context("failed to load mdbook engine")?;
Generator::new(engine, registry)
.generate()
.await
.context("failed to generate mdbook content")
}
#[tokio::main]
async fn main() {
if let Err(err) = run(Opt::from_args()).await {
eprintln!("{:#}", err);
}
}