Skip to content

Commit

Permalink
Split error acceptors (sinks) and collectors
Browse files Browse the repository at this point in the history
  • Loading branch information
NebelNidas committed Apr 15, 2024
1 parent 12dfc4d commit 9333443
Show file tree
Hide file tree
Showing 18 changed files with 225 additions and 163 deletions.
18 changes: 9 additions & 9 deletions src/main/java/net/fabricmc/mappingio/MappingReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

import org.jetbrains.annotations.Nullable;

import net.fabricmc.mappingio.format.ErrorCollector;
import net.fabricmc.mappingio.format.ErrorCollector.Severity;
import net.fabricmc.mappingio.format.ErrorCollector.ThrowingErrorCollector;
import net.fabricmc.mappingio.format.ErrorAcceptor;
import net.fabricmc.mappingio.format.MappingFormat;
import net.fabricmc.mappingio.format.ThrowingErrorAcceptor;
import net.fabricmc.mappingio.format.ParsingError.Severity;
import net.fabricmc.mappingio.format.enigma.EnigmaDirReader;
import net.fabricmc.mappingio.format.enigma.EnigmaFileReader;
import net.fabricmc.mappingio.format.jobf.JobfFileReader;
Expand Down Expand Up @@ -224,7 +224,7 @@ public static void read(Path path, MappingVisitor visitor) throws IOException {
* @param errorCollector The error collector instance to log errors to.
* @throws IOException If the format can't be detected or reading fails.
*/
public static void read(Path path, MappingVisitor visitor, ErrorCollector errorCollector) throws IOException {
public static void read(Path path, MappingVisitor visitor, ErrorAcceptor errorCollector) throws IOException {
read(path, null, visitor, errorCollector);
}

Expand All @@ -238,7 +238,7 @@ public static void read(Path path, MappingVisitor visitor, ErrorCollector errorC
*/
@Deprecated
public static void read(Path path, MappingFormat format, MappingVisitor visitor) throws IOException {
read(path, format, visitor, new ThrowingErrorCollector(Severity.ERROR));
read(path, format, visitor, new ThrowingErrorAcceptor(Severity.ERROR));
}

/**
Expand All @@ -249,7 +249,7 @@ public static void read(Path path, MappingFormat format, MappingVisitor visitor)
* @param visitor The receiving visitor.
* @throws IOException If reading fails.
*/
public static void read(Path path, MappingFormat format, MappingVisitor visitor, ErrorCollector errorCollector) throws IOException {
public static void read(Path path, MappingFormat format, MappingVisitor visitor, ErrorAcceptor errorCollector) throws IOException {
if (format == null) {
format = detectFormat(path);
if (format == null) throw new IOException("invalid/unsupported mapping format");
Expand Down Expand Up @@ -290,7 +290,7 @@ public static void read(Reader reader, MappingVisitor visitor) throws IOExceptio
* @param errorCollector The error collector instance to log errors to.
* @throws IOException If the format can't be detected or reading fails.
*/
public static void read(Reader reader, MappingVisitor visitor, ErrorCollector errorCollector) throws IOException {
public static void read(Reader reader, MappingVisitor visitor, ErrorAcceptor errorCollector) throws IOException {
read(reader, null, visitor, errorCollector);
}

Expand All @@ -304,7 +304,7 @@ public static void read(Reader reader, MappingVisitor visitor, ErrorCollector er
*/
@Deprecated
public static void read(Reader reader, MappingFormat format, MappingVisitor visitor) throws IOException {
read(reader, format, visitor, new ThrowingErrorCollector(Severity.ERROR));
read(reader, format, visitor, new ThrowingErrorAcceptor(Severity.ERROR));
}

/**
Expand All @@ -316,7 +316,7 @@ public static void read(Reader reader, MappingFormat format, MappingVisitor visi
* @param errorCollector The error collector instance to log errors to.
* @throws IOException If reading fails.
*/
public static void read(Reader reader, MappingFormat format, MappingVisitor visitor, ErrorCollector errorCollector) throws IOException {
public static void read(Reader reader, MappingFormat format, MappingVisitor visitor, ErrorAcceptor errorCollector) throws IOException {
if (format == null) {
if (!reader.markSupported()) reader = new BufferedReader(reader);
reader.mark(DETECT_HEADER_LEN);
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/net/fabricmc/mappingio/format/ErrorAcceptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2023 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.mappingio.format;

import java.io.IOException;

import net.fabricmc.mappingio.format.ParsingError.Severity;

public interface ErrorAcceptor {
default void addInfo(String message) throws IOException {
add(Severity.INFO, message);
}

default void addWarning(String message) throws IOException {
add(Severity.WARNING, message);
}

default void addError(String message) throws IOException {
add(Severity.ERROR, message);
}

void add(Severity severity, String message) throws IOException;
}
81 changes: 4 additions & 77 deletions src/main/java/net/fabricmc/mappingio/format/ErrorCollector.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 FabricMC
* Copyright (c) 2024 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,17 +18,16 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.jetbrains.annotations.ApiStatus;
import net.fabricmc.mappingio.format.ParsingError.Severity;

public interface ErrorCollector {
public interface ErrorCollector extends ErrorAcceptor {
static ErrorCollector create() {
return new ErrorCollector() {
@Override
public void add(Severity severity, String message) throws IOException {
errors.add(new ParsingError(severity, message));
errors.add(ParsingError.create(severity, message));
}

@Override
Expand All @@ -40,77 +39,5 @@ public List<ParsingError> getErrors() {
};
}

default void addInfo(String message) throws IOException {
add(Severity.INFO, message);
}

default void addWarning(String message) throws IOException {
add(Severity.WARNING, message);
}

default void addError(String message) throws IOException {
add(Severity.ERROR, message);
}

void add(Severity severity, String message) throws IOException;

List<ParsingError> getErrors();

enum Severity {
/**
* When something's technically wrong but doesn't affect
* parsing or the mapping data in any way.
*/
INFO,
/**
* When element data is partially missing, but the rest of the element
* could still be deciphered and it didn't have to be skipped entirely.
* Or when an unknown top-level element is encountered.
*/
WARNING,
/**
* An issue so severe that parsing of entire elements had to be skipped.
* E.g. a class's/member's source name being absent.
*/
ERROR
}

class ParsingError {
ParsingError(Severity severity, String message) {
this.severity = severity;
this.message = message;
}

public Severity getSeverity() {
return severity;
}

public String getMessage() {
return message;
}

private final Severity severity;
private final String message;
}

@ApiStatus.Internal
class ThrowingErrorCollector implements ErrorCollector {
public ThrowingErrorCollector(Severity severityToThrowAt) {
this.severityToThrowAt = severityToThrowAt;
}

@Override
public void add(Severity severity, String message) throws IOException {
if (severity.compareTo(severityToThrowAt) >= 0) {
throw new IOException(message);
}
}

@Override
public List<ParsingError> getErrors() {
return Collections.emptyList();
}

private Severity severityToThrowAt;
}
}
59 changes: 59 additions & 0 deletions src/main/java/net/fabricmc/mappingio/format/ParsingError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2024 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.mappingio.format;

import org.jetbrains.annotations.ApiStatus;

@ApiStatus.NonExtendable
public interface ParsingError {
static ParsingError create(Severity severity, String message) {
return new ParsingError() {
@Override
public Severity getSeverity() {
return severity;
}

@Override
public String getMessage() {
return message;
}
};
}

Severity getSeverity();

String getMessage();

enum Severity {
/**
* When something's technically wrong but doesn't affect
* parsing or the mapping data in any way.
*/
INFO,
/**
* When element data is partially missing, but the rest of the element
* could still be deciphered and it didn't have to be skipped entirely.
* Or when an unknown top-level element is encountered.
*/
WARNING,
/**
* An issue so severe that parsing of entire elements had to be skipped.
* E.g. a class's/member's source name being absent.
*/
ERROR
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.mappingio.format;

import java.io.IOException;

import org.jetbrains.annotations.ApiStatus;

import net.fabricmc.mappingio.format.ParsingError.Severity;

@ApiStatus.Internal
public final class ThrowingErrorAcceptor implements ErrorAcceptor {
public ThrowingErrorAcceptor(Severity severityToThrowAt) {
this.severityToThrowAt = severityToThrowAt;
}

@Override
public void add(Severity severity, String message) throws IOException {
if (severity.compareTo(severityToThrowAt) >= 0) {
throw new IOException(message);
}
}

private Severity severityToThrowAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
import net.fabricmc.mappingio.MappingUtil;
import net.fabricmc.mappingio.MappingVisitor;
import net.fabricmc.mappingio.adapter.ForwardingMappingVisitor;
import net.fabricmc.mappingio.format.ErrorCollector;
import net.fabricmc.mappingio.format.ErrorCollector.Severity;
import net.fabricmc.mappingio.format.ErrorCollector.ThrowingErrorCollector;
import net.fabricmc.mappingio.format.ErrorAcceptor;
import net.fabricmc.mappingio.format.MappingFormat;
import net.fabricmc.mappingio.format.ThrowingErrorAcceptor;
import net.fabricmc.mappingio.format.ParsingError.Severity;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MemoryMappingTree;

Expand All @@ -51,16 +51,16 @@ public static void read(Path dir, MappingVisitor visitor) throws IOException {
read(dir, MappingUtil.NS_SOURCE_FALLBACK, MappingUtil.NS_TARGET_FALLBACK, visitor);
}

public static void read(Path dir, MappingVisitor visitor, ErrorCollector errorCollector) throws IOException {
public static void read(Path dir, MappingVisitor visitor, ErrorAcceptor errorCollector) throws IOException {
read(dir, MappingUtil.NS_SOURCE_FALLBACK, MappingUtil.NS_TARGET_FALLBACK, visitor, errorCollector);
}

@Deprecated
public static void read(Path dir, String sourceNs, String targetNs, MappingVisitor visitor) throws IOException {
read(dir, sourceNs, targetNs, visitor, new ThrowingErrorCollector(Severity.ERROR));
read(dir, sourceNs, targetNs, visitor, new ThrowingErrorAcceptor(Severity.ERROR));
}

public static void read(Path dir, String sourceNs, String targetNs, MappingVisitor visitor, ErrorCollector errorCollector) throws IOException {
public static void read(Path dir, String sourceNs, String targetNs, MappingVisitor visitor, ErrorAcceptor errorCollector) throws IOException {
Set<MappingFlag> flags = visitor.getFlags();
MappingVisitor parentVisitor = null;

Expand Down
Loading

0 comments on commit 9333443

Please sign in to comment.