Module
LinkThe module reads positional rows or associates data cells with unique header names. Positional parsing retains every column, including repeated text.
Why
LinkPositional rows preserve every column, while named rows require a unique name for each column.
Availability
LinkImport std.csv in language mode 5.21. The current compiler selects std/csv.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
Public API
LinkPending contracts
LinkRequire unique headers for named CSV rows
LinkRequire unique headers for named CSV rows
parseWithHeader returns Result<Array<Map<string, string>>, string>. A repeated header name returns Err instead of choosing a first or last column.
Header uniqueness is checked before converting data rows. Repeated names are rejected when cell values are equal and when the input contains a header without data rows.
The header names are the parsed CSV field values. Equality uses those string values without an additional trimming or case-folding step.
Use csv.parse when columns must remain positional. Positional parsing preserves every field and does not assign header names.
Why
A named row gives each name one column. Rejecting duplicates keeps column order from selecting a winner, while positional parsing retains data that has no unique names.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 830 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 841 - compiler/crates/topaz_value/src/value/collections.rs at
271329b482bc· 386 -
topaz.stdlib.contract.csv-unique-headersatworking-tree - topaz.decision.csv-unique-headers.1 at
topaz.decisi
Read unique named columns
Linkimport std.csvimport std.testimport std.iolet rows = test.assertOk(csv.parseWithHeader("name,count\nTopaz,6"))let row = test.assertSome(rows.get(0))io.writeLine(test.assertSome(row.get("name")))io.writeLine(test.assertSome(row.get("count")))
Topaz 6
Expected result
Expected result
Execution completes. Exit code 0.
Topaz6
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 830 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 841 - compiler/crates/topaz_value/src/value/collections.rs at
271329b482bc· 386 -
topaz.stdlib.contract.csv-unique-headersatworking-tree - topaz.decision.csv-unique-headers.1 at
topaz.decisi
Reject duplicate names
Linkimport std.csvimport std.iomatch (csv.parseWithHeader("name,name\nleft,right")) {case Err(_) => io.writeLine("duplicate headers rejected")case Ok(_) => io.writeLine("accepted")}
duplicate headers rejected
Expected result
Expected result
Execution completes. Exit code 0.
duplicate headers rejected
The parser rejects this input by returning Err. The program handles that result and exits successfully.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 830 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 841 - compiler/crates/topaz_value/src/value/collections.rs at
271329b482bc· 386 -
topaz.stdlib.contract.csv-unique-headersatworking-tree - topaz.decision.csv-unique-headers.1 at
topaz.decisi
Reject duplicate names after reversing values
Linkimport std.csvimport std.iomatch (csv.parseWithHeader("name,name\nright,left")) {case Err(_) => io.writeLine("duplicate headers rejected")case Ok(_) => io.writeLine("accepted")}
duplicate headers rejected
Expected result
Expected result
Execution completes. Exit code 0.
duplicate headers rejected
The parser rejects this input by returning Err. The program handles that result and exits successfully.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 830 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 841 - compiler/crates/topaz_value/src/value/collections.rs at
271329b482bc· 386 -
topaz.stdlib.contract.csv-unique-headersatworking-tree - topaz.decision.csv-unique-headers.1 at
topaz.decisi
Reject duplicate names even when the values agree
Linkimport std.csvimport std.iomatch (csv.parseWithHeader("name,name\nsame,same")) {case Err(_) => io.writeLine("duplicate headers rejected")case Ok(_) => io.writeLine("accepted")}
duplicate headers rejected
Expected result
Expected result
Execution completes. Exit code 0.
duplicate headers rejected
The parser rejects this input by returning Err. The program handles that result and exits successfully.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 830 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 841 - compiler/crates/topaz_value/src/value/collections.rs at
271329b482bc· 386 -
topaz.stdlib.contract.csv-unique-headersatworking-tree - topaz.decision.csv-unique-headers.1 at
topaz.decisi
Reject duplicate names before reading data rows
Linkimport std.csvimport std.iomatch (csv.parseWithHeader("name,name")) {case Err(_) => io.writeLine("duplicate headers rejected")case Ok(_) => io.writeLine("accepted")}
duplicate headers rejected
Expected result
Expected result
Execution completes. Exit code 0.
duplicate headers rejected
The parser rejects this input by returning Err. The program handles that result and exits successfully.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 830 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 841 - compiler/crates/topaz_value/src/value/collections.rs at
271329b482bc· 386 -
topaz.stdlib.contract.csv-unique-headersatworking-tree - topaz.decision.csv-unique-headers.1 at
topaz.decisi
Preserve repeated header text in positional rows
Linkimport std.csvimport std.testimport std.iolet rows = test.assertOk(csv.parse("name,name\nleft,right"))let row = test.assertSome(rows.get(1))io.writeLine("{row[0]}:{row[1]}")
left:right
Expected result
Expected result
Execution completes. Exit code 0.
left:right
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 830 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
271329b482bc· 841 - compiler/crates/topaz_value/src/value/collections.rs at
271329b482bc· 386 -
topaz.stdlib.contract.csv-unique-headersatworking-tree - topaz.decision.csv-unique-headers.1 at
topaz.decisi
Serialize positional and named CSV rows
LinkSerialize positional and named CSV rows
stringify accepts arrays of string rows and returns text. It preserves row order and cell text. Rows may have different field counts. Commas separate fields and every row ends with LF, including the last row. An empty array of rows becomes empty text.
A field containing a comma, quotation mark, CR or LF is enclosed in quotation marks. An embedded quotation mark is doubled. Embedded CR and LF remain inside the quoted field. A sole empty field is quoted to distinguish it from a row with no fields. Empty fields in a larger row need no quotation marks. Other whitespace and Unicode text remain unchanged.
stringify writes a header using the supplied column names and then projects every map onto that ordered list. An absent key supplies an empty cell. Keys outside the list are ignored. A header is emitted even when there are no data rows.
Column names use exact string equality. A repeated name, including a repeated empty name, returns a nonempty Err identifying that name and its zero based repeated index. Validation happens before row projection. A unique empty name is permitted. An empty column list writes an empty header row and one empty row for each input row.
Positional serialization has no value error for a well typed input. Named serialization uses Result for invalid columns. Incorrect row or cell types follow ordinary checking diagnostics. Both functions evaluate supplied arguments once from left to right and perform no I/O or mutation. Their collection arguments follow value semantics.
Named parsing and named writing require the same unique header names. Positional serialization remains available when a row has no naming schema. CSV parsing has its separate malformed input and field count rules.
Why
Explicit columns determine stable output without depending on map insertion order. Unique names prevent two fields from competing for one key. A Result reports invalid naming data through the same recoverable channel as named parsing.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 793 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 810 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 867 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 134 -
topaz.stdlib.contract.csv-stringifyatworking-tree - topaz.decision.csv-header-result.1 at
topaz.decisi
Quote delimiters and line breaks and finish each record
Linkimport std.csvimport std.jsonimport std.testimport std.iolet rows = [["name", "note"], ["a,b", "x\"y"], ["line\nnext", "carriage\rreturn"]]io.writeLine(test.assertOk(json.stringify(csv.stringify(rows))))
"name,note\n\"a,b\",\"x\"\"y\"\n\"line\nnext\",\"carriage\rreturn\"\n"
Expected result
Expected result
Execution completes. Exit code 0.
"name,note\n\"a,b\",\"x\"\"y\"\n\"line\nnext\",\"carriage\rreturn\"\n"
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 793 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 810 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 867 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 134 -
topaz.stdlib.contract.csv-stringifyatworking-tree - topaz.decision.csv-header-result.1 at
topaz.decisi
Distinguish no rows, no fields and one empty field
Linkimport std.csvimport std.jsonimport std.testimport std.iolet none: Array<Array<string>> = []let emptyRows: Array<Array<string>> = [[], [""]]io.writeLine(test.assertOk(json.stringify(csv.stringify(none))))io.writeLine(test.assertOk(json.stringify(csv.stringify(emptyRows))))
"" "\n\"\"\n"
Expected result
Expected result
Execution completes. Exit code 0.
"""\n\"\"\n"
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 793 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 810 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 867 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 134 -
topaz.stdlib.contract.csv-stringifyatworking-tree - topaz.decision.csv-header-result.1 at
topaz.decisi
Choose output columns explicitly and fill absent cells
Linkimport std.csvimport std.jsonimport std.testimport std.iolet rows = [map { "name": "Topaz", "extra": "ignored", "count": "6" }, map { "name": "Ink" }]let text = test.assertOk(csv.stringifyWithHeader(rows, ["count", "name"]))io.writeLine(test.assertOk(json.stringify(text)))let noRows: Array<Map<string, string>> = []io.writeLine(test.assertOk(json.stringify(test.assertOk(csv.stringifyWithHeader(noRows, ["name"])))))
"count,name\n6,Topaz\n,Ink\n" "name\n"
Expected result
Expected result
Execution completes. Exit code 0.
"count,name\n6,Topaz\n,Ink\n""name\n"
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 793 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 810 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 867 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 134 -
topaz.stdlib.contract.csv-stringifyatworking-tree - topaz.decision.csv-header-result.1 at
topaz.decisi
Reject duplicate columns before serializing a row
Linkimport std.csvimport std.iolet rows: Array<Map<string, string>> = []match (csv.stringifyWithHeader(rows, ["name", "name"])) {case Ok(_) => io.writeLine("accepted")case Err(_) => io.writeLine("duplicate columns rejected")}
duplicate columns rejected
Expected result
Expected result
Execution completes. Exit code 0.
duplicate columns rejected
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 793 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 810 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 867 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 134 -
topaz.stdlib.contract.csv-stringifyatworking-tree - topaz.decision.csv-header-result.1 at
topaz.decisi