1use crate::compiler::prelude::*;
2
3fn to_string(value: Value) -> Resolved {
4 use Value::{Boolean, Bytes, Float, Integer, Null, Timestamp};
5 use chrono::SecondsFormat;
6 let value = match value {
7 v @ Bytes(_) => v,
8 Integer(v) => v.to_string().into(),
9 Float(v) => v.to_string().into(),
10 Boolean(v) => v.to_string().into(),
11 Timestamp(v) => v.to_rfc3339_opts(SecondsFormat::AutoSi, true).into(),
12 Null => "".into(),
13 v => return Err(format!("unable to coerce {} into string", v.kind()).into()),
14 };
15 Ok(value)
16}
17
18#[derive(Clone, Copy, Debug)]
19pub struct ToString;
20
21impl Function for ToString {
22 fn identifier(&self) -> &'static str {
23 "to_string"
24 }
25
26 fn usage(&self) -> &'static str {
27 "Coerces the `value` into a string."
28 }
29
30 fn category(&self) -> &'static str {
31 Category::Coerce.as_ref()
32 }
33
34 fn internal_failure_reasons(&self) -> &'static [&'static str] {
35 &["`value` is not an integer, float, boolean, string, timestamp, or null."]
36 }
37
38 fn return_kind(&self) -> u16 {
39 kind::BYTES
40 }
41
42 fn return_rules(&self) -> &'static [&'static str] {
43 &[
44 "If `value` is an integer or float, returns the string representation.",
45 "If `value` is a boolean, returns `\"true\"` or `\"false\"`.",
46 "If `value` is a timestamp, returns an [RFC 3339](\\(urls.rfc3339)) representation.",
47 "If `value` is a null, returns `\"\"`.",
48 ]
49 }
50
51 fn parameters(&self) -> &'static [Parameter] {
52 const PARAMETERS: &[Parameter] = &[Parameter::required(
53 "value",
54 kind::ANY,
55 "The value to convert to a string.",
56 )];
57 PARAMETERS
58 }
59
60 fn examples(&self) -> &'static [Example] {
61 &[
62 example! {
63 title: "Coerce to a string (Boolean)",
64 source: "to_string(true)",
65 result: Ok("s'true'"),
66 },
67 example! {
68 title: "Coerce to a string (int)",
69 source: "to_string(52)",
70 result: Ok("s'52'"),
71 },
72 example! {
73 title: "Coerce to a string (float)",
74 source: "to_string(52.2)",
75 result: Ok("s'52.2'"),
76 },
77 example! {
78 title: "String",
79 source: "to_string(s'foo')",
80 result: Ok("foo"),
81 },
82 example! {
83 title: "False",
84 source: "to_string(false)",
85 result: Ok("s'false'"),
86 },
87 example! {
88 title: "Null",
89 source: "to_string(null)",
90 result: Ok(""),
91 },
92 example! {
93 title: "Timestamp",
94 source: "to_string(t'2020-01-01T00:00:00Z')",
95 result: Ok("2020-01-01T00:00:00Z"),
96 },
97 example! {
98 title: "Array",
99 source: "to_string!([])",
100 result: Err(
101 r#"function call error for "to_string" at (0:14): unable to coerce array into string"#,
102 ),
103 },
104 example! {
105 title: "Object",
106 source: "to_string!({})",
107 result: Err(
108 r#"function call error for "to_string" at (0:14): unable to coerce object into string"#,
109 ),
110 },
111 example! {
112 title: "Regex",
113 source: "to_string!(r'foo')",
114 result: Err(
115 r#"function call error for "to_string" at (0:18): unable to coerce regex into string"#,
116 ),
117 },
118 ]
119 }
120
121 fn compile(
122 &self,
123 _state: &state::TypeState,
124 _ctx: &mut FunctionCompileContext,
125 arguments: ArgumentList,
126 ) -> Compiled {
127 let value = arguments.required("value");
128
129 Ok(ToStringFn { value }.as_expr())
130 }
131}
132
133#[derive(Debug, Clone)]
134struct ToStringFn {
135 value: Box<dyn Expression>,
136}
137
138impl FunctionExpression for ToStringFn {
139 fn resolve(&self, ctx: &mut Context) -> Resolved {
140 let value = self.value.resolve(ctx)?;
141
142 to_string(value)
143 }
144
145 fn type_def(&self, state: &state::TypeState) -> TypeDef {
146 let td = self.value.type_def(state);
147
148 TypeDef::bytes()
149 .maybe_fallible(td.contains_array() || td.contains_object() || td.contains_regex())
150 }
151}
152
153#[cfg(test)]
154mod tests {
155 use super::*;
156
157 test_function![
158 to_string => ToString;
159
160 integer {
161 args: func_args![value: 20],
162 want: Ok("20"),
163 tdef: TypeDef::bytes(),
164 }
165
166 float {
167 args: func_args![value: 20.5],
168 want: Ok("20.5"),
169 tdef: TypeDef::bytes(),
170 }
171 ];
172}