diff --git a/src/event_count/database.rs b/src/event_count/database.rs index c9adbcfc..3925260d 100644 --- a/src/event_count/database.rs +++ b/src/event_count/database.rs @@ -24,7 +24,7 @@ pub(crate) fn record_event(event_type: EventType, input: OrgSource<'_>) { *db.entry(key).or_insert(0) += 1; } -pub fn report(original_document: &str) { +pub(crate) fn report(original_document: &str) { let mut db = GLOBAL_DATA.lock().unwrap(); let db = db.get_or_insert_with(HashMap::new); let mut results: Vec<_> = db.iter().collect(); diff --git a/src/event_count/mod.rs b/src/event_count/mod.rs index 8987f8ba..687fef33 100644 --- a/src/event_count/mod.rs +++ b/src/event_count/mod.rs @@ -2,5 +2,5 @@ mod database; mod event_type; pub(crate) use database::record_event; -pub use database::report; +pub(crate) use database::report; pub(crate) use event_type::EventType; diff --git a/src/lib.rs b/src/lib.rs index 214e9430..c352e9bb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ #![feature(is_sorted)] #![feature(test)] #![feature(iter_intersperse)] +#![feature(exact_size_is_empty)] // TODO: #![warn(missing_docs)] #![allow(clippy::bool_assert_comparison)] // Sometimes you want the long form because its easier to see at a glance. @@ -11,6 +12,7 @@ extern crate test; #[cfg(feature = "compare")] pub mod compare; +pub mod parse_cli; #[cfg(any(feature = "compare", feature = "wasm", feature = "wasm_test"))] pub mod util; #[cfg(any(feature = "wasm", feature = "wasm_test"))] @@ -21,7 +23,7 @@ pub mod wasm_test; mod context; mod error; #[cfg(feature = "event_count")] -pub mod event_count; +mod event_count; mod iter; pub mod parser; pub mod types; diff --git a/src/main.rs b/src/main.rs index ecf67c1a..116136f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,4 @@ -#![feature(round_char_boundary)] -#![feature(exact_size_is_empty)] -use std::io::Read; -use std::path::Path; - -use ::organic::parser::parse; -use organic::parser::parse_with_settings; -use organic::settings::GlobalSettings; -use organic::settings::LocalFileAccessInterface; +use organic::parse_cli::main_body; #[cfg(feature = "tracing")] use crate::init_tracing::init_telemetry; @@ -30,55 +22,3 @@ fn main() -> Result<(), Box> { main_body_result }) } - -#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn main_body() -> Result<(), Box> { - let args = std::env::args().skip(1); - if args.is_empty() { - let org_contents = read_stdin_to_string()?; - run_anonymous_parse(org_contents) - } else { - for arg in args { - run_parse_on_file(arg)? - } - Ok(()) - } -} - -fn read_stdin_to_string() -> Result> { - let mut stdin_contents = String::new(); - std::io::stdin() - .lock() - .read_to_string(&mut stdin_contents)?; - Ok(stdin_contents) -} - -fn run_anonymous_parse>(org_contents: P) -> Result<(), Box> { - let org_contents = org_contents.as_ref(); - let rust_parsed = parse(org_contents)?; - println!("{:#?}", rust_parsed); - #[cfg(feature = "event_count")] - organic::event_count::report(org_contents); - Ok(()) -} - -fn run_parse_on_file>(org_path: P) -> Result<(), Box> { - let org_path = org_path.as_ref(); - let parent_directory = org_path - .parent() - .ok_or("Should be contained inside a directory.")?; - let org_contents = std::fs::read_to_string(org_path)?; - let org_contents = org_contents.as_str(); - let file_access_interface = LocalFileAccessInterface { - working_directory: Some(parent_directory.to_path_buf()), - }; - let global_settings = GlobalSettings { - file_access: &file_access_interface, - ..Default::default() - }; - let rust_parsed = parse_with_settings(org_contents, &global_settings)?; - println!("{:#?}", rust_parsed); - #[cfg(feature = "event_count")] - organic::event_count::report(org_contents); - Ok(()) -} diff --git a/src/parse_cli/mod.rs b/src/parse_cli/mod.rs new file mode 100644 index 00000000..c3bc9744 --- /dev/null +++ b/src/parse_cli/mod.rs @@ -0,0 +1,59 @@ +use std::io::Read; +use std::path::Path; + +use crate::parser::parse; +use crate::parser::parse_with_settings; +use crate::settings::GlobalSettings; +use crate::settings::LocalFileAccessInterface; + +#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] +pub fn main_body() -> Result<(), Box> { + let args = std::env::args().skip(1); + if args.is_empty() { + let org_contents = read_stdin_to_string()?; + run_anonymous_parse(org_contents) + } else { + for arg in args { + run_parse_on_file(arg)? + } + Ok(()) + } +} + +fn read_stdin_to_string() -> Result> { + let mut stdin_contents = String::new(); + std::io::stdin() + .lock() + .read_to_string(&mut stdin_contents)?; + Ok(stdin_contents) +} + +fn run_anonymous_parse>(org_contents: P) -> Result<(), Box> { + let org_contents = org_contents.as_ref(); + let rust_parsed = parse(org_contents)?; + println!("{:#?}", rust_parsed); + #[cfg(feature = "event_count")] + crate::event_count::report(org_contents); + Ok(()) +} + +fn run_parse_on_file>(org_path: P) -> Result<(), Box> { + let org_path = org_path.as_ref(); + let parent_directory = org_path + .parent() + .ok_or("Should be contained inside a directory.")?; + let org_contents = std::fs::read_to_string(org_path)?; + let org_contents = org_contents.as_str(); + let file_access_interface = LocalFileAccessInterface { + working_directory: Some(parent_directory.to_path_buf()), + }; + let global_settings = GlobalSettings { + file_access: &file_access_interface, + ..Default::default() + }; + let rust_parsed = parse_with_settings(org_contents, &global_settings)?; + println!("{:#?}", rust_parsed); + #[cfg(feature = "event_count")] + crate::event_count::report(org_contents); + Ok(()) +}