Skip to main content

neo_types/
string.rs

1// Copyright (c) 2025-2026 R3E Network
2// Licensed under the MIT License
3
4use std::fmt;
5use std::string::String;
6
7#[cfg(feature = "serde")]
8use serde::{Deserialize, Serialize};
9
10/// Neo N3 String type
11#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
12#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
13pub struct NeoString {
14    data: String,
15}
16
17impl NeoString {
18    pub fn new(data: String) -> Self {
19        Self { data }
20    }
21
22    /// Creates a `NeoString` from a string slice.
23    #[allow(clippy::should_implement_trait)]
24    pub fn from_str(s: &str) -> Self {
25        Self {
26            data: String::from(s),
27        }
28    }
29
30    pub fn as_str(&self) -> &str {
31        &self.data
32    }
33
34    pub fn len(&self) -> usize {
35        self.data.len()
36    }
37
38    pub fn is_empty(&self) -> bool {
39        self.data.is_empty()
40    }
41}
42
43impl fmt::Display for NeoString {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        write!(f, "{}", self.data)
46    }
47}
48
49impl std::str::FromStr for NeoString {
50    type Err = std::convert::Infallible;
51    fn from_str(s: &str) -> Result<Self, Self::Err> {
52        Ok(Self {
53            data: String::from(s),
54        })
55    }
56}
57
58impl From<&str> for NeoString {
59    fn from(s: &str) -> Self {
60        Self {
61            data: String::from(s),
62        }
63    }
64}
65
66impl From<String> for NeoString {
67    fn from(data: String) -> Self {
68        Self { data }
69    }
70}
71
72impl AsRef<str> for NeoString {
73    fn as_ref(&self) -> &str {
74        &self.data
75    }
76}
77
78impl PartialEq<str> for NeoString {
79    fn eq(&self, other: &str) -> bool {
80        self.data == other
81    }
82}
83
84impl PartialEq<&str> for NeoString {
85    fn eq(&self, other: &&str) -> bool {
86        self.data == *other
87    }
88}