Skip to content

Commit

Permalink
Resolve to_language_definition todo
Browse files Browse the repository at this point in the history
  • Loading branch information
Erin van der Veen committed Jan 2, 2024
1 parent 530d916 commit 4d23922
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
30 changes: 22 additions & 8 deletions topiary-cli/src/configuration/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,11 @@ pub struct Language {
/// The indentation string used for this language; defaults to " " (i.e., two spaces). Any
/// string can be provided, but in most instances it will be some whitespace (e.g., " ",
/// "\t", etc.)
indent: Option<String>,
pub indent: Option<String>,
}

// TODO I don't think we're going to need this here...but maybe
impl Language {
pub fn indent(&self) -> &str {
match &self.indent {
Some(indent) => &indent,
None => " ",
}
}

pub fn find_query_file(&self) -> CLIResult<PathBuf> {
let basename = PathBuf::from(match self.name.as_str() {
"bash" => "bash",
Expand Down Expand Up @@ -78,6 +71,27 @@ impl Language {
)
})
}

pub fn grammar(&self) -> CLIResult<tree_sitter_facade::Language> {
Ok(match self.name.as_str() {
"bash" => tree_sitter_bash::language(),
"json" => tree_sitter_json::language(),
"nickel" => tree_sitter_nickel::language(),
"ocaml" => tree_sitter_ocaml::language_ocaml(),
"ocaml_interface" => tree_sitter_ocaml::language_ocaml_interface(),
"ocamllex" => tree_sitter_ocamllex::language(),
"rust" => tree_sitter_rust::language(),
"toml" => tree_sitter_toml::language(),
"tree_sitter_query" => tree_sitter_query::language(),
name => {
return Err(TopiaryError::Bin(
format!("Could not find grammar for language {name}"),
Some(CLIError::UnsupportedLanguage(name.to_string())),
))
}
}
.into())
}
}

/// The configuration of the Topiary CLI.
Expand Down
30 changes: 17 additions & 13 deletions topiary-cli/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,21 @@ pub struct InputFile<'cfg> {
impl<'cfg> InputFile<'cfg> {
/// Convert our `InputFile` into language definition values that Topiary can consume
pub async fn to_language_definition(&self) -> CLIResult<LanguageDefinition> {
todo!()
// let contents = match &self.query {
// QuerySource::Path(query) => tokio::fs::read_to_string(query).await?,
// QuerySource::BuiltIn(contents) => contents.to_owned(),
// };
// let query = TopiaryQuery::new(&self.language.grammar, &contents)?;

// Ok(LanguageDefinition {
// query,
// language: self.language,
// })
let grammar = self.language().grammar()?;
let contents = match &self.query {
QuerySource::Path(query) => tokio::fs::read_to_string(query).await?,
QuerySource::BuiltIn(contents) => contents.to_owned(),
};
let query = TopiaryQuery::new(&grammar, &contents)?;

Ok(LanguageDefinition {
language: Language {
name: self.language.name.clone(),
query: query,
grammar: grammar,
indent: self.language().indent.clone(),
},
})
}

/// Expose input source
Expand Down Expand Up @@ -185,7 +189,7 @@ impl<'cfg, 'i> Inputs<'cfg> {
let inputs = match inputs.into() {
InputFrom::Stdin(language_name, query) => {
vec![(|| {
let language = config.get_language(language_name)?;
let language = config.get_language(&language_name)?;
let query_source: QuerySource = match query {
// The user specified a query file
Some(p) => p,
Expand All @@ -198,7 +202,7 @@ impl<'cfg, 'i> Inputs<'cfg> {
// fail to find anything, because the builtin error might be unexpected.
Err(e) => {
log::warn!("No query files found in any of the expected locations. Falling back to compile-time included files.");
to_query(language_name).map_err(|_| e)?
to_query(&language_name).map_err(|_| e)?
}
},
};
Expand Down
1 change: 0 additions & 1 deletion topiary-cli/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use crate::{error::CLIResult, io::InputFile};
/// `LanguageDefinition` contains the necessary language-related values that the Topiary API
/// expects to do its job
pub struct LanguageDefinition {
pub query: TopiaryQuery,
pub language: Language,
}

Expand Down

0 comments on commit 4d23922

Please sign in to comment.