From f2ce971dc9389c2c964605a3f46a728108acf724 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 23 Dec 2024 11:27:31 +0000 Subject: Extract TomlTableWithSource.parse() --- .../org/shedlang/compiler/config/ProjectConfig.java | 17 +++-------------- .../compiler/config/toml/TomlTableWithSource.java | 13 +++++++++++++ 2 files changed, 16 insertions(+), 14 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/config/ProjectConfig.java b/src/main/java/org/shedlang/compiler/config/ProjectConfig.java index 12a6b3f6..817ae31f 100644 --- a/src/main/java/org/shedlang/compiler/config/ProjectConfig.java +++ b/src/main/java/org/shedlang/compiler/config/ProjectConfig.java @@ -23,27 +23,16 @@ public record ProjectConfig( var configPath = projectPath.resolve("shed.toml"); var contents = Files.readString(configPath); var source = Sources.file(configPath, contents); - var tomlParseResult = Toml.parse(configPath); - return fromTomlParseResult(projectPath, tomlParseResult, source); + return fromToml(projectPath, TomlTableWithSource.parse(source)); } public static ProjectConfig fromString(Path projectPath, String config) { - return fromTomlParseResult( + return fromToml( projectPath, - Toml.parse(config), - Sources.string(config) + TomlTableWithSource.parse(Sources.string(config)) ); } - private static ProjectConfig fromTomlParseResult(Path projectPath, TomlParseResult result, Source source) { - if (result.hasErrors()) { - var error = result.errors().get(0); - throw TomlSyntaxConfigError.fromTomljError(error, source); - } - - return fromToml(projectPath, new TomlTableWithSource(result, TomlPath.TOP, source)); - } - public static ProjectConfig fromToml(Path root, TomlTableWithSource table) { return new ProjectConfig( root, diff --git a/src/main/java/org/shedlang/compiler/config/toml/TomlTableWithSource.java b/src/main/java/org/shedlang/compiler/config/toml/TomlTableWithSource.java index f4c96647..c9d102c4 100644 --- a/src/main/java/org/shedlang/compiler/config/toml/TomlTableWithSource.java +++ b/src/main/java/org/shedlang/compiler/config/toml/TomlTableWithSource.java @@ -1,7 +1,9 @@ package org.shedlang.compiler.config.toml; +import org.shedlang.compiler.config.errors.TomlSyntaxConfigError; import org.shedlang.compiler.config.errors.UnexpectedTypeConfigError; import org.shedlang.compiler.sources.Source; +import org.tomlj.Toml; import org.tomlj.TomlArray; import org.tomlj.TomlPosition; import org.tomlj.TomlTable; @@ -15,6 +17,17 @@ public record TomlTableWithSource( TomlPath path, Source fullSource ) { + public static TomlTableWithSource parse(Source source) { + var result = Toml.parse(source.contents()); + + if (result.hasErrors()) { + var error = result.errors().get(0); + throw TomlSyntaxConfigError.fromTomljError(error, source); + } + + return new TomlTableWithSource(result, TomlPath.TOP, source); + } + public Optional getTable(String key) { var value = table.get(key); var position = table.inputPositionOf(key); -- cgit v1.2.3