Skip to content
Merged
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
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [main]

env:
RUST_TOOLCHAIN: "1.89.0"
RUST_TOOLCHAIN: "1.97.0"

jobs:
test:
Expand All @@ -27,6 +27,8 @@ jobs:
key: ${{ runner.os }}-almide-${{ env.RUST_TOOLCHAIN }}-${{ hashFiles('almide.lock') }}
restore-keys: ${{ runner.os }}-almide-${{ env.RUST_TOOLCHAIN }}-
- name: Install Almide
run: cargo install --git https://github.com/almide/almide.git --branch develop
run: |
curl -fsSL "https://github.com/almide/almide/releases/download/v0.69.0/almide-linux-x86_64.tar.gz" -o almide.tar.gz
tar xzf almide.tar.gz --strip-components=1 && sudo mv almide /usr/local/bin/ && almide --version
- name: Run tests
run: almide test
14 changes: 7 additions & 7 deletions spec/csv_test.almd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import self as csv
import json


fn unwrap(r: Result[Value, String]) -> Value = match r {
ok(v) => v,
Expand Down Expand Up @@ -105,23 +105,23 @@ test "parse_records empty input" {
// ═══════════════════════════════════════════════════════
test "stringify simple" {
let v = value.array([
value.array([value.str("a"), value.str("b")]),
value.array([value.str("1"), value.str("2")])
value.array([value.string("a"), value.string("b")]),
value.array([value.string("1"), value.string("2")])
]);
assert_eq(csv.stringify(v), "a,b\n1,2")
}

test "stringify escapes comma" {
let v = value.array([
value.array([value.str("hello, world"), value.str("ok")])
value.array([value.string("hello, world"), value.string("ok")])
]);
let result = csv.stringify(v);
assert(string.contains(result, '"hello, world"'))
}

test "stringify escapes quotes" {
let v = value.array([
value.array([value.str('say "hi"'), value.str("ok")])
value.array([value.string('say "hi"'), value.string("ok")])
]);
let result = csv.stringify(v);
assert(string.contains(result, '"say ""hi"""'))
Expand All @@ -132,8 +132,8 @@ test "stringify escapes quotes" {
// ═══════════════════════════════════════════════════════
test "stringify_records" {
let v = value.array([
value.object([("name", value.str("Alice")), ("age", value.str("30"))]),
value.object([("name", value.str("Bob")), ("age", value.str("25"))])
value.object([("name", value.string("Alice")), ("age", value.string("30"))]),
value.object([("name", value.string("Bob")), ("age", value.string("25"))])
]);
let result = csv.stringify_records(v);
assert(string.contains(result, "name,age"));
Expand Down
8 changes: 4 additions & 4 deletions src/mod.almd
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import json

effect fn parse(text: String) -> Result[Value, String] = {
let rows = parse_rows(text)
ok(value.array(rows |> list.map((row) => value.array(row |> list.map((cell) => value.str(cell))))))
ok(value.array(rows |> list.map((row) => value.array(row |> list.map((cell) => value.string(cell))))))
}

fn stringify(v: Value) -> String = {
let rows = value.as_array(v) ?? []
rows
|> list.map((row) => {
let cells = value.as_array(row) ?? []
cells |> list.map((cell) => escape_cell(value.as_string(cell) ?? value.stringify(cell))) |> list.join(",")
cells |> list.map((cell) => escape_cell(value.as_string(cell) ?? json.stringify(cell))) |> list.join(",")
})
|> list.join("\n")
}
Expand All @@ -36,7 +36,7 @@ effect fn parse_records(text: String) -> Result[Value, String] = {
|> list.enumerate
|> list.map((entry) => {
let (i, key) = entry
(key, value.str(list.get_or(row, i, "")))
(key, value.string((list.get(row, i) ?? "")))
})
value.object(pairs)
})
Expand All @@ -48,7 +48,7 @@ fn stringify_records(v: Value) -> String = {
if list.is_empty(rows) then ""
else {
let first = list.get(rows, 0) ?? value.object([])
let header = json.keys(first)
let header = list.map(value.as_object(first) ?? [], (kv) => kv.0)
let header_line = header |> list.map((h) => escape_cell(h)) |> list.join(",")
let data_lines = rows |> list.map((row) => {
header |> list.map((h) => escape_cell(value.as_string(value.get(row, h) ?? value.null()) ?? "")) |> list.join(",")
Expand Down
Loading