Skip to content

Commit

Permalink
♻️ Move compile to build sub-command
Browse files Browse the repository at this point in the history
Co-authored-by: lmittmann <[email protected]>
  • Loading branch information
lmittmann and lmittmann authored Oct 18, 2024
1 parent b2d11e9 commit 83b3dec
Showing 1 changed file with 44 additions and 26 deletions.
70 changes: 44 additions & 26 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -44,12 +24,50 @@ enum HuffError {
// Parser(Report),
// Parser(#[from] ParseError<usize, Token<'src>, 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,
Expand Down

0 comments on commit 83b3dec

Please sign in to comment.