From 83b3dec52641c7319a7939532f51efe4bef4e9f0 Mon Sep 17 00:00:00 2001 From: lmittmann <3458786+lmittmann@users.noreply.github.com> Date: Fri, 18 Oct 2024 11:36:30 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Move=20compile=20to=20`bui?= =?UTF-8?q?ld`=20sub-command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: lmittmann --- crates/cli/src/main.rs | 70 ++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index b01ca56..2f40646 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -5,36 +5,16 @@ use std::{fs::read_to_string, io, process::exit}; use thiserror::Error; fn main() { - let cli = argh::from_env(); - if let Err(e) = run(cli) { + let cli: Cli = argh::from_env(); + let res = match cli.command { + Commands::Build(cmd) => build(cmd), + }; + if let Err(e) = res { eprintln!("error: {}", e); exit(1); } } -fn run(cli: Cli) -> HuffResult { - let src = read_to_string(&cli.filename)?; - let filename: String = cli.filename; - match parse(&src) { - Ok(ast) => println!("{:?}", ast), - Err(errs) => errs.into_iter().for_each(|e| { - Report::build(ReportKind::Error, filename.clone(), e.span().start) - .with_config(Config::default().with_index_type(IndexType::Byte)) - .with_message(e.reason()) - .with_label( - Label::new((filename.clone(), e.span().into_range())) - .with_message(e.reason()) - .with_color(Color::Red), - ) - .finish() - .print(sources([(filename.clone(), &src)])) - .unwrap() - }), - } - - Ok(()) -} - #[derive(Error, Debug)] enum HuffError { /// Wrapper around `io::Error` @@ -44,12 +24,50 @@ enum HuffError { // Parser(Report), // Parser(#[from] ParseError, huff_ast::Error>), } - type HuffResult = Result<(), HuffError>; +fn build(cmd: BuildCommand) -> HuffResult { + let src = read_to_string(&cmd.filename)?; + let filename: String = cmd.filename; + match parse(&src) { + Ok(ast) => println!("{:?}", ast), + Err(errs) => { + errs.into_iter().for_each(|e| { + Report::build(ReportKind::Error, filename.clone(), e.span().start) + .with_config(Config::default().with_index_type(IndexType::Byte)) + // .with_message(e.reason()) + .with_label( + Label::new((filename.clone(), e.span().into_range())) + .with_message(e.reason()) + .with_color(Color::Red), + ) + .finish() + .print(sources([(filename.clone(), &src)])) + .unwrap() + }); + } + } + + Ok(()) +} + #[derive(FromArgs)] /// Huff Language Compiler struct Cli { + #[argh(subcommand)] + command: Commands, +} + +#[derive(FromArgs)] +#[argh(subcommand)] +enum Commands { + Build(BuildCommand), +} + +#[derive(FromArgs)] +#[argh(subcommand, name = "build")] +/// Build compiles a huff file. +struct BuildCommand { /// filename #[argh(positional)] filename: String,