Skip to content

Commit

Permalink
Fix failing to parse tool specifications in foreman.toml (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dekkonot authored Jul 13, 2024
2 parents 0d3dea8 + eeadc28 commit 32437d3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed

- Failed to parse tool specifications in foreman.toml if they were an inline table ([#36])

[#36]: https://github.com/rojo-rbx/rokit/pull/36

## `0.1.4` - July 11th, 2024

### Fixed
Expand Down
28 changes: 25 additions & 3 deletions lib/discovery/foreman.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use std::{collections::HashMap, str::FromStr};

use semver::Version;
use toml_edit::DocumentMut;
use toml_edit::{DocumentMut, InlineTable, Table};

use crate::tool::{ToolAlias, ToolId, ToolSpec};

use super::Manifest;

enum SpecType {
InlineTable(InlineTable),
Table(Table),
}

#[derive(Debug, Clone)]
pub(crate) struct ForemanManifest {
document: DocumentMut,
Expand Down Expand Up @@ -35,7 +40,19 @@ impl Manifest for ForemanManifest {
if let Some(map) = self.document.get("tools").and_then(|t| t.as_table()) {
for (alias, tool_def) in map {
let tool_alias = alias.parse::<ToolAlias>().ok();
let tool_spec = tool_def.as_table().and_then(parse_foreman_tool_definition);

let tool_spec = if tool_def.is_inline_table() {
tool_def
.as_inline_table()
.cloned()
.and_then(|map| parse_foreman_tool_definition(SpecType::InlineTable(map)))
} else {
tool_def
.as_table()
.cloned()
.and_then(|map| parse_foreman_tool_definition(SpecType::Table(map)))
};

if let (Some(alias), Some(spec)) = (tool_alias, tool_spec) {
tools.insert(alias, spec);
}
Expand All @@ -45,7 +62,12 @@ impl Manifest for ForemanManifest {
}
}

fn parse_foreman_tool_definition(map: &toml_edit::Table) -> Option<ToolSpec> {
fn parse_foreman_tool_definition(map: SpecType) -> Option<ToolSpec> {
let map = match map {
SpecType::InlineTable(table) => table,
SpecType::Table(table) => table.into_inline_table(),
};

let version = map.get("version").and_then(|t| t.as_str()).and_then(|v| {
// TODO: Support real version requirements instead of just exact/min versions
let without_prefix = v.trim_start_matches('=').trim_start_matches('^');
Expand Down

0 comments on commit 32437d3

Please sign in to comment.