mcfunction-debugger 2.0.0

A debugger for Minecraft's *.mcfunction files that does not require any Minecraft mods
Documentation
// Mcfunction-Debugger is a debugger for Minecraft's *.mcfunction files that does not require any
// Minecraft mods.
//
// © Copyright (C) 2021-2024 Adrodoc <adrodoc55@googlemail.com> & Skagaros <skagaros@gmail.com>
//
// This file is part of Mcfunction-Debugger.
//
// Mcfunction-Debugger is free software: you can redistribute it and/or modify it under the terms of
// the GNU General Public License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// Mcfunction-Debugger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with Mcfunction-Debugger.
// If not, see <http://www.gnu.org/licenses/>.

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))
}