Skip to content

Commit

Permalink
Implement reptr parse <file> CLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
werediver committed Aug 15, 2023
1 parent f1f2dcc commit 77593f2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions reptr/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod parse_file;
pub mod scan_dir;

pub use parse_file::parse_file;
pub use scan_dir::scan_dir;
16 changes: 16 additions & 0 deletions reptr/src/commands/parse_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::{io, path::PathBuf};

use crate::common::try_load;

pub fn parse_file(file_path: PathBuf) -> io::Result<()> {
let source = try_load(&file_path)?;
match dart_parser::parse(&source) {
Ok(ast) => {
println!("{ast:#?}");
}
Err(e) => {
println!("Error parsing file at path {file_path:?}\n\n{e}");
}
}
Ok(())
}
4 changes: 2 additions & 2 deletions reptr/src/commands/scan_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ pub struct Options {
pub quiet: bool,
}

pub fn scan_dir(dir: Option<std::path::PathBuf>, options: Options) -> io::Result<()> {
pub fn scan_dir(dir_path: Option<std::path::PathBuf>, options: Options) -> io::Result<()> {
let println = move |s: String| {
if !options.quiet {
println!("{s}");
}
};

let dir = dir.map_or_else(env::current_dir, Ok)?;
let dir = dir_path.map_or_else(env::current_dir, Ok)?;

let read_dir_ext = ReadDirExt::new(
dir.clone(),
Expand Down
4 changes: 2 additions & 2 deletions reptr/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use commands::scan_dir;
use commands::{parse_file, scan_dir};
use std::io;

mod commands;
Expand All @@ -18,7 +18,7 @@ fn main() -> io::Result<()> {

match cmd {
RunCmd::Scan { dir, quiet } => scan_dir(dir, scan_dir::Options { quiet })?,
RunCmd::Parse { file: _ } => todo!(),
RunCmd::Parse { file } => parse_file(file)?,
}

Ok(())
Expand Down

0 comments on commit 77593f2

Please sign in to comment.