From caf528b6c0e12586ac315500a9c9d2c4fe7304af Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Sun, 13 Aug 2023 07:30:59 +0530 Subject: [PATCH] args: use PathBuf instead of string for file path Signed-off-by: Amneesh Singh --- flake.nix | 11 ++++++----- src/args.rs | 7 ++++--- src/main.rs | 12 +++++++++--- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/flake.nix b/flake.nix index 0cd34ad..f91e3af 100644 --- a/flake.nix +++ b/flake.nix @@ -34,10 +34,11 @@ commonArgs = { inherit src; }; - cargoArtifacts = craneLib.buildDepsOnly commonArgs; + # there are no deps right now + # cargoArtifacts = craneLib.buildDepsOnly commonArgs; tricc = craneLib.buildPackage (commonArgs // { - inherit cargoArtifacts; + # inherit cargoArtifacts; doCheck = false; }); in @@ -49,7 +50,7 @@ # not using flake checks to run them individually checks = { clippy = craneLib.cargoClippy (commonArgs // { - inherit cargoArtifacts; + # inherit cargoArtifacts; }); fmt = craneLib.cargoFmt { @@ -57,11 +58,11 @@ }; doc = craneLib.cargoDoc (commonArgs // { - inherit cargoArtifacts; + # inherit cargoArtifacts; }); nextest = craneLib.cargoNextest (commonArgs // { - inherit cargoArtifacts; + # inherit cargoArtifacts; partitions = 1; partitionType = "count"; }); diff --git a/src/args.rs b/src/args.rs index 17d042d..05a47b9 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1,4 +1,5 @@ use std::env; +use std::path::PathBuf; use std::process::exit; const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -8,7 +9,7 @@ const CRATE: &str = env!("CARGO_CRATE_NAME"); #[derive(Default)] pub struct Args { version: bool, - file: Option, + file: Option, } impl Args { @@ -34,7 +35,7 @@ impl Args { if self.file.is_some() { panic!("please specify only a single source file!"); } - self.file = Some(file.to_owned()); + self.file = Some(PathBuf::from(file)); } } } @@ -55,7 +56,7 @@ impl Args { /// Fetches the file from the arguments. /// Panics if there is no file in the arguments #[inline] - pub fn get_file(self) -> String { + pub fn get_file(self) -> PathBuf { self.file.expect("no file supplied!") } } diff --git a/src/main.rs b/src/main.rs index 10683d5..0317806 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,8 +27,14 @@ fn main() { args.handle(); let file = args.get_file(); - let content = fs::read_to_string(file).expect("Couldn't read the file"); + let content = fs::read_to_string(&file).expect("Couldn't read the file"); let mut parser = Parser::new(&content); - - println!("{:?}", parser.parse()); + let Some(parent) = parser.parse() else { + eprintln!( + "Failed to parse {} - See the errors above", + file.to_string_lossy() + ); + std::process::exit(1); + }; + println!("Parsed AST:\n{:#?}", parent); }