parse function Null safety

Future parse(
  1. CommandsPlugin commands,
  2. IChatContext context,
  3. StringView toParse,
  4. Type expectedType,
  5. {Converter? converterOverride}
)

Apply a converter to an input and return the result.

  • commands is the instance of CommandsPlugin used to retrieve the appropriate converter;
  • context is the context to parse arguments in;
  • toParse is the input to the converter;
  • expectedType is the type that should be returned from this function;
  • converterOverride can be specified to use that converter instead of querying commands for the converter to use.

You might also be interested in:

Implementation

Future<dynamic> parse(
  CommandsPlugin commands,
  IChatContext context,
  StringView toParse,
  Type expectedType, {
  Converter<dynamic>? converterOverride,
}) async {
  Converter<dynamic>? converter = converterOverride ?? commands.getConverter(expectedType);
  if (converter == null) {
    throw NoConverterException(expectedType, context);
  }

  try {
    dynamic parsed = await converter.convert(toParse, context);

    if (parsed == null) {
      throw BadInputException('Could not parse input $context to type "$expectedType"', context);
    }

    return parsed;
  } on ParsingException catch (e) {
    throw BadInputException('Bad input $context: ${e.message}', context);
  }
}