Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fall back to compiled-in queries for supported languages. #557

Merged
merged 3 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 19 additions & 0 deletions topiary/src/tree_sitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ impl TopiaryQuery {
}
}

#[cfg(not(target_arch = "wasm32"))]
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())),
}
}
}

impl From<Point> for Position {
fn from(point: Point) -> Self {
Self {
Expand Down