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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "gtmpl"
name = "gtmpl-moyan"
version = "0.7.1"
authors = ["Florian Dieminger <me@fiji-flo.de>"]
authors = ["Florian Dieminger <me@fiji-flo.de>", "moyan <moyan@moyanjdc.top>"]
description = "The Golang Templating Language for Rust"
license = "MIT"
repository = "https://github.com/fiji-flo/gtmpl-rust"
Expand Down
73 changes: 73 additions & 0 deletions src/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub static BUILTINS: &[(&str, Func)] = &[
("printf", printf as Func),
("index", index as Func),
("call", call as Func),
("slice", slice as Func),
];

macro_rules! val {
Expand Down Expand Up @@ -545,6 +546,78 @@ fn cmp(left: &Value, right: &Value) -> Option<Ordering> {
}
}

pub fn slice(args: &[Value]) -> Result<Value, FuncError> {
if args.is_empty() {
return Err(FuncError::ExactlyXArgs("slice".to_string(), 1));
}
match &args[0] {
Value::String(s) => {
let mut indices = Vec::new();
for arg in &args[1..] {
if let Value::Number(n) = arg {
if let Some(i) = n.as_i64() {
indices.push(i as usize);
} else {
return Err(FuncError::Generic("slice bounds out of range".to_string()));
}
} else {
return Err(FuncError::Generic(
"slice bounds must be numbers".to_string(),
));
}
}
let result = match indices.len() {
0 => s.clone(),
1 => s[indices[0]..].to_string(),
2 => s[indices[0]..indices[1]].to_string(),
3 => {
// Go 语法中的 x[1:2:3] 在 Rust 中不直接支持 capacity,
// 这里简化处理,只取 slice[1..2]
if indices[0] <= indices[1] && indices[1] <= s.len() {
s[indices[0]..indices[1]].to_string()
} else {
return Err(FuncError::Generic("slice bounds out of range".to_string()));
}
}
_ => return Err(FuncError::ExactlyXArgs("a".to_string(), 4)),
};
Ok(Value::String(result))
}
Value::Array(arr) => {
let mut indices = Vec::new();
for arg in &args[1..] {
if let Value::Number(n) = arg {
if let Some(i) = n.as_i64() {
indices.push(i as usize);
} else {
return Err(FuncError::Generic("slice bounds out of range".to_string()));
}
} else {
return Err(FuncError::Generic(
"slice bounds must be numbers".to_string(),
));
}
}
let result = match indices.len() {
0 => arr.clone(),
1 => arr[indices[0]..].to_vec(),
2 => arr[indices[0]..indices[1]].to_vec(),
3 => {
// 同样简化处理
if indices[0] <= indices[1] && indices[1] <= arr.len() {
arr[indices[0]..indices[1]].to_vec()
} else {
return Err(FuncError::Generic("slice bounds out of range".to_string()));
}
}
_ => return Err(FuncError::ExactlyXArgs("Doesn't engouh arg".to_string(), 4)),
};
Ok(Value::Array(result))
}
_ => Err(FuncError::Generic("slice".to_string())),
}
}

#[cfg(test)]
mod tests_mocked {
use super::*;
Expand Down
1 change: 0 additions & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,6 @@ node!(NumberNode {
});

impl NumberNode {
#[cfg_attr(feature = "cargo-clippy", allow(clippy::float_cmp))]
pub fn new(
tr: TreeId,
pos: Pos,
Expand Down
2 changes: 1 addition & 1 deletion tests/define.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use gtmpl::{Context, Template};
use gtmpl_derive::Gtmpl;
use gtmpl_moyan::{Context, Template};

#[test]
fn simple_define() {
Expand Down
4 changes: 2 additions & 2 deletions tests/niladic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::anyhow;
use gtmpl::{Func, FuncError, Value};
use gtmpl_derive::Gtmpl;
use gtmpl_moyan::{Func, FuncError, Value};

fn plus_one(args: &[Value]) -> Result<Value, FuncError> {
if let Value::Object(ref o) = &args[0] {
Expand All @@ -22,6 +22,6 @@ struct AddMe {
#[test]
fn simple_niladic_method() {
let add_me = AddMe { num: 42, plus_one };
let output = gtmpl::template("The answer is: {{ .plus_one }}", add_me);
let output = gtmpl_moyan::template("The answer is: {{ .plus_one }}", add_me);
assert_eq!(&output.unwrap(), "The answer is: 43");
}