From e5e5120a1057b818485eeb3a07b344f07e218908 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 16 Aug 2023 16:05:16 -0400 Subject: [PATCH] Move telemetry handling to the tracing-specific main function. This is so main_body can exit with an error at any time without missing the shutdown_telemetry function. This does not catch panics. --- src/main.rs | 35 +++++++++++++++-------------------- src/parser/plain_list.rs | 13 +------------ 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9228d32..7c6323c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,11 +9,12 @@ use organic::compare_document; use organic::emacs_parse_org_document; #[cfg(feature = "compare")] use organic::parser::sexp::sexp_with_padding; -#[cfg(feature = "tracing")] -use tracing::span; +#[cfg(feature = "tracing")] use crate::init_tracing::init_telemetry; +#[cfg(feature = "tracing")] use crate::init_tracing::shutdown_telemetry; +#[cfg(feature = "tracing")] mod init_tracing; #[cfg(not(feature = "tracing"))] @@ -24,28 +25,22 @@ fn main() -> Result<(), Box> { #[cfg(feature = "tracing")] fn main() -> Result<(), Box> { let rt = tokio::runtime::Runtime::new()?; - let result = rt.block_on(async { main_body() }); + let result = rt.block_on(async { + init_telemetry()?; + let main_body_result = main_body(); + shutdown_telemetry()?; + main_body_result + }); result } +#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn main_body() -> Result<(), Box> { - init_telemetry()?; - let compare_result = { - #[cfg(feature = "tracing")] - let span = span!(tracing::Level::DEBUG, "run_compare"); - #[cfg(feature = "tracing")] - let _enter = span.enter(); - - run_compare( - std::env::args() - .nth(1) - .expect("Pass a single file into this script."), - ) - }; - - shutdown_telemetry()?; - compare_result?; - Ok(()) + run_compare( + std::env::args() + .nth(1) + .expect("Pass a single file into this script."), + ) } #[cfg(feature = "compare")] diff --git a/src/parser/plain_list.rs b/src/parser/plain_list.rs index c19fc01..685701c 100644 --- a/src/parser/plain_list.rs +++ b/src/parser/plain_list.rs @@ -241,18 +241,7 @@ fn plain_list_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn plain_list_item_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { - let current_item_indent_level: &usize = - get_context_item_indent(context).ok_or(nom::Err::Error(CustomError::MyError(MyError( - "Not inside a plain list item", - ))))?; - let plain_list_item_matcher = parser_with_context!(plain_list_item)(context); - let line_indented_lte_matcher = parser_with_context!(line_indented_lte)(context); - alt(( - recognize(verify(plain_list_item_matcher, |pli| { - pli.indentation <= *current_item_indent_level - })), - recognize(line_indented_lte_matcher), - ))(input) + recognize(parser_with_context!(line_indented_lte)(context))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]