formattable

Crates.ioformattable
lib.rsformattable
version0.2.0
created_at2025-03-28 22:59:44.728124+00
updated_at2025-05-06 23:21:45.830172+00
descriptionErgonomically support formatted output
homepagehttps://git.sr.ht/~yikesh/formattable
repositoryhttps://git.sr.ht/~yikesh/formattable
max_upload_size
id1610652
size26,149
(yikesh)

documentation

README

formattable

Make it easy and ergonomic to provide formatted output.

[Format] is an enum that allows selecting a particular format, but supports standard methods for serializing content. This makes it convenient to select a user-selected serializer at runtime.

For usage with clap, we also provide [FormatArg] and [FormatArgOpt] which standardizes the flags and parsing for selecting a serializer.

Usage

Make sure you enable/disable the appropriate features for the functionality you want.

Basic

use formattable::Format;
use serde::Serialize;

// Have some data structure that can be serialized.
#[derive(Serialize)]
struct Foo;

// Instantiate a Format variant. Format is Copy, so it's easy to use and pass
// around.
let fmt = Format::Json;

// Write your data structure to a JSON string.
let foo = Foo;
fmt.to_string(&foo).unwrap();

clap Integration [feature: clap]

use clap::Parser;
use formattable::Format;
use serde::Serialize;

/// Demonstrate how to use [`Format`] in a `clap`-based CLI.
///
/// This example just dumps the CLI arguments themselves as the selected format.
#[derive(Debug, Parser, Serialize)]
struct Cli {
    /// Select a format for output.
    #[clap(short, long, value_enum, default_value_t = Format::Json)]
    format: Format,
}

fn main() {
    let cli = Cli::parse();

    println!("{}", cli.format.to_string(&cli).unwrap());
}

Flattened clap Integration [feature: clap]

The easiest way to consistently integrate [Format] with clap is to use [FormatArg], which implements clap::Args:

use clap::Parser;
use formattable::FormatArg;
use serde::Serialize;

/// Demonstrate how to use `formattable` in a `clap`-based CLI.
///
/// This example just dumps the CLI arguments themselves as the selected format.
#[derive(Debug, Parser, Serialize)]
struct Cli {
    #[clap(flatten)]
    format: FormatArg,
}

fn main() {
    let cli = Cli::parse_from(["", "-f", "json"]);

    println!("{}", cli.format.to_string_pretty(&cli).unwrap());
}

Features

  • default [toml, json, yaml]
  • clap - enables the clap CLI integration
  • json - default; enables serialization to JSON
  • yaml - default; enables serialization to YAML
  • toml - default; enables serialization to TOML

Roadmap

There's no concrete roadmap or plans, but general ideas that would be interesting to implement include:

  • Use a macro to remove the boilerplate around the [FormatArg] and [FormatArgOpt] struct definitions and implementations;
  • Instead of providing dedicated argument types, provide a macro that can be used to add format support to structs;
  • Add support for a default format. This would be relatively easy if functionality was provided via macro;
  • Figure out some way to have text be a selectable format.
Commit count: 0

cargo fmt