libcsv
| Kind | ffi-c |
|---|---|
| Capabilities | ffi |
| Categories | data-processing ffi |
| Keywords | csv libcsv data parsing tabular |
CSV parsing and generation library for Kit using libcsv
Files
| File | Description |
|---|---|
.editorconfig | Editor formatting configuration |
.gitignore | Git ignore rules for build artifacts and dependencies |
.tool-versions | asdf tool versions (Zig, Kit) |
LICENSE | MIT license file |
README.md | This file |
c/kit_csv.c | C FFI wrapper |
c/kit_csv.h | C header for FFI wrapper |
examples/basic.kit | Basic usage example |
kit.toml | Package manifest with metadata and dependencies |
src/libcsv.kit | Kit LibCSV - CSV parsing and generation library |
tests/libcsv.test.kit | Tests for libcsv |
Dependencies
No Kit package dependencies.
Installation
kit add gitlab.com/kit-lang/packages/kit-libcsv.gitSystem Requirements
| Platform | Command |
|---|---|
| macOS | brew install libcsv |
| Ubuntu | sudo apt install libcsv-dev |
| Fedora | sudo dnf install libcsv-devel |
Usage
import Kit.LibCSV as LibCSV
main = fn =>
# Parse CSV string into rows
csv-text = "Alice,30,Engineer\nBob,25,Designer\nCarol,35,Manager"
match LibCSV.parse csv-text
| Ok rows ->
println ("Parsed " ++ (show (List.length rows)) ++ " rows:")
List.map (fn(row) => println (" " ++ (show row))) rows
no-op
| Err e -> println ("Parse error: " ++ e)
# Parse with header row
csv-with-header = "name,age,role\nAlice,30,Engineer\nBob,25,Designer"
match LibCSV.parse-with-header csv-with-header
| Ok parsed ->
println ("Headers: " ++ (show parsed.headers))
println ("Data rows: " ++ (show (List.length parsed.rows)))
| Err e -> println ("Parse error: " ++ e)
# Generate CSV from rows
rows = [["Alice", "30", "Engineer"], ["Bob", "25", "Designer"]]
csv = LibCSV.generate rows
println ("Generated:\n" ++ csv)
# Generate with headers
headers = ["name", "age", "role"]
csv-out = LibCSV.generate-with-header headers rows
println ("With headers:\n" ++ csv-out)
mainDevelopment
Running Examples
Run examples with the interpreter:
kit run examples/basic.kitCompile examples to a native binary:
kit build examples/basic.kit && ./basicRunning Tests
Run the test suite:
kit testRun the test suite with coverage:
kit test --coverageRunning kit dev
Run the standard development workflow (format, check, test):
kit devThis will:
- Format and check source files in
src/ - Run tests in
tests/with coverage
Generating Documentation
Generate API documentation from doc comments:
kit docNote: Kit sources with doc comments (##) will generate HTML documents in docs/*.html
Cleaning Build Artifacts
Remove generated files, caches, and build artifacts:
kit task cleanNote: Defined in kit.toml.
Local Installation
To install this package locally for development:
kit installThis installs the package to ~/.kit/packages/@kit/libcsv/, making it available for import as Kit.LibCSV in other projects.
License
This package is released under the MIT License - see LICENSE for details.
libcsv is released under the LGPL 2.1 License.
Exported Functions & Types
parse
Parse a CSV string into a list of rows, where each row is a list of fields.
Handles RFC 4180 compliant CSV including quoted fields, embedded commas, embedded newlines, and escaped double quotes.
Parameters:
Returns:
String -> Result [[String]] String
csv = "Alice,30\nBob,25"
match parse csv
| Ok rows ->
List.map (fn(row) => println (show row)) rows
no-op
| Err e -> println "Parse error: ${e}"parse-with-header
Parse a CSV string with the first row as column headers.
Returns a record with headers (list of header names) and rows (list of data rows, each a list of fields).
Parameters:
Returns: Ok with headers and data rows, or Err with message
String -> Result {headers: [String], rows: [[String]]} String
csv = "name,age\nAlice,30\nBob,25"
match parse-with-header csv
| Ok result ->
println ("Headers: " ++ (show result.headers))
println ("Data rows: " ++ (show (List.length result.rows)))
| Err e -> println "Parse error: ${e}"generate
Generate a CSV string from a list of rows.
Each row is a list of field strings. Fields containing commas, double quotes, or newlines are automatically quoted.
Parameters:
Returns:
[[String]] -> String
rows = [["Alice", "30"], ["Bob", "25"]]
csv = generate rows
println csv # Alice,30\r\nBob,25generate-with-header
Generate a CSV string with a header row.
Parameters:
Returns:
[String] -> [[String]] -> String
headers = ["name", "age"]
rows = [["Alice", "30"], ["Bob", "25"]]
csv = generate-with-header headers rows
println csv # name,age\r\nAlice,30\r\nBob,25