libcsv

CSV parsing and generation library for Kit using libcsv

Files

FileDescription
.editorconfigEditor formatting configuration
.gitignoreGit ignore rules for build artifacts and dependencies
.tool-versionsasdf tool versions (Zig, Kit)
LICENSEMIT license file
README.mdThis file
c/kit_csv.cC FFI wrapper
c/kit_csv.hC header for FFI wrapper
examples/basic.kitBasic usage example
kit.tomlPackage manifest with metadata and dependencies
src/libcsv.kitKit LibCSV - CSV parsing and generation library
tests/libcsv.test.kitTests for libcsv

Dependencies

No Kit package dependencies.

Installation

kit add gitlab.com/kit-lang/packages/kit-libcsv.git

System Requirements

PlatformCommand
macOSbrew install libcsv
Ubuntusudo apt install libcsv-dev
Fedorasudo 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)

main

Development

Running Examples

Run examples with the interpreter:

kit run examples/basic.kit

Compile examples to a native binary:

kit build examples/basic.kit && ./basic

Running Tests

Run the test suite:

kit test

Run the test suite with coverage:

kit test --coverage

Running kit dev

Run the standard development workflow (format, check, test):

kit dev

This will:

  1. Format and check source files in src/
  2. Run tests in tests/ with coverage

Generating Documentation

Generate API documentation from doc comments:

kit doc

Note: Kit sources with doc comments (##) will generate HTML documents in docs/*.html

Cleaning Build Artifacts

Remove generated files, caches, and build artifacts:

kit task clean

Note: Defined in kit.toml.

Local Installation

To install this package locally for development:

kit install

This 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,25

generate-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