khora_data/ecs/components/name.rs
1// Copyright 2025 eraflo
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use khora_macros::Component;
16
17/// A component providing a human-readable name for an entity.
18///
19/// Used by the editor scene tree and debug utilities to display entities
20/// with meaningful identifiers instead of raw `EntityId` values.
21#[derive(Debug, Clone, PartialEq, Eq, Component, Default)]
22#[component(domain = Spatial)]
23pub struct Name(pub String);
24
25impl Name {
26 /// Creates a new `Name` from the given string.
27 pub fn new(name: impl Into<String>) -> Self {
28 Self(name.into())
29 }
30
31 /// Returns the name as a string slice.
32 pub fn as_str(&self) -> &str {
33 &self.0
34 }
35}
36
37impl std::fmt::Display for Name {
38 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39 f.write_str(&self.0)
40 }
41}
42
43impl<S: Into<String>> From<S> for Name {
44 fn from(s: S) -> Self {
45 Self(s.into())
46 }
47}