From c04cfb838490180721489686624c09575dcb4920 Mon Sep 17 00:00:00 2001 From: Tor Hovland Date: Thu, 6 Jul 2023 11:03:28 +0200 Subject: [PATCH] Rework to fit the parallel format executing code. --- topiary-cli/src/main.rs | 70 +++++++++++++++++++++++++++----------- topiary/src/tree_sitter.rs | 23 ++++++++----- 2 files changed, 64 insertions(+), 29 deletions(-) diff --git a/topiary-cli/src/main.rs b/topiary-cli/src/main.rs index 7717f8a7..72a56495 100644 --- a/topiary-cli/src/main.rs +++ b/topiary-cli/src/main.rs @@ -136,8 +136,16 @@ async fn run() -> CLIResult<()> { )] }; + type IoFile = ( + String, + String, + Language, + Option, + CLIResult, + ); + // Add the language and query Path to the io_files - let io_files: Vec<(String, String, Language, PathBuf)> = io_files + let io_files: Vec = io_files .into_iter() // Add the appropriate language to all of the tuples .map(|(i, o)| { @@ -146,12 +154,15 @@ async fn run() -> CLIResult<()> { } else { Language::detect(&i, &configuration)?.clone() }; + let query_path = if let Some(query) = &args.query { - query.clone() + Ok(query.clone()) } else { - language.query_file()? - }; - Ok((i, o, language, query_path)) + language.query_file() + } + .map_err(TopiaryError::Lib); + + Ok((i, o, language, args.query.clone(), query_path)) }) .collect::>>()?; @@ -159,22 +170,41 @@ async fn run() -> CLIResult<()> { // _ holds the tree_sitter_facade::Language let fmt_args: Vec<(String, String, Language, _, TopiaryQuery)> = futures::future::try_join_all(io_files.into_iter().map( - |(i, o, language, query_path)| async move { - let query_content = ({ - let mut reader = BufReader::new(File::open(&query_path)?); - let mut contents = String::new(); - reader.read_to_string(&mut contents)?; - - Ok(contents) - }) - .map_err(|e| { - TopiaryError::Bin( - "Could not open query file".into(), - Some(CLIError::IOError(e)), - ) - })?; + |(i, o, language, query_arg, query_path)| async move { let grammar = language.grammar().await?; - let query = TopiaryQuery::new(&grammar, &query_content)?; + + let query = query_path + .and_then(|query_path| { + { + let mut reader = BufReader::new(File::open(query_path)?); + let mut contents = String::new(); + reader.read_to_string(&mut contents)?; + Ok(contents) + } + .map_err(|e| { + TopiaryError::Bin( + "Could not open query file".into(), + Some(CLIError::IOError(e)), + ) + }) + }) + .and_then(|query_content: String| { + Ok(TopiaryQuery::new(&grammar, &query_content)?) + }) + .or_else(|e| { + // If we weren't able to read the query file, and the user didn't + // request a specific query file, we should fall back to the built-in + // queries. + if query_arg.is_none() { + log::info!( + "No language file found for {language:?}. Will use built-in query." + ); + Ok((&language).try_into()?) + } else { + Err(e) + } + })?; + Ok::<_, TopiaryError>((i, o, language, grammar, query)) }, )) diff --git a/topiary/src/tree_sitter.rs b/topiary/src/tree_sitter.rs index 65485a63..4b201bcf 100644 --- a/topiary/src/tree_sitter.rs +++ b/topiary/src/tree_sitter.rs @@ -141,15 +141,20 @@ impl TopiaryQuery { } #[cfg(not(target_arch = "wasm32"))] -impl From for TopiaryQuery { - fn from(language: crate::SupportedLanguage) -> Self { - match language { - crate::SupportedLanguage::Json => TopiaryQuery::json(), - crate::SupportedLanguage::Nickel => TopiaryQuery::nickel(), - crate::SupportedLanguage::Ocaml | crate::SupportedLanguage::OcamlInterface => { - TopiaryQuery::ocaml() - } - crate::SupportedLanguage::Toml => TopiaryQuery::toml(), +impl TryFrom<&crate::Language> for TopiaryQuery { + type Error = FormatterError; + + fn try_from(language: &crate::Language) -> FormatterResult { + match language.name.as_str() { + "bash" => Ok(TopiaryQuery::bash()), + "json" => Ok(TopiaryQuery::json()), + "nickel" => Ok(TopiaryQuery::nickel()), + "ocaml" => Ok(TopiaryQuery::ocaml()), + "ocaml_interface" => Ok(TopiaryQuery::ocaml_interface()), + "rust" => Ok(TopiaryQuery::rust()), + "toml" => Ok(TopiaryQuery::toml()), + "tree_sitter_query" => Ok(TopiaryQuery::tree_sitter_query()), + name => Err(FormatterError::UnsupportedLanguage(name.to_string())), } } }