Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/auto-ready-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr merge "${{ github.event.pull_request.html_url }}" --auto --merge
gh pr merge "${{ github.event.pull_request.html_url }}" --auto --merge || true
2 changes: 2 additions & 0 deletions plan_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Check that the changes were correctly applied by the previous write_file operation
git diff stdlib/src/strings.rs
186 changes: 119 additions & 67 deletions stdlib/src/strings.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,115 @@
use crate::{StdFunction, StdlibModule, StdlibRegistry};
use std::collections::HashMap;
use std::rc::Rc;
use techscript_runtime::{error::RuntimeError, error::RuntimeErrorKind, value::RuntimeValue};
use techscript_runtime::{
error::RuntimeError, error::RuntimeErrorKind, value::RuntimeValue, RuntimeContext,
};

fn string_trim(
_ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
let s = args[0].try_into_string()?;
Ok(RuntimeValue::Str(s.trim().to_string()))
}

fn string_replace(
_ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
let source = args[0].try_into_string()?;
let from = args[1].try_into_string()?;
let to = args[2].try_into_string()?;
Ok(RuntimeValue::Str(source.replace(&from, &to)))
}

fn string_split(
_ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
let s = args[0].try_into_string()?;
let pat = args[1].try_into_string()?;
let parts: Vec<RuntimeValue> = s
.split(&pat)
.map(|p| RuntimeValue::Str(p.to_string()))
.collect();
Ok(RuntimeValue::List {
items: Rc::new(std::cell::RefCell::new(parts)),
is_const: false,
})
}

fn string_join(
_ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
let sep = args[1].try_into_string()?;
if let RuntimeValue::List { items, .. } = &args[0] {
let mut str_parts = Vec::new();
for item in items.borrow().iter() {
str_parts.push(item.try_into_string()?);
}
Ok(RuntimeValue::Str(str_parts.join(&sep)))
} else {
Err(RuntimeError::new(
RuntimeErrorKind::TypeMismatch {
expected: "List".to_string(),
found: args[0].runtime_type().to_string(),
},
None,
None,
))
}
}

fn string_to_lower(
_ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
let s = args[0].try_into_string()?;
Ok(RuntimeValue::Str(s.to_lowercase()))
}

fn string_to_upper(
_ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
let s = args[0].try_into_string()?;
Ok(RuntimeValue::Str(s.to_uppercase()))
}

fn string_contains(
_ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
let s = args[0].try_into_string()?;
let sub = args[1].try_into_string()?;
Ok(RuntimeValue::Bool(s.contains(&sub)))
}

fn string_from_int(
_ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
let val = args[0].try_into_int()?;
Ok(RuntimeValue::Str(val.to_string()))
}

fn string_from_float(
_ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
let val = args[0].try_into_float()?;
Ok(RuntimeValue::Str(val.to_string()))
}

fn string_from_bool(
_ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
let val = args[0].try_into_bool()?;
Ok(RuntimeValue::Str(val.to_string()))
}

impl StdlibRegistry {
pub fn register_strings(&mut self) {
Expand All @@ -13,10 +121,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "trim".to_string(),
arity: 1,
callback: |_ctx, args| {
let s = args[0].try_into_string()?;
Ok(RuntimeValue::Str(s.trim().to_string()))
},
callback: string_trim,
}),
);

Expand All @@ -25,12 +130,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "replace".to_string(),
arity: 3,
callback: |_ctx, args| {
let source = args[0].try_into_string()?;
let from = args[1].try_into_string()?;
let to = args[2].try_into_string()?;
Ok(RuntimeValue::Str(source.replace(&from, &to)))
},
callback: string_replace,
}),
);

Expand All @@ -39,18 +139,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "split".to_string(),
arity: 2,
callback: |_ctx, args| {
let s = args[0].try_into_string()?;
let pat = args[1].try_into_string()?;
let parts: Vec<RuntimeValue> = s
.split(&pat)
.map(|p| RuntimeValue::Str(p.to_string()))
.collect();
Ok(RuntimeValue::List {
items: Rc::new(std::cell::RefCell::new(parts)),
is_const: false,
})
},
callback: string_split,
}),
);

Expand All @@ -59,25 +148,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "join".to_string(),
arity: 2,
callback: |_ctx, args| {
let sep = args[1].try_into_string()?;
if let RuntimeValue::List { items, .. } = &args[0] {
let mut str_parts = Vec::new();
for item in items.borrow().iter() {
str_parts.push(item.try_into_string()?);
}
Ok(RuntimeValue::Str(str_parts.join(&sep)))
} else {
Err(RuntimeError::new(
RuntimeErrorKind::TypeMismatch {
expected: "List".to_string(),
found: args[0].runtime_type().to_string(),
},
None,
None,
))
}
},
callback: string_join,
}),
);

Expand All @@ -86,10 +157,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "to_lower".to_string(),
arity: 1,
callback: |_ctx, args| {
let s = args[0].try_into_string()?;
Ok(RuntimeValue::Str(s.to_lowercase()))
},
callback: string_to_lower,
}),
);

Expand All @@ -98,10 +166,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "to_upper".to_string(),
arity: 1,
callback: |_ctx, args| {
let s = args[0].try_into_string()?;
Ok(RuntimeValue::Str(s.to_uppercase()))
},
callback: string_to_upper,
}),
);

Expand All @@ -110,11 +175,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "contains".to_string(),
arity: 2,
callback: |_ctx, args| {
let s = args[0].try_into_string()?;
let sub = args[1].try_into_string()?;
Ok(RuntimeValue::Bool(s.contains(&sub)))
},
callback: string_contains,
}),
);

Expand All @@ -123,10 +184,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "from_int".to_string(),
arity: 1,
callback: |_ctx, args| {
let val = args[0].try_into_int()?;
Ok(RuntimeValue::Str(val.to_string()))
},
callback: string_from_int,
}),
);

Expand All @@ -135,10 +193,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "from_float".to_string(),
arity: 1,
callback: |_ctx, args| {
let val = args[0].try_into_float()?;
Ok(RuntimeValue::Str(val.to_string()))
},
callback: string_from_float,
}),
);

Expand All @@ -147,10 +202,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "from_bool".to_string(),
arity: 1,
callback: |_ctx, args| {
let val = args[0].try_into_bool()?;
Ok(RuntimeValue::Str(val.to_string()))
},
callback: string_from_bool,
}),
);

Expand Down
8 changes: 6 additions & 2 deletions stdlib/tests/stdlib_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,9 @@ fn test_http_module() {
&mut ctx_unprivileged,
vec![RuntimeValue::Str(format!("http://127.0.0.1:{}", port))],
);
assert!(matches!(res_get, Err(techscript_runtime::RuntimeError { kind: techscript_runtime::RuntimeErrorKind::InvalidOperation(msg), .. }) if msg.contains("Security policy violation")));
assert!(
matches!(res_get, Err(techscript_runtime::RuntimeError { kind: techscript_runtime::RuntimeErrorKind::InvalidOperation(msg), .. }) if msg.contains("Security policy violation"))
);

let post = http.exports.get("post").unwrap();
let res_post = post.call(
Expand All @@ -550,7 +552,9 @@ fn test_http_module() {
RuntimeValue::Str("body".to_string()),
],
);
assert!(matches!(res_post, Err(techscript_runtime::RuntimeError { kind: techscript_runtime::RuntimeErrorKind::InvalidOperation(msg), .. }) if msg.contains("Security policy violation")));
assert!(
matches!(res_post, Err(techscript_runtime::RuntimeError { kind: techscript_runtime::RuntimeErrorKind::InvalidOperation(msg), .. }) if msg.contains("Security policy violation"))
);

// Test with Network capability
let mut caps = HashSet::new();
Expand Down
Loading