Skip to content

A pure Vala TOML parser library adhering to TOML v1.0.0 specification, with async parsing, file watching, and JSON serialization support.

License

Notifications You must be signed in to change notification settings

dardevelin/toml-vala

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

toml-vala

A pure-Vala implementation of a TOML v1.0.0 parser, designed to be lightweight, fast, and easy to integrate into Vala projects. This library avoids external C dependencies, making it platform-independent and suitable for cross-compilation.

Features

  • Full TOML v1.0.0 Support: Parses all TOML constructs including tables, arrays, inline tables, array-of-tables, strings (basic and literal, single-line and multi-line), numbers (integers and floats), booleans, and datetimes.
  • Tree-Based Data Model: Uses a TomlValue tree structure for easy navigation and access.
  • Serialization: Supports serialization back to TOML format and to JSON.
  • Asynchronous Parsing: Includes async file parsing APIs using GLib/GIO.
  • File Watching: Provides a TomlWatcher class for hot-reloading on file changes.
  • Error Handling: Comprehensive error reporting with TomlError domain.
  • Multi-Encoding Support: Reads TOML files in ASCII, UTF-8, UTF-16, and UTF-32 encodings with automatic BOM detection.
  • Validation: Optional validation tool that checks TOML files against the specification, reports errors, and suggests fixes (not linked by default).
  • Dependencies: Only requires GLib, Gee, and json-glib (for JSON serialization).

Installation

Building from Source

  1. Ensure you have Vala and GLib development packages installed.
  2. Clone the repository:
    git clone https://github.com/dardevelin/toml-vala.git
    cd toml-vala
    
  3. Build the library:
    make
    
    This will compile the Vala source into a shared library (libtoml-vala.so) and generate a VAPI file for integration.

Integration into Your Project

To use toml-vala in your Vala project:

  1. Add the VAPI file to your Vala compiler flags:
    valac --vapidir=path/to/toml-vala --pkg toml-vala your_source.vala
    
  2. Link against the shared library:
    gcc -o your_program your_source.o -Lpath/to/toml-vala -ltoml-vala -lgee-0.8 -ljson-glib-1.0 -lgio-2.0 -lglib-2.0
    

For meson builds, add the library as a subproject or dependency.

Usage

Basic Parsing

using Gee;

public static int main(string[] args) {
    try {
        // Parse with automatic encoding detection
        var root = TomlParser.parse_file("config.toml");
        
        // Or specify encoding explicitly
        // var root = TomlParser.parse_file("config.toml", "UTF-16");
        
        // Access values
        var title = root.get("title").string_val;
        var port = root.get("server").get("port").int_val;
        
        // Iterate over arrays
        var servers = root.get("servers").array_val;
        foreach (var server in servers) {
            var name = server.get("name").string_val;
            print("Server: %s\n", name);
        }
    } catch (TomlError e) {
        stderr.printf("Error: %s\n", e.message);
        return 1;
    }
    return 0;
}

Asynchronous Parsing

using Gee;
using GLib;

public static async void parse_async() {
    try {
        // Async parsing with encoding support
        var root = yield TomlParser.parse_file_async("config.toml", "UTF-8");
        // Use root as above
    } catch (TomlError e) {
        stderr.printf("Error: %s\n", e.message);
    }
}

File Watching

using Gee;
using GLib;

public static int main() {
    var watcher = new TomlWatcher("config.toml");
    watcher.changed.connect((root) => {
        print("Config reloaded!\n");
        // Handle config changes
    });
    watcher.start();
    
    var loop = new MainLoop();
    loop.run();
    return 0;
}

Serialization

using Gee;

public static int main() {
    try {
        var root = TomlParser.parse_file("config.toml");
        
        // Serialize to TOML
        var toml_str = root.to_toml();
        print("%s\n", toml_str);
        
        // Serialize to JSON
        var json_str = root.to_json();
        print("%s\n", json_str);
    } catch (TomlError e) {
        stderr.printf("Error: %s\n", e.message);
        return 1;
    }
    return 0;
}

Validation

using Gee;

public static int main(string[] args) {
    // Validate TOML file (optional feature, not linked by default)
    var errors = TomlValidator.validate_file("config.toml");
    if (errors.size > 0) {
        foreach (var error in errors) {
            print("Error at line %d, col %d: %s\n", error.line, error.col, error.message);
            print("Suggestion: %s\n", error.suggestion);
        }
        return 1;
    }
    print("TOML file is valid.\n");
    return 0;
}

API Reference

TomlValue

  • TomlType val_type: The type of the value.
  • string? string_val: String value (for STRING, DATETIME).
  • int64 int_val: Integer value.
  • double float_val: Float value.
  • bool bool_val: Boolean value.
  • Gee.ArrayList<TomlValue>? array_val: Array value.
  • Gee.HashMap<string, TomlValue>? table_val: Table value.

Methods:

  • TomlValue? get(string key): Get a value from a table.
  • TomlValue? get_index(int index): Get a value from an array.
  • string to_toml(): Serialize to TOML string.
  • string to_json(): Serialize to JSON string.

TomlParser

Static methods:

  • TomlValue parse_file(string filename, string? encoding = null) throws TomlError: Parse a TOML file synchronously. Encoding defaults to auto-detection.
  • async TomlValue parse_file_async(string filename, string? encoding = null) throws TomlError: Parse a TOML file asynchronously. Encoding defaults to auto-detection.

TomlValidator

  • Gee.ArrayList<TomlValidationError> validate_file(string filename, string? encoding = null): Validate a TOML file and return a list of errors with suggestions.

TomlValidationError

  • string message: Error message.
  • int line: Line number (0 if not available).
  • int col: Column number (0 if not available).
  • string suggestion: Suggested fix.

TomlWatcher

  • TomlWatcher(string filename): Constructor.
  • void start(): Start watching the file.
  • void stop(): Stop watching.
  • signal void changed(TomlValue root): Emitted when the file changes and is re-parsed.

TomlError

Error domain with codes: INVALID_SYNTAX, INVALID_VALUE, DUPLICATE_KEY, MISSING_KEY.

Limitations

  • Datetimes are stored as strings; full RFC3339 parsing may be added in future versions.
  • Unicode escape sequences (\u/\U) are not fully decoded; basic handling is implemented.
  • The to_toml() serializer is simple and may not preserve all formatting/comments.
  • No support for binary data or extensions beyond TOML v1.0.0.
  • Validation currently reports only the first error encountered; full multi-error validation may be improved in future versions.

Contributing

Contributions are welcome! Please submit issues and pull requests on GitHub.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A pure Vala TOML parser library adhering to TOML v1.0.0 specification, with async parsing, file watching, and JSON serialization support.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published