Skip to main content

yolol_devices/value/
string.rs

1use std::fmt::Display;
2use std::ops::Add;
3use std::ops::Deref;
4use std::ops::DerefMut;
5use std::ops::Sub;
6
7use super::ValueTrait;
8use super::YololInt;
9use super::YololValue;
10
11#[derive(Debug, Clone, Default)]
12pub struct YololString(String);
13
14impl ValueTrait for YololString {
15    fn post_inc(&mut self) -> YololValue {
16        let org = self.clone();
17        *self = self.clone() + " ".into();
18        org.into()
19    }
20    fn pre_inc(&mut self) -> YololValue {
21        *self = self.clone() + " ".into();
22        self.clone().into()
23    }
24
25    fn post_dec(&mut self) -> Option<YololValue> {
26        if self.0.is_empty() {
27            return None;
28        }
29        let org = self.clone();
30        *self = self.0[0..self.0.len() - 1].into();
31        Some(org.into())
32    }
33
34    fn pre_dec(&mut self) -> Option<YololValue> {
35        if self.0.is_empty() {
36            return None;
37        }
38        *self = self.0[0..self.0.len() - 1].into();
39        Some(self.clone().into())
40    }
41
42    fn fac(&self) -> Option<YololValue> {
43        //runtime error
44        None
45    }
46
47    fn abs(&self) -> Option<YololValue> {
48        None
49    }
50
51    fn sqrt(&self) -> Option<YololValue> {
52        None
53    }
54
55    fn sin(&self) -> Option<YololValue> {
56        None
57    }
58
59    fn asin(&self) -> Option<YololValue> {
60        None
61    }
62
63    fn cos(&self) -> Option<YololValue> {
64        None
65    }
66
67    fn acos(&self) -> Option<YololValue> {
68        None
69    }
70
71    fn tan(&self) -> Option<YololValue> {
72        None
73    }
74
75    fn atan(&self) -> Option<YololValue> {
76        None
77    }
78
79    fn pow(&self, _: &YololValue) -> Option<YololValue> {
80        None
81    }
82
83    fn not(&self) -> YololValue {
84        false.into()
85    }
86}
87
88impl PartialEq for YololString {
89    fn eq(&self, other: &Self) -> bool {
90        self.0.eq(&other.0)
91    }
92}
93
94impl PartialOrd for YololString {
95    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
96        self.0.partial_cmp(&other.0)
97    }
98}
99
100impl From<&YololString> for bool {
101    fn from(_: &YololString) -> Self {
102        false
103    }
104}
105
106impl Deref for YololString {
107    type Target = String;
108
109    fn deref(&self) -> &Self::Target {
110        &self.0
111    }
112}
113
114impl DerefMut for YololString {
115    fn deref_mut(&mut self) -> &mut Self::Target {
116        &mut self.0
117    }
118}
119
120impl From<&str> for YololString {
121    fn from(v: &str) -> Self {
122        Self(v.to_string())
123    }
124}
125
126impl From<&YololInt> for YololString {
127    fn from(int: &YololInt) -> Self {
128        let f: f64 = int.into();
129        YololString(format!("{}", f))
130    }
131}
132
133impl Add for YololString {
134    type Output = YololString;
135
136    fn add(self, rhs: Self) -> Self::Output {
137        Self(format!("{}{}", self.0, rhs.0))
138    }
139}
140
141impl Sub for YololString {
142    type Output = Option<YololString>;
143
144    fn sub(self, rhs: Self) -> Self::Output {
145        if rhs.0.len() > self.0.len() {
146            return None;
147        } else if rhs.0 == self.0 {
148            return Some(Self("".into()));
149        } else {
150            for i in 0..(self.0.len() - rhs.0.len()) {
151                let s = self.0.len() - i - rhs.0.len();
152                let e = self.0.len() - i;
153                if rhs.0 == self.0[s..e] {
154                    return Some(Self(format!(
155                        "{}{}",
156                        &self.0[0..s],
157                        &self.0[e..self.0.len()]
158                    )));
159                }
160            }
161        }
162        Some(self)
163    }
164}
165
166impl Display for YololString {
167    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
168        self.0.fmt(f)
169    }
170}
171
172#[test]
173fn concat_test() {
174    let a = "Hello";
175    let b = " world";
176    assert_eq!(
177        YololString::from("Hello world"),
178        YololString::from(a) + YololString::from(b)
179    )
180}
181
182#[test]
183fn remove_test_1() {
184    let a = "Hello";
185    let b = " world";
186    assert_eq!(
187        YololString::from("Hello"),
188        (YololString::from(a) - YololString::from(b)).unwrap()
189    )
190}
191
192#[test]
193fn remove_test_2() {
194    let a = "Hello world";
195    let b = " world";
196    assert_eq!(
197        YololString::from("Hello"),
198        (YololString::from(a) - YololString::from(b)).unwrap()
199    )
200}
201
202#[test]
203fn remove_test_3() {
204    let b = "Hello world";
205    let a = " world";
206    assert_eq!(
207        YololString::from(" world"),
208        (YololString::from(a) - YololString::from(b)).unwrap()
209    )
210}
211
212#[test]
213fn remove_test_4() {
214    let a = "Hello world Hello world";
215    let b = " world";
216    assert_eq!(
217        YololString::from("Hello world Hello"),
218        (YololString::from(a) - YololString::from(b)).unwrap()
219    )
220}