From e4407cbdd1387df3f0b98abd00cb28bb8627be5e Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 3 Jan 2024 20:06:53 -0500 Subject: [PATCH] Hide the event_count module. By placing the code for the parse executable inside a module inside the organic library, we only need to expose the entrypoint publicly rather than all functions it calls. This hides the event_count module, but I will be expanding the practice to the rest of the code base shortly. This is important for not inadvertently promising stability w.r.t. semver for essentially internal functions for development tools. It was the parse binary, not compare. --- src/event_count/database.rs | 2 +- src/event_count/mod.rs | 2 +- src/lib.rs | 4 ++- src/main.rs | 62 +------------------------------------ src/parse_cli/mod.rs | 59 +++++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 64 deletions(-) create mode 100644 src/parse_cli/mod.rs diff --git a/src/event_count/database.rs b/src/event_count/database.rs index c9adbcf..3925260 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 8987f8b..687fef3 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 214e943..c352e9b 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 ecf67c1..116136f 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 0000000..c3bc974 --- /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(()) +}