1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::{
cmp::Ordering,
fmt::{Display, Write},
hash::Hash,
};
use crate::{symbol::Symbol, DynamicValue, FaultKind, PoppedValues, Value, ValueKind};
#[must_use]
pub struct StringLiteralDisplay<'a>(&'a str);
impl<'a> StringLiteralDisplay<'a> {
pub fn new(value: &'a str) -> Self {
Self(value)
}
}
impl<'a> Display for StringLiteralDisplay<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_char('"')?;
for ch in self.0.chars() {
match ch {
'"' => {
f.write_str("\\\"")?;
}
'\\' => {
f.write_str("\\\\")?;
}
ch if ch.is_alphanumeric() || ch == ' ' || ch.is_ascii_punctuation() => {
f.write_char(ch)?;
}
'\t' => {
f.write_str("\\t")?;
}
'\r' => {
f.write_str("\\r")?;
}
'\n' => {
f.write_str("\\n")?;
}
other => {
let codepoint = u32::from(other);
write!(f, "\\u{{{codepoint:x}}}").expect("error writing codepoint");
}
}
}
f.write_char('"')
}
}
impl DynamicValue for String {
fn is_truthy(&self) -> bool {
!self.is_empty()
}
fn kind(&self) -> Symbol {
Symbol::from("String")
}
fn partial_eq(&self, other: &Value) -> Option<bool> {
other.as_dynamic::<Self>().map(|other| self == other)
}
fn partial_cmp(&self, other: &Value) -> Option<Ordering> {
other.as_dynamic::<Self>().map(|other| self.cmp(other))
}
fn call(&self, name: &Symbol, args: &mut PoppedValues<'_>) -> Result<Value, FaultKind> {
match name.as_str() {
"replace" => {
let needle = args.next_argument("pattern")?;
let needle = needle.as_dynamic::<Self>().ok_or_else(|| {
FaultKind::invalid_type(
"search pattern must be a string. Found `@received-value` (@received-kind)",
needle.clone(),
)
})?;
let replacement = args.next_argument("replacement")?;
let replacement = replacement.as_dynamic::<Self>().ok_or_else(|| {
FaultKind::invalid_type(
"replacement must be a string. Found `@received-value` (@received-kind)",
replacement.clone(),
)
})?;
args.verify_empty()?;
Ok(Value::dynamic(self.replace(needle, replacement)))
}
_ => Err(FaultKind::UnknownFunction {
kind: ValueKind::Dynamic(self.kind()),
name: name.clone(),
}),
}
}
fn to_source(&self) -> Option<String> {
Some(StringLiteralDisplay::new(self).to_string())
}
fn checked_add(&self, other: &Value, _is_reverse: bool) -> Result<Option<Value>, FaultKind> {
if let Some(other) = other.as_dynamic::<Self>() {
let combined = [self.as_str(), other.as_str()].join("");
Ok(Some(Value::dynamic(combined)))
} else {
Ok(None)
}
}
fn checked_mul(&self, other: &Value, _is_reverse: bool) -> Result<Option<Value>, FaultKind> {
if let Some(repeat) = other
.as_i64()
.and_then(|repeat| usize::try_from(repeat).ok())
{
if let Some(total_length) = self.len().checked_mul(repeat) {
let mut repeated = String::with_capacity(total_length);
for _ in 0..repeat {
repeated.push_str(self);
}
return Ok(Some(Value::dynamic(repeated)));
}
}
Ok(None)
}
fn hash<H>(&self, state: &mut H) -> bool
where
H: std::hash::Hasher,
{
Hash::hash(self, state);
true
}
}