doge_runtime/stdlib/
strings.rs1use crate::error::{DogeError, DogeResult};
2use crate::value::Value;
3
4fn str_arg<'a>(fname: &str, v: &'a Value) -> DogeResult<&'a str> {
7 crate::stdlib::str_arg("strings", fname, v)
8}
9
10pub fn strings_beeg(s: &Value) -> DogeResult {
12 Ok(Value::str(str_arg("beeg", s)?.to_uppercase()))
13}
14
15pub fn strings_smoll(s: &Value) -> DogeResult {
17 Ok(Value::str(str_arg("smoll", s)?.to_lowercase()))
18}
19
20pub fn strings_trim(s: &Value) -> DogeResult {
22 Ok(Value::str(str_arg("trim", s)?.trim()))
23}
24
25pub fn strings_split(s: &Value, sep: &Value) -> DogeResult {
29 let s = str_arg("split", s)?;
30 let sep = str_arg("split", sep)?;
31 if sep.is_empty() {
32 return Err(DogeError::value_error("cannot split on an empty Str"));
33 }
34 Ok(Value::list(s.split(sep).map(Value::str).collect()))
35}
36
37pub fn strings_join(parts: &Value, sep: &Value) -> DogeResult {
40 let items = match parts {
41 Value::List(items) => items.borrow(),
42 _ => return Err(DogeError::type_error("strings.join needs a List of Str")),
43 };
44 let mut pieces = Vec::with_capacity(items.len());
45 for item in items.iter() {
46 match item {
47 Value::Str(piece) => pieces.push(piece.to_string()),
48 _ => return Err(DogeError::type_error("strings.join needs a List of Str")),
49 }
50 }
51 let sep = str_arg("join", sep)?;
52 Ok(Value::str(pieces.join(sep)))
53}
54
55pub fn strings_contains(s: &Value, needle: &Value) -> DogeResult {
57 let s = str_arg("contains", s)?;
58 let needle = str_arg("contains", needle)?;
59 Ok(Value::Bool(s.contains(needle)))
60}
61
62pub fn strings_replace(s: &Value, from: &Value, to: &Value) -> DogeResult {
65 let s = str_arg("replace", s)?;
66 let from = str_arg("replace", from)?;
67 let to = str_arg("replace", to)?;
68 if from.is_empty() {
69 return Err(DogeError::value_error("cannot replace an empty Str"));
70 }
71 Ok(Value::str(s.replace(from, to)))
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77 use crate::error::ErrorKind;
78
79 #[test]
80 fn case_and_trim() {
81 assert!(matches!(strings_beeg(&Value::str("wow")).unwrap(), Value::Str(s) if &*s == "WOW"));
82 assert!(
83 matches!(strings_smoll(&Value::str("WOW")).unwrap(), Value::Str(s) if &*s == "wow")
84 );
85 assert!(
86 matches!(strings_trim(&Value::str(" hi ")).unwrap(), Value::Str(s) if &*s == "hi")
87 );
88 }
89
90 #[test]
91 fn split_keeps_empty_pieces_and_rejects_empty_sep() {
92 let parts = strings_split(&Value::str("a,,b"), &Value::str(",")).unwrap();
93 match parts {
94 Value::List(items) => assert_eq!(items.borrow().len(), 3),
95 _ => panic!("expected a list"),
96 }
97 assert_eq!(
98 strings_split(&Value::str("a"), &Value::str(""))
99 .unwrap_err()
100 .kind,
101 ErrorKind::ValueError
102 );
103 }
104
105 #[test]
106 fn join_needs_all_strs() {
107 let parts = Value::list(vec![Value::str("much"), Value::str("wow")]);
108 assert!(
109 matches!(strings_join(&parts, &Value::str(" ")).unwrap(), Value::Str(s) if &*s == "much wow")
110 );
111 let mixed = Value::list(vec![Value::str("a"), Value::int(1)]);
112 assert_eq!(
113 strings_join(&mixed, &Value::str(" ")).unwrap_err().kind,
114 ErrorKind::TypeError
115 );
116 }
117
118 #[test]
119 fn contains_and_replace() {
120 assert!(matches!(
121 strings_contains(&Value::str("kabosu"), &Value::str("bos")).unwrap(),
122 Value::Bool(true)
123 ));
124 assert!(
125 matches!(strings_replace(&Value::str("a-b-c"), &Value::str("-"), &Value::str("_")).unwrap(), Value::Str(s) if &*s == "a_b_c")
126 );
127 assert_eq!(
128 strings_replace(&Value::str("x"), &Value::str(""), &Value::str("y"))
129 .unwrap_err()
130 .kind,
131 ErrorKind::ValueError
132 );
133 }
134
135 #[test]
136 fn non_str_subject_is_a_type_error() {
137 assert_eq!(
138 strings_trim(&Value::int(1)).unwrap_err().kind,
139 ErrorKind::TypeError
140 );
141 }
142}