Skip to main content

kcl_lib/std/
string.rs

1//! Standard library string operations.
2
3use crate::errors::KclError;
4use crate::execution::ExecState;
5use crate::execution::KclValue;
6use crate::execution::types::RuntimeType;
7use crate::std::Args;
8
9/// Convert all cased characters in a string to uppercase.
10pub async fn uppercase(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
11    let text: String = args.get_unlabeled_kw_arg("text", &RuntimeType::string(), exec_state)?;
12
13    Ok(KclValue::String {
14        value: text.to_uppercase(),
15        meta: args.into(),
16    })
17}
18
19/// Convert all cased characters in a string to lowercase.
20pub async fn lowercase(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
21    let text: String = args.get_unlabeled_kw_arg("text", &RuntimeType::string(), exec_state)?;
22
23    Ok(KclValue::String {
24        value: text.to_lowercase(),
25        meta: args.into(),
26    })
27}
28
29/// Compare two strings for equality.
30pub async fn is_equal(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
31    let text: String = args.get_unlabeled_kw_arg("text", &RuntimeType::string(), exec_state)?;
32    let to: String = args.get_kw_arg("to", &RuntimeType::string(), exec_state)?;
33    let case_insensitive = args
34        .get_kw_arg_opt("caseInsensitive", &RuntimeType::bool(), exec_state)?
35        .unwrap_or(false);
36
37    let value = if case_insensitive {
38        unicase::eq(&text, &to)
39    } else {
40        text == to
41    };
42
43    Ok(KclValue::Bool {
44        value,
45        meta: args.into(),
46    })
47}
48
49fn trim_whitespace(text: &str, at_start: bool, at_end: bool) -> &str {
50    match (at_start, at_end) {
51        (true, true) => text.trim(),
52        (true, false) => text.trim_start(),
53        (false, true) => text.trim_end(),
54        (false, false) => text,
55    }
56}
57
58/// Remove whitespace from the start and end of a string.
59pub async fn trim(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
60    let text: String = args.get_unlabeled_kw_arg("text", &RuntimeType::string(), exec_state)?;
61    let value = trim_whitespace(&text, true, true).to_owned();
62
63    Ok(KclValue::String {
64        value,
65        meta: args.into(),
66    })
67}
68
69/// Remove whitespace from the start of a string.
70pub async fn trim_start(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
71    let text: String = args.get_unlabeled_kw_arg("text", &RuntimeType::string(), exec_state)?;
72    let value = trim_whitespace(&text, true, false).to_owned();
73
74    Ok(KclValue::String {
75        value,
76        meta: args.into(),
77    })
78}
79
80/// Remove whitespace from the end of a string.
81pub async fn trim_end(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
82    let text: String = args.get_unlabeled_kw_arg("text", &RuntimeType::string(), exec_state)?;
83    let value = trim_whitespace(&text, false, true).to_owned();
84
85    Ok(KclValue::String {
86        value,
87        meta: args.into(),
88    })
89}