diff --git a/stdlib/src/csv.rs b/stdlib/src/csv.rs index 33b6e035..b6f6d7d4 100644 --- a/stdlib/src/csv.rs +++ b/stdlib/src/csv.rs @@ -9,58 +9,9 @@ impl StdlibRegistry { let mut exports: HashMap> = 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 = 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 = 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", @@ -74,6 +25,57 @@ impl StdlibRegistry { } } +fn csv_parse_fn() -> Rc { + 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 = 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 { + 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 = 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::*; diff --git a/stdlib/tests/stdlib_tests.rs b/stdlib/tests/stdlib_tests.rs index 05c13a8c..51fdb1dc 100644 --- a/stdlib/tests/stdlib_tests.rs +++ b/stdlib/tests/stdlib_tests.rs @@ -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); } });