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.
This commit is contained in:
parent
f57d60dab0
commit
e4407cbdd1
@ -24,7 +24,7 @@ pub(crate) fn record_event(event_type: EventType, input: OrgSource<'_>) {
|
|||||||
*db.entry(key).or_insert(0) += 1;
|
*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 mut db = GLOBAL_DATA.lock().unwrap();
|
||||||
let db = db.get_or_insert_with(HashMap::new);
|
let db = db.get_or_insert_with(HashMap::new);
|
||||||
let mut results: Vec<_> = db.iter().collect();
|
let mut results: Vec<_> = db.iter().collect();
|
||||||
|
@ -2,5 +2,5 @@ mod database;
|
|||||||
mod event_type;
|
mod event_type;
|
||||||
|
|
||||||
pub(crate) use database::record_event;
|
pub(crate) use database::record_event;
|
||||||
pub use database::report;
|
pub(crate) use database::report;
|
||||||
pub(crate) use event_type::EventType;
|
pub(crate) use event_type::EventType;
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#![feature(is_sorted)]
|
#![feature(is_sorted)]
|
||||||
#![feature(test)]
|
#![feature(test)]
|
||||||
#![feature(iter_intersperse)]
|
#![feature(iter_intersperse)]
|
||||||
|
#![feature(exact_size_is_empty)]
|
||||||
// TODO: #![warn(missing_docs)]
|
// TODO: #![warn(missing_docs)]
|
||||||
#![allow(clippy::bool_assert_comparison)] // Sometimes you want the long form because its easier to see at a glance.
|
#![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")]
|
#[cfg(feature = "compare")]
|
||||||
pub mod compare;
|
pub mod compare;
|
||||||
|
pub mod parse_cli;
|
||||||
#[cfg(any(feature = "compare", feature = "wasm", feature = "wasm_test"))]
|
#[cfg(any(feature = "compare", feature = "wasm", feature = "wasm_test"))]
|
||||||
pub mod util;
|
pub mod util;
|
||||||
#[cfg(any(feature = "wasm", feature = "wasm_test"))]
|
#[cfg(any(feature = "wasm", feature = "wasm_test"))]
|
||||||
@ -21,7 +23,7 @@ pub mod wasm_test;
|
|||||||
mod context;
|
mod context;
|
||||||
mod error;
|
mod error;
|
||||||
#[cfg(feature = "event_count")]
|
#[cfg(feature = "event_count")]
|
||||||
pub mod event_count;
|
mod event_count;
|
||||||
mod iter;
|
mod iter;
|
||||||
pub mod parser;
|
pub mod parser;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
62
src/main.rs
62
src/main.rs
@ -1,12 +1,4 @@
|
|||||||
#![feature(round_char_boundary)]
|
use organic::parse_cli::main_body;
|
||||||
#![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;
|
|
||||||
|
|
||||||
#[cfg(feature = "tracing")]
|
#[cfg(feature = "tracing")]
|
||||||
use crate::init_tracing::init_telemetry;
|
use crate::init_tracing::init_telemetry;
|
||||||
@ -30,55 +22,3 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
main_body_result
|
main_body_result
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
||||||
fn main_body() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
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<String, Box<dyn std::error::Error>> {
|
|
||||||
let mut stdin_contents = String::new();
|
|
||||||
std::io::stdin()
|
|
||||||
.lock()
|
|
||||||
.read_to_string(&mut stdin_contents)?;
|
|
||||||
Ok(stdin_contents)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run_anonymous_parse<P: AsRef<str>>(org_contents: P) -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
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<P: AsRef<Path>>(org_path: P) -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
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(())
|
|
||||||
}
|
|
||||||
|
59
src/parse_cli/mod.rs
Normal file
59
src/parse_cli/mod.rs
Normal file
@ -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<dyn std::error::Error>> {
|
||||||
|
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<String, Box<dyn std::error::Error>> {
|
||||||
|
let mut stdin_contents = String::new();
|
||||||
|
std::io::stdin()
|
||||||
|
.lock()
|
||||||
|
.read_to_string(&mut stdin_contents)?;
|
||||||
|
Ok(stdin_contents)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_anonymous_parse<P: AsRef<str>>(org_contents: P) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
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<P: AsRef<Path>>(org_path: P) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
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(())
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user