Skip to content

Commit

Permalink
Remove LanguageDefinition
Browse files Browse the repository at this point in the history
At this point, the LanguageDefinition contained only a single value, and was thus mostly useless
  • Loading branch information
Erin van der Veen committed Jan 4, 2024
1 parent d81b80b commit 3aa683f
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 26 deletions.
15 changes: 6 additions & 9 deletions topiary-cli/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::{
cli::{AtLeastOneInput, ExactlyOneInput, FromStdin},
configuration::{self, Configuration},
error::{CLIError, CLIResult, TopiaryError},
language::LanguageDefinition,
};

#[derive(Debug, Clone, Hash)]
Expand Down Expand Up @@ -118,21 +117,19 @@ 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> {
pub async fn to_language(&self) -> CLIResult<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,
grammar,
indent: self.language().indent.clone(),
},
Ok(Language {
name: self.language.name.clone(),
query,
grammar,
indent: self.language().indent.clone(),
})
}

Expand Down
12 changes: 3 additions & 9 deletions topiary-cli/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,16 @@ use topiary::Language;

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 language: Language,
}

/// Thread-safe language definition cache
pub struct LanguageDefinitionCache(Mutex<HashMap<u64, Arc<LanguageDefinition>>>);
pub struct LanguageDefinitionCache(Mutex<HashMap<u64, Arc<Language>>>);

impl LanguageDefinitionCache {
pub fn new() -> Self {
LanguageDefinitionCache(Mutex::new(HashMap::new()))
}

/// Fetch the language definition from the cache, populating if necessary, with thread-safety
pub async fn fetch<'i>(&self, input: &'i InputFile<'i>) -> CLIResult<Arc<LanguageDefinition>> {
pub async fn fetch<'i>(&self, input: &'i InputFile<'i>) -> CLIResult<Arc<Language>> {
// There's no need to store the input's identifying information (language name and query)
// in the key, so we use its hash directly. This side-steps any awkward lifetime issues.
let key = {
Expand Down Expand Up @@ -66,7 +60,7 @@ impl LanguageDefinitionCache {
input.query()
);

let lang_def = Arc::new(input.to_language_definition().await?);
let lang_def = Arc::new(input.to_language().await?);
slot.insert(lang_def).to_owned()
}
})
Expand Down
8 changes: 4 additions & 4 deletions topiary-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async fn run() -> CLIResult<()> {
scope.spawn(async {
let result: CLIResult<()> = match input {
Ok(input) => {
let lang_def = cache.fetch(&input).await?;
let language = cache.fetch(&input).await?;
let output = OutputFile::try_from(&input)?;

log::info!(
Expand All @@ -72,7 +72,7 @@ async fn run() -> CLIResult<()> {
formatter(
&mut buf_input,
&mut buf_output,
&lang_def.language,
&language,
Operation::Format {
skip_idempotence,
tolerate_parsing_errors,
Expand Down Expand Up @@ -122,7 +122,7 @@ async fn run() -> CLIResult<()> {

// We don't need a `LanguageDefinitionCache` when there's only one input,
// which saves us the thread-safety overhead
let lang_def = input.to_language_definition().await?;
let language = input.to_language().await?;

log::info!(
"Visualising {}, as {}, to {}",
Expand All @@ -137,7 +137,7 @@ async fn run() -> CLIResult<()> {
formatter(
&mut buf_input,
&mut buf_output,
&lang_def.language,
&language,
Operation::Visualise {
output_format: format.into(),
},
Expand Down
2 changes: 0 additions & 2 deletions topiary/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use std::fs;
use std::io;
use topiary::{formatter, Language, Operation, TopiaryQuery};

// FIXME Configuration is no longer part of the library

async fn format() {
let input = fs::read_to_string("../topiary-cli/tests/samples/input/ocaml.ml").unwrap();
let query_content = fs::read_to_string("../queries/ocaml.scm").unwrap();
Expand Down
2 changes: 0 additions & 2 deletions topiary/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use std::fmt;

use crate::TopiaryQuery;

// FIXME

/// A Language contains all the information Topiary requires to format that
/// specific languages.
#[derive(Debug)]
Expand Down

0 comments on commit 3aa683f

Please sign in to comment.