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
104 changes: 53 additions & 51 deletions stdlib/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,9 @@ impl StdlibRegistry {
let mut exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
HashMap::new();

exports.insert(
"parse".to_string(),
Rc::new(StdFunction {
name: "parse".to_string(),
arity: 1,
callback: |_ctx, args| {
let csv = args[0].try_into_string()?;
let mut list = Vec::new();
for line in csv.lines() {
let parts: Vec<RuntimeValue> = line
.split(',')
.map(|s| RuntimeValue::Str(s.to_string()))
.collect();
list.push(RuntimeValue::List {
items: Rc::new(RefCell::new(parts)),
is_const: false,
});
}
Ok(RuntimeValue::List {
items: Rc::new(RefCell::new(list)),
is_const: false,
})
},
}),
);
exports.insert("parse".to_string(), csv_parse_fn());

exports.insert(
"stringify".to_string(),
Rc::new(StdFunction {
name: "stringify".to_string(),
arity: 1,
callback: |_ctx, args| {
let mut lines = Vec::new();
if let RuntimeValue::List { items, .. } = &args[0] {
for row in items.borrow().iter() {
if let RuntimeValue::List {
items: row_items, ..
} = row
{
let parts: Vec<String> = row_items
.borrow()
.iter()
.map(|item| item.try_into_string().unwrap_or_default())
.collect();
lines.push(parts.join(","));
}
}
}
Ok(RuntimeValue::Str(lines.join("\n")))
},
}),
);
exports.insert("stringify".to_string(), csv_stringify_fn());

self.register_module(
"std.csv",
Expand All @@ -74,6 +25,57 @@ impl StdlibRegistry {
}
}

fn csv_parse_fn() -> Rc<dyn techscript_runtime::function::Callable> {
Rc::new(StdFunction {
name: "parse".to_string(),
arity: 1,
callback: |_ctx, args| {
let csv = args[0].try_into_string()?;
let mut list = Vec::new();
for line in csv.lines() {
let parts: Vec<RuntimeValue> = line
.split(',')
.map(|s| RuntimeValue::Str(s.to_string()))
.collect();
list.push(RuntimeValue::List {
items: Rc::new(RefCell::new(parts)),
is_const: false,
});
}
Ok(RuntimeValue::List {
items: Rc::new(RefCell::new(list)),
is_const: false,
})
},
})
}

fn csv_stringify_fn() -> Rc<dyn techscript_runtime::function::Callable> {
Rc::new(StdFunction {
name: "stringify".to_string(),
arity: 1,
callback: |_ctx, args| {
let mut lines = Vec::new();
if let RuntimeValue::List { items, .. } = &args[0] {
for row in items.borrow().iter() {
if let RuntimeValue::List {
items: row_items, ..
} = row
{
let parts: Vec<String> = row_items
.borrow()
.iter()
.map(|item| item.try_into_string().unwrap_or_default())
.collect();
lines.push(parts.join(","));
}
}
}
Ok(RuntimeValue::Str(lines.join("\n")))
},
})
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
1 change: 1 addition & 0 deletions stdlib/tests/stdlib_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ fn test_http_module() {
let response = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\nHello Server!";
let _ = stream.write_all(response.as_bytes());
let _ = stream.flush();
let _ = stream.shutdown(std::net::Shutdown::Both);
}
});

Expand Down
Loading