All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kz.greetgo.logging.structure.util.ReaderUtil Maven / Gradle / Ivy

There is a newer version: 0.2.0
Show newest version
package kz.greetgo.logging.structure.util;

import kz.greetgo.logging.structure.model.Level;
import kz.greetgo.logging.structure.parser.model.InstructionPart;
import kz.greetgo.logging.structure.parser.model.ParseError;
import kz.greetgo.logging.structure.parser.model.ParseErrorCode;
import kz.greetgo.logging.structure.parser.model.ParseErrorHandler;
import kz.greetgo.logging.structure.parser.model.SubInstruction;

import java.util.Optional;

public class ReaderUtil {
  public static Optional readLevel(SubInstruction subInstruction, ParseErrorHandler errorHandler) {
    Optional atValue = subInstruction.partList.at(1);

    if (atValue.isEmpty()) {
      errorHandler.happenedParseError(new ParseError(subInstruction.line, subInstruction.partList.get(0).range,
                                                     ParseErrorCode.SUB_INSTRUCTION_LEVEL_WITHOUT_VALUE,
                                                     "Подинструкция level без значения"));
      return Optional.empty();
    }

    try {
      var level = Level.valueOf(atValue.orElseThrow().str());
      return Optional.of(level);
    } catch (IllegalArgumentException e) {
      errorHandler.happenedParseError(new ParseError(subInstruction.line, subInstruction.partList.get(0).range,
                                                     ParseErrorCode.SUB_INSTRUCTION_LEVEL_WITH_UNKNOWN_VALUE,
                                                     "Подинструкция level с неизвестным значением: " +
                                                       "`" + atValue.orElseThrow().str() + "`"));
      return Optional.empty();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy