Skip to content

Commit

Permalink
Rework to fit the parallel format executing code.
Browse files Browse the repository at this point in the history
  • Loading branch information
torhovland committed Jul 6, 2023
1 parent c29f935 commit c04cfb8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 29 deletions.
70 changes: 50 additions & 20 deletions topiary-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,16 @@ async fn run() -> CLIResult<()> {
)]
};

type IoFile = (
String,
String,
Language,
Option<PathBuf>,
CLIResult<PathBuf>,
);

// Add the language and query Path to the io_files
let io_files: Vec<(String, String, Language, PathBuf)> = io_files
let io_files: Vec<IoFile> = io_files
.into_iter()
// Add the appropriate language to all of the tuples
.map(|(i, o)| {
Expand All @@ -146,35 +154,57 @@ 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::<CLIResult<Vec<_>>>()?;

// Converts the simple types into arguments we can pass to the `formatter` function
// _ 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))
},
))
Expand Down
23 changes: 14 additions & 9 deletions topiary/src/tree_sitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,20 @@ impl TopiaryQuery {
}

#[cfg(not(target_arch = "wasm32"))]
impl From<crate::SupportedLanguage> 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<Self> {
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())),
}
}
}
Expand Down

0 comments on commit c04cfb8

Please sign in to comment.