Skip to content

Commit

Permalink
Added convenience aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Xophmeister committed Feb 24, 2023
1 parent 6884e50 commit 47f53af
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/bin/topiary/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn run() -> CLIResult<()> {
let language: Option<Language> = if let Some(language) = args.language {
Some(language.into())
} else if let Some(filename) = args.input_file.as_deref() {
Some(PathBuf::from(filename).try_into()?)
Some(Language::detect(filename)?)
} else {
// At this point, Clap ensures that args.query must be present.
// We will read the language from the query file later.
Expand All @@ -86,7 +86,7 @@ fn run() -> CLIResult<()> {
query
} else if let Some(language) = language {
// Deduce the query file from the language, if the argument is missing
language.try_into()?
language.query_file()?
} else {
// Clap ensures we won't get here
unreachable!();
Expand Down
2 changes: 1 addition & 1 deletion src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl FromStr for Configuration {
log::warn!("The #language! pragma has already been set");
}

language = Some(value.try_into()?);
language = Some(Language::new(value)?);
} else {
return Err(FormatterError::Query(
"The #language! pragma must have a parameter".into(),
Expand Down
24 changes: 23 additions & 1 deletion src/language.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::io;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use crate::{FormatterError, FormatterResult, IoError};

Expand Down Expand Up @@ -48,6 +48,28 @@ const EXTENSIONS: &[(Language, &[&str])] = &[
(Language::TreeSitterQuery, &["scm"]),
];

impl Language {
/// Convenience alias to create a Language from "magic strings".
pub fn new(s: &str) -> FormatterResult<Self> {
s.try_into()
}

/// Convenience alias to detect the Language from a Path-like value's extension.
pub fn detect<P: AsRef<Path>>(path: P) -> FormatterResult<Self> {
path.as_ref().to_path_buf().try_into()
}

/// Convenience alias to return the query file path for the Language.
pub fn query_file(&self) -> FormatterResult<PathBuf> {
self.to_owned().try_into()
}

/// Convenience alias to return the Tree-sitter grammars for the Language.
pub fn grammars(&self) -> Vec<tree_sitter::Language> {
self.to_owned().into()
}
}

/// Convert a string into a Language, if possible.
impl TryFrom<&str> for Language {
type Error = FormatterError;
Expand Down
2 changes: 1 addition & 1 deletion src/tree_sitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn apply_query(
query_content: &str,
language: Language,
) -> FormatterResult<AtomCollection> {
let (tree, grammar) = parse(input_content, language.into())?;
let (tree, grammar) = parse(input_content, language.grammars())?;
let root = tree.root_node();
let source = input_content.as_bytes();
let query = Query::new(grammar, query_content)
Expand Down
14 changes: 7 additions & 7 deletions tests/sample-tester.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use pretty_assertions::assert_eq;
use std::fs;
use std::io::BufReader;
use std::path::{Path, PathBuf};
use std::path::Path;

use pretty_assertions::assert_eq;
use test_log::test;

use topiary::{formatter, Language};

#[test]
Expand All @@ -12,16 +14,14 @@ fn input_output_tester() {

for file in input_dir {
let file = file.unwrap();

let input_path = file.path();
let language: Language = input_path.try_into().unwrap();
let language = Language::detect(file.path()).unwrap();

let expected_path = expected_dir.join(file.file_name());
let expected = fs::read_to_string(expected_path).unwrap();

let mut input = BufReader::new(fs::File::open(file.path()).unwrap());
let mut output = Vec::new();
let query = fs::read_to_string(PathBuf::try_from(language).unwrap()).unwrap();
let query = fs::read_to_string(language.query_file().unwrap()).unwrap();
let mut query = query.as_bytes();

formatter(&mut input, &mut output, &mut query, Some(language), true).unwrap();
Expand All @@ -45,7 +45,7 @@ fn formatted_query_tester() {

let mut input = BufReader::new(fs::File::open(file.path()).unwrap());
let mut output = Vec::new();
let query = fs::read_to_string(PathBuf::try_from(language).unwrap()).unwrap();
let query = fs::read_to_string(language.query_file().unwrap()).unwrap();
let mut query = query.as_bytes();

formatter(&mut input, &mut output, &mut query, Some(language), true).unwrap();
Expand Down

0 comments on commit 47f53af

Please # to comment.