From 0c79c6270d244fc4a3da2c789e0429e07033641c Mon Sep 17 00:00:00 2001 From: raffitz Date: Mon, 22 Mar 2021 18:33:31 +0000 Subject: [PATCH] Add file stating --- Cargo.lock | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/main.rs | 28 ++++++++++++++ 3 files changed, 135 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 87c18f7..42ce965 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,6 +20,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "beef" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6736e2428df2ca2848d846c43e88745121a6654696e349ce0054a420815a7409" + [[package]] name = "bitflags" version = "1.2.1" @@ -41,6 +47,12 @@ dependencies = [ "vec_map", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "hermit-abi" version = "0.1.17" @@ -56,12 +68,91 @@ version = "0.2.80" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" +[[package]] +name = "logos" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "427e2abca5be13136da9afdbf874e6b34ad9001dd70f2b103b083a85daa7b345" +dependencies = [ + "logos-derive", +] + +[[package]] +name = "logos-derive" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56a7d287fd2ac3f75b11f19a1c8a874a7d55744bd91f7a1b3e7cf87d4343c36d" +dependencies = [ + "beef", + "fnv", + "proc-macro2", + "quote", + "regex-syntax", + "syn", + "utf8-ranges", +] + +[[package]] +name = "pomelo" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76dd249f63dbbc68c8caa9b5fd16b32c7ba5da56df93d40a9552c91ebfd2a1cd" +dependencies = [ + "pomelo-impl", +] + +[[package]] +name = "pomelo-impl" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597c3287a549da151aca6ada2795ecde089c7527bd5093114e8e0e1c3f0e52b1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex-syntax" +version = "0.6.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5f089152e60f62d28b835fbff2cd2e8dc0baf1ac13343bef92ab7eed84548" + [[package]] name = "strsim" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +[[package]] +name = "syn" +version = "1.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fd9d1e9976102a03c542daa2eff1b43f9d72306342f3f8b3ed5fb8908195d6f" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -77,6 +168,18 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + +[[package]] +name = "utf8-ranges" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ae116fef2b7fea257ed6440d3cfcff7f190865f170cdad00bb6465bf18ecba" + [[package]] name = "vec_map" version = "0.8.2" @@ -88,6 +191,8 @@ name = "voxelmap" version = "0.1.0" dependencies = [ "clap", + "logos", + "pomelo", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 4e42093..a58446e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,5 @@ description = "Converts mathematical descriptions of objects to voxel maps" [dependencies] clap = "2.33" +logos = "0.12" +pomelo = "0.1.5" diff --git a/src/main.rs b/src/main.rs index e58dcd8..64486de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ use clap::{crate_authors, crate_description, crate_name, crate_version, App, Arg}; +use std::fs; +use std::io::{Error, ErrorKind}; macro_rules! scale_message { ($n:ident) => { @@ -6,6 +8,14 @@ macro_rules! scale_message { }; } +fn io_error(err: Error, path: &str) -> String { + match err.kind() { + ErrorKind::NotFound => format!("{} not found", path), + ErrorKind::PermissionDenied => format!("Permission to read {} denied", path), + _ => format!("Unexpected error accessing {}", path), + } +} + fn main() { let matches = App::new(crate_name!()) .version(crate_version!()) @@ -32,6 +42,24 @@ fn main() { } }), ) + .arg( + Arg::with_name("FILE") + .help("The file describing the shape to map") + .required(true) + .index(1) + .validator(|path: String| -> Result<(), String> { + match fs::metadata(&path) { + Ok(stat) => { + if stat.is_dir() { + Err(format!("{} is a directory, not a file", path)) + } else { + Ok(()) + } + } + Err(error) => Err(io_error(error, &path)), + } + }), + ) .get_matches(); let scale = matches.value_of("scale").map(|s| s.parse::().unwrap());