use crate::{
generator::parser::command::{argument::ArgumentValue, resource_location::ResourceLocationRef},
utils::Map0,
};
#[derive(Clone, Debug, PartialEq)]
pub struct MinecraftResource<'l>(pub ResourceLocationRef<&'l str>);
impl<'l> ArgumentValue<'l> for MinecraftResource<'l> {
fn parse(string: &'l str) -> Result<(Self, usize), String> {
Ok(ResourceLocationRef::parse(string)?.map0(Self))
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct MinecraftResourceLocation<'l>(pub ResourceLocationRef<&'l str>);
impl<'l> ArgumentValue<'l> for MinecraftResourceLocation<'l> {
fn parse(string: &'l str) -> Result<(Self, usize), String> {
Ok(ResourceLocationRef::parse(string)?.map0(Self))
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct MinecraftResourceOrTag<'l> {
tag: bool,
resource: ResourceLocationRef<&'l str>,
}
impl<'l> ArgumentValue<'l> for MinecraftResourceOrTag<'l> {
fn parse(string: &'l str) -> Result<(Self, usize), String> {
let suffix = string;
let (tag, suffix) = parse_prefix(suffix, '#');
let (resource, resource_len) = ResourceLocationRef::parse(suffix)?;
let suffix = &suffix[resource_len..];
Ok((
MinecraftResourceOrTag { tag, resource },
string.len() - suffix.len(),
))
}
}
fn parse_prefix(string: &str, prefix: char) -> (bool, &str) {
let suffix = string.strip_prefix(prefix);
(suffix.is_some(), suffix.unwrap_or(string))
}