change function parameter types from &str to impl Into<String> if converted to string anyways

This commit is contained in:
Moritz Hölting 2025-03-07 16:45:36 +01:00
parent 811d715082
commit cf8c922704
3 changed files with 24 additions and 22 deletions

View File

@ -25,11 +25,11 @@ pub struct Function {
}
impl Function {
pub(in crate::datapack) fn new(namespace: &str, name: &str) -> Self {
pub(in crate::datapack) fn new(namespace: impl Into<String>, name: impl Into<String>) -> Self {
Self {
commands: Vec::new(),
name: name.to_string(),
namespace: namespace.to_string(),
name: name.into(),
namespace: namespace.into(),
}
}
/// Add a command to the function.

View File

@ -95,36 +95,37 @@ impl Datapack {
}
/// Mutably get a namespace by name or create a new one if it doesn't exist.
pub fn namespace_mut(&mut self, name: &str) -> &mut Namespace {
pub fn namespace_mut(&mut self, name: impl Into<String>) -> &mut Namespace {
let name = name.into();
self.namespaces
.entry(name.to_string())
.entry(name.clone())
.or_insert_with(|| Namespace::new(name))
}
/// Add a function to the tick function list.
pub fn add_tick(&mut self, function: &str) {
pub fn add_tick(&mut self, function: impl Into<String>) {
self.namespace_mut("minecraft")
.tag_mut("tick", tag::TagType::Function)
.add_value(tag::TagValue::Simple(function.to_string()));
.add_value(tag::TagValue::Simple(function.into()));
}
/// Add a function to the load function list.
pub fn add_load(&mut self, function: &str) {
pub fn add_load(&mut self, function: impl Into<String>) {
self.namespace_mut("minecraft")
.tag_mut("load", tag::TagType::Function)
.add_value(tag::TagValue::Simple(function.to_string()));
.add_value(tag::TagValue::Simple(function.into()));
}
/// Register a scoreboard.
pub fn register_scoreboard(
&mut self,
name: &str,
criteria: Option<&str>,
display_name: Option<&str>,
name: impl Into<String>,
criteria: Option<impl Into<String>>,
display_name: Option<impl Into<String>>,
) {
self.scoreboards.insert(
name.to_string(),
(criteria.map(String::from), display_name.map(String::from)),
name.into(),
(criteria.map(Into::into), display_name.map(Into::into)),
);
}

View File

@ -28,9 +28,9 @@ pub struct Namespace {
impl Namespace {
/// Create a new namespace.
pub(in crate::datapack) fn new(name: &str) -> Self {
pub(in crate::datapack) fn new(name: impl Into<String>) -> Self {
Self {
name: name.to_string(),
name: name.into(),
functions: HashMap::new(),
tags: HashMap::new(),
}
@ -62,23 +62,24 @@ impl Namespace {
/// Mutably get a function by name or create a new one if it doesn't exist.
#[must_use]
pub fn function_mut(&mut self, name: &str) -> &mut Function {
pub fn function_mut(&mut self, name: impl Into<String>) -> &mut Function {
let name = name.into();
self.functions
.entry(name.to_string())
.entry(name.clone())
.or_insert_with(|| Function::new(&self.name, name))
}
/// Get a tag by name and type.
#[must_use]
pub fn tag(&self, name: &str, tag_type: TagType) -> Option<&Tag> {
self.tags.get(&(name.to_string(), tag_type))
pub fn tag(&self, name: impl Into<String>, tag_type: TagType) -> Option<&Tag> {
self.tags.get(&(name.into(), tag_type))
}
/// Mutably get a tag by name and type or create a new one if it doesn't exist.
#[must_use]
pub fn tag_mut(&mut self, name: &str, tag_type: TagType) -> &mut Tag {
pub fn tag_mut(&mut self, name: impl Into<String>, tag_type: TagType) -> &mut Tag {
self.tags
.entry((name.to_string(), tag_type))
.entry((name.into(), tag_type))
.or_insert_with(|| Tag::new(false))
}