Skip to content

Commit 54dc950

Browse files
committed
v1.1.4
New Features
1 parent 05c465d commit 54dc950

18 files changed

+648
-345
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
}
44

55
group = 'dev.manere.inscript'
6-
version = '1.1.3'
6+
version = '1.1.4'
77

88
repositories {
99
mavenCentral()

src/main/java/dev/manere/inscript/ConfigSection.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ public interface ConfigSection {
1616
@NotNull
1717
@Unmodifiable
1818
default Set<ConfigNode> getChildren() {
19-
return Set.copyOf(getSection().getChildren());
19+
return new LinkedHashSet<>(getSection().getChildren());
20+
}
21+
22+
@NotNull
23+
@CanIgnoreReturnValue
24+
default ConfigSection copy(final @NotNull ConfigSection other) {
25+
getSection().getChildren().addAll(other.getChildren());
26+
return this;
2027
}
2128

2229
@NotNull
2330
@Unmodifiable
2431
default Set<String> getKeys() {
25-
final Set<String> keys = new HashSet<>();
32+
final Set<String> keys = new LinkedHashSet<>();
2633

2734
for (final ConfigNode node : getChildren()) {
2835
keys.add(node.getKey());
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package dev.manere.inscript;
2+
3+
import dev.manere.inscript.format.Line;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
import java.nio.file.Path;
7+
import java.util.List;
8+
9+
public class ErrorContext {
10+
private final Inscript inscript;
11+
private final int position;
12+
private final String line;
13+
private final String error;
14+
15+
public ErrorContext(final @NotNull Inscript inscript, final int position, final @NotNull String line, final @NotNull String error) {
16+
this.inscript = inscript;
17+
this.position = position;
18+
this.line = line;
19+
this.error = error;
20+
}
21+
22+
@NotNull
23+
public static ErrorContext create(final @NotNull Line line, final @NotNull Inscript inscript, final @NotNull String error) {
24+
return new ErrorContext(inscript, line.getPosition() + 1, line.getText(), error);
25+
}
26+
27+
public int getPosition() {
28+
return position;
29+
}
30+
31+
@NotNull
32+
public String getLine() {
33+
return line;
34+
}
35+
36+
@NotNull
37+
public String getError() {
38+
return error;
39+
}
40+
41+
@NotNull
42+
public String buildDefault() {
43+
if (inscript.getPath().isPresent()) {
44+
final Path path = inscript.getPath().get();
45+
final String name = path.getFileName().toString();
46+
47+
return String.join("\n", List.of(
48+
"Failed to parse " + name + ":" + position,
49+
" " + error + ": " + line
50+
));
51+
} else {
52+
return String.join("\n", List.of(
53+
"Failed to parse line " + position,
54+
" " + error + ": " + line
55+
));
56+
}
57+
}
58+
59+
public void handle() {
60+
InscriptConstants.ERROR_HANDLER.getValue().accept(this);
61+
}
62+
}

src/main/java/dev/manere/inscript/Inscript.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.errorprone.annotations.CanIgnoreReturnValue;
44
import dev.manere.inscript.format.FileFormat;
55
import dev.manere.inscript.format.FileFormats;
6+
import dev.manere.inscript.format.InscriptReader;
67
import dev.manere.inscript.node.RootSectionNode;
78
import dev.manere.inscript.value.ValueRegistry;
89
import org.jetbrains.annotations.NotNull;
@@ -111,17 +112,27 @@ public void loadFromDisk() {
111112

112113
try (final BufferedReader reader = Files.newBufferedReader(getPath().get())) {
113114
root.reset();
114-
format.load(reader, root);
115+
116+
final List<ErrorContext> errors = format.load(InscriptReader.reader(reader.lines().toList()), this);
117+
118+
for (final ErrorContext error : errors) {
119+
error.handle();
120+
}
115121
} catch (final Exception e) {
116122
throw new InscriptException(e);
117123
}
118124
}
119125

120126
public void loadFromString(final @NotNull String configString) {
121-
try (final BufferedReader reader = new BufferedReader(new StringReader(configString))) {
127+
try {
122128
root.reset();
123-
format.load(reader, root);
124-
} catch (Exception e) {
129+
130+
final List<ErrorContext> errors = format.load(InscriptReader.reader(configString.lines().toList()), this);
131+
132+
for (final ErrorContext error : errors) {
133+
error.handle();
134+
}
135+
} catch (final Exception e) {
125136
throw new InscriptException(e);
126137
}
127138
}

src/main/java/dev/manere/inscript/InscriptConstant.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public InscriptConstant(final @NotNull T value) {
1212

1313
@NotNull
1414
@CanIgnoreReturnValue
15-
public InscriptConstant<T> value(final @NotNull T value) {
15+
public InscriptConstant<T> edit(final @NotNull T value) {
1616
this.value = value;
1717
return this;
1818
}

src/main/java/dev/manere/inscript/InscriptConstants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.jetbrains.annotations.NotNull;
44

55
import java.util.Optional;
6+
import java.util.function.Consumer;
67
import java.util.function.Function;
78
import java.util.function.Supplier;
89

@@ -16,4 +17,7 @@ public interface InscriptConstants {
1617

1718
@NotNull
1819
InscriptConstant<Supplier<Optional<String>>> VERSION = new InscriptConstant<>(() -> Optional.ofNullable(InscriptConstants.class.getPackage().getImplementationVersion()));
20+
21+
@NotNull
22+
InscriptConstant<Consumer<ErrorContext>> ERROR_HANDLER = new InscriptConstant<>(context -> System.err.println(context.buildDefault()));
1923
}

src/main/java/dev/manere/inscript/SimpleConfigSection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public record SimpleConfigSection(@NotNull SectionNode sectionNode) implements C
3131
if (sectionFound.isPresent()) return sectionFound.get();
3232

3333
final SectionNode created = new SectionNode() {
34-
private final Set<ConfigNode> nodes = Collections.newSetFromMap(new ConcurrentHashMap<>());
34+
private final Set<ConfigNode> nodes = new LinkedHashSet<>();
3535

3636
@NotNull
3737
public Set<ConfigNode> getChildren() {

0 commit comments

Comments
 (0)