|
| 1 | +use super::argument_parser::*; |
| 2 | + |
| 3 | +#[derive(Clone)] |
| 4 | +pub enum ArgumentType { |
| 5 | + String, |
| 6 | + Integer { min: i32, max: i32 }, |
| 7 | + Float { min: f32, max: f32 }, |
| 8 | + Boolean, |
| 9 | + Player, |
| 10 | + Direction, |
| 11 | + Vec3, |
| 12 | + ColumnPos, |
| 13 | + Container, |
| 14 | + Pattern, |
| 15 | + Mask, |
| 16 | + DirectionExt, |
| 17 | + BlockPos, |
| 18 | + GreedyString, |
| 19 | + Flags { flags: Vec<FlagSpec> }, |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Default, Clone)] |
| 23 | +pub struct ArgumentTypeFlagBuilder { |
| 24 | + flags: Vec<FlagSpec>, |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Clone, PartialEq, Eq, Hash)] |
| 28 | +pub struct FlagSpec { |
| 29 | + pub(super) short: Option<char>, |
| 30 | + pub(super) long: String, |
| 31 | +} |
| 32 | + |
| 33 | +impl ArgumentType { |
| 34 | + pub(super) fn parse<'a>(&self, input: &'a str) -> ArgumentParseResult<'a> { |
| 35 | + match self { |
| 36 | + ArgumentType::String => parse_string(input), |
| 37 | + ArgumentType::Integer { min, max } => { |
| 38 | + let (value, rest) = parse_integer(input)?; |
| 39 | + if let Ok(value) = value.as_integer() { |
| 40 | + if !(*min..=*max).contains(&value) { |
| 41 | + return Err(()); |
| 42 | + } |
| 43 | + } |
| 44 | + Ok((value, rest)) |
| 45 | + } |
| 46 | + ArgumentType::Float { min, max } => { |
| 47 | + let (value, rest) = parse_float(input)?; |
| 48 | + if let Ok(value) = value.as_float() { |
| 49 | + if !(*min..=*max).contains(&value) { |
| 50 | + return Err(()); |
| 51 | + } |
| 52 | + } |
| 53 | + Ok((value, rest)) |
| 54 | + } |
| 55 | + ArgumentType::Boolean => parse_boolean(input), |
| 56 | + ArgumentType::Player => parse_player(input), |
| 57 | + ArgumentType::Direction => parse_direction(input), |
| 58 | + ArgumentType::Vec3 => parse_vec3(input), |
| 59 | + ArgumentType::ColumnPos => parse_column_pos(input), |
| 60 | + ArgumentType::Container => parse_container(input), |
| 61 | + ArgumentType::Pattern => parse_pattern(input), |
| 62 | + ArgumentType::Mask => parse_mask(input), |
| 63 | + ArgumentType::DirectionExt => parse_direction_ext(input), |
| 64 | + ArgumentType::BlockPos => parse_block_pos(input), |
| 65 | + ArgumentType::GreedyString => parse_greedy_string(input), |
| 66 | + ArgumentType::Flags { flags: specs } => parse_flags(input, specs), |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + pub fn string() -> Self { |
| 71 | + ArgumentType::String |
| 72 | + } |
| 73 | + |
| 74 | + pub fn integer(min: i32, max: i32) -> Self { |
| 75 | + ArgumentType::Integer { min, max } |
| 76 | + } |
| 77 | + |
| 78 | + pub fn float(min: f32, max: f32) -> Self { |
| 79 | + ArgumentType::Float { min, max } |
| 80 | + } |
| 81 | + |
| 82 | + pub fn boolean() -> Self { |
| 83 | + ArgumentType::Boolean |
| 84 | + } |
| 85 | + |
| 86 | + pub fn player() -> Self { |
| 87 | + ArgumentType::Player |
| 88 | + } |
| 89 | + |
| 90 | + pub fn direction() -> Self { |
| 91 | + ArgumentType::Direction |
| 92 | + } |
| 93 | + |
| 94 | + pub fn vec3() -> Self { |
| 95 | + ArgumentType::Vec3 |
| 96 | + } |
| 97 | + |
| 98 | + pub fn column_pos() -> Self { |
| 99 | + ArgumentType::ColumnPos |
| 100 | + } |
| 101 | + |
| 102 | + pub fn container() -> Self { |
| 103 | + ArgumentType::Container |
| 104 | + } |
| 105 | + |
| 106 | + pub fn pattern() -> Self { |
| 107 | + ArgumentType::Pattern |
| 108 | + } |
| 109 | + |
| 110 | + pub fn mask() -> Self { |
| 111 | + ArgumentType::Mask |
| 112 | + } |
| 113 | + |
| 114 | + pub fn direction_ext() -> Self { |
| 115 | + ArgumentType::DirectionExt |
| 116 | + } |
| 117 | + |
| 118 | + pub fn block_pos() -> Self { |
| 119 | + ArgumentType::BlockPos |
| 120 | + } |
| 121 | + |
| 122 | + pub fn greedy_string() -> Self { |
| 123 | + ArgumentType::GreedyString |
| 124 | + } |
| 125 | + |
| 126 | + pub fn flags() -> ArgumentTypeFlagBuilder { |
| 127 | + ArgumentTypeFlagBuilder::default() |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +impl ArgumentTypeFlagBuilder { |
| 132 | + pub fn add(mut self, flag: impl Into<FlagSpec>) -> Self { |
| 133 | + self.flags.push(flag.into()); |
| 134 | + self |
| 135 | + } |
| 136 | + |
| 137 | + pub fn build(self) -> ArgumentType { |
| 138 | + ArgumentType::Flags { flags: self.flags } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +impl From<ArgumentTypeFlagBuilder> for ArgumentType { |
| 143 | + fn from(value: ArgumentTypeFlagBuilder) -> Self { |
| 144 | + value.build() |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +impl From<&str> for FlagSpec { |
| 149 | + fn from(value: &str) -> Self { |
| 150 | + Self { |
| 151 | + short: None, |
| 152 | + long: value.to_string(), |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +impl From<(&str, char)> for FlagSpec { |
| 158 | + fn from(value: (&str, char)) -> Self { |
| 159 | + Self { |
| 160 | + short: Some(value.1), |
| 161 | + long: value.0.to_string(), |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments