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

Fix failing to parse tool specifications in foreman.toml #36

Merged
merged 4 commits into from
Jul 13, 2024
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
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