mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 06:16:04 +00:00
move parsers
into arguments
This commit is contained in:
parent
8d71fbf813
commit
b8ceb56e71
7 changed files with 24 additions and 13 deletions
7
azalea-brigadier/src/arguments/argument_type.rs
Normal file
7
azalea-brigadier/src/arguments/argument_type.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
use std::{any::Any, rc::Rc};
|
||||
|
||||
use crate::{exceptions::CommandSyntaxException, string_reader::StringReader};
|
||||
|
||||
pub trait ArgumentType {
|
||||
fn parse(&self, reader: &mut StringReader) -> Result<Rc<dyn Any>, CommandSyntaxException>;
|
||||
}
|
|
@ -6,9 +6,7 @@ use crate::{
|
|||
string_reader::StringReader,
|
||||
};
|
||||
|
||||
pub trait Parser {
|
||||
fn parse(&self, reader: &mut StringReader) -> Result<Rc<dyn Any>, CommandSyntaxException>;
|
||||
}
|
||||
use super::ArgumentType;
|
||||
|
||||
#[derive(Default)]
|
||||
struct Integer {
|
||||
|
@ -16,7 +14,7 @@ struct Integer {
|
|||
pub maximum: Option<i32>,
|
||||
}
|
||||
|
||||
impl Parser for Integer {
|
||||
impl ArgumentType for Integer {
|
||||
fn parse(&self, reader: &mut StringReader) -> Result<Rc<dyn Any>, CommandSyntaxException> {
|
||||
let start = reader.cursor;
|
||||
let result = reader.read_int()?;
|
||||
|
@ -44,7 +42,7 @@ impl Parser for Integer {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn integer() -> impl Parser {
|
||||
pub fn integer() -> impl ArgumentType {
|
||||
Integer::default()
|
||||
}
|
||||
pub fn get_integer<S>(context: &CommandContext<S>, name: &str) -> Option<i32> {
|
4
azalea-brigadier/src/arguments/mod.rs
Normal file
4
azalea-brigadier/src/arguments/mod.rs
Normal file
|
@ -0,0 +1,4 @@
|
|||
mod argument_type;
|
||||
pub mod integer_argument_type;
|
||||
|
||||
pub use argument_type::ArgumentType;
|
|
@ -141,8 +141,8 @@ mod tests {
|
|||
use std::rc::Rc;
|
||||
|
||||
use crate::{
|
||||
arguments::integer_argument_type::integer,
|
||||
builder::{literal_argument_builder::literal, required_argument_builder::argument},
|
||||
parsers::integer,
|
||||
};
|
||||
|
||||
use super::ArgumentBuilder;
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
use super::argument_builder::{ArgumentBuilder, ArgumentBuilderType};
|
||||
use crate::{exceptions::CommandSyntaxException, parsers::Parser, string_reader::StringReader};
|
||||
use crate::{
|
||||
arguments::ArgumentType, exceptions::CommandSyntaxException, string_reader::StringReader,
|
||||
};
|
||||
use std::{any::Any, fmt::Debug, rc::Rc};
|
||||
|
||||
/// An argument node type. The `T` type parameter is the type of the argument,
|
||||
|
@ -7,10 +9,10 @@ use std::{any::Any, fmt::Debug, rc::Rc};
|
|||
#[derive(Clone)]
|
||||
pub struct Argument {
|
||||
pub name: String,
|
||||
parser: Rc<dyn Parser>,
|
||||
parser: Rc<dyn ArgumentType>,
|
||||
}
|
||||
impl Argument {
|
||||
pub fn new(name: &str, parser: Rc<dyn Parser>) -> Self {
|
||||
pub fn new(name: &str, parser: Rc<dyn ArgumentType>) -> Self {
|
||||
Self {
|
||||
name: name.to_string(),
|
||||
parser,
|
||||
|
@ -38,6 +40,6 @@ impl Debug for Argument {
|
|||
}
|
||||
|
||||
/// Shortcut for creating a new argument builder node.
|
||||
pub fn argument<S>(name: &str, parser: impl Parser + 'static) -> ArgumentBuilder<S> {
|
||||
pub fn argument<S>(name: &str, parser: impl ArgumentType + 'static) -> ArgumentBuilder<S> {
|
||||
ArgumentBuilder::new(Argument::new(name, Rc::new(parser)).into())
|
||||
}
|
||||
|
|
|
@ -301,8 +301,8 @@ impl<S> Clone for CommandDispatcher<S> {
|
|||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
arguments::integer_argument_type::integer,
|
||||
builder::{literal_argument_builder::literal, required_argument_builder::argument},
|
||||
parsers::integer,
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
pub mod arguments;
|
||||
pub mod builder;
|
||||
pub mod command_dispatcher;
|
||||
pub mod context;
|
||||
|
@ -5,7 +6,6 @@ pub mod exceptions;
|
|||
pub mod message;
|
||||
pub mod modifier;
|
||||
pub mod parse_results;
|
||||
pub mod parsers;
|
||||
pub mod string_reader;
|
||||
pub mod tree;
|
||||
|
||||
|
@ -15,10 +15,10 @@ mod tests {
|
|||
use std::rc::Rc;
|
||||
|
||||
use crate::{
|
||||
arguments::integer_argument_type::{get_integer, integer},
|
||||
builder::{literal_argument_builder::literal, required_argument_builder::argument},
|
||||
command_dispatcher::CommandDispatcher,
|
||||
context::CommandContext,
|
||||
parsers::{get_integer, integer},
|
||||
};
|
||||
|
||||
struct CommandSourceStack {
|
||||
|
|
Loading…
Add table
Reference in a new issue