14 releases

0.5.0 Feb 16, 2023
0.4.4 Aug 10, 2022
0.4.3 Jun 20, 2022
0.3.0 Mar 15, 2022

#471 in Cargo plugins

Download history 244/week @ 2024-07-27 2/week @ 2024-08-03 37/week @ 2024-08-10 94/week @ 2024-08-17 339/week @ 2024-08-24 221/week @ 2024-08-31 64/week @ 2024-09-07 241/week @ 2024-09-14 238/week @ 2024-09-21 181/week @ 2024-09-28 133/week @ 2024-10-05 125/week @ 2024-10-12 34/week @ 2024-10-19 70/week @ 2024-10-26 45/week @ 2024-11-02 27/week @ 2024-11-09

198 downloads per month

MIT/Apache

36KB
607 lines

Cargo spec  

This is a tool to turn your code into a specification. It is language agnostic, but you need Cargo (which you can install via Rustup) to use it. To see it in action, just look at the code in this codebase, as well as the spec it produced. You can read more about the concepts behind it in the blogpost The code is the specification: introducing cargo-spec.

Usage

initialize. To start your specification, you need two files: a Specification.toml configuration file and a template containing your specification written in markdown. You can create these in your current directory via cargo spec new <NAME>, or on a given path via cargo spec init <PATH>:

$ cargo install cargo-spec
$ cargo spec new

Created new specification as Specification.toml and specification_template.md
You can now run `cargo spec build` to create the specification file

build. Building your specification will convert your template into the desired format (markdown by default), at some path (specification.md by default):

$ cargo spec build

=> html output saved at ./specification.md

You can also watch for any changes:

$ cargo spec watch

=> html output saved at ./specification.md

How to write a specification?

cargo-spec's philosophy stems from the fact that most protocols often come from a reference implementation. That reference implementation tends to change, and as you want your spec to be up to date you will want to keep parts of the spec as close to the code as possible.

cargo-spec allows you to write your specification as a markdown file, and extract special "spec comments" from your code to fill in sections of your spec.

Configuration

To run cargo-spec, you need a Specification.toml file. Although you can specify a different filename via the --specification-path option.

[specification]
# some metadata
name = "Consensus"
description = "This specification describes the consensus protocol."
version = "0.1.0"
authors = ["David Wong"]

[config]
# the path to your template
template = "template.md"

[sections]
# all the files you want to extract "spec comments" from
data_structures = "src/data_structures.rs"
abstract_modules = "@/src/module.rs" # you can also use absolute paths (you need to be in a git repo)

Template

A template is simply a markdown file that contains placeholders. The path of the template must be specified in the Specification.toml file. By default the cargo spec new <NAME> (or cargo spec init <PATH>) command will use specification_template.md as template.

# Consensus specification

Here's the consensus spec

## Data structures

{sections.data_structures}

## Abstract modules

{sections.abstract_modules}

Spec comments in your code

Cargo-spec recognizes comments starting with the tilde ~. For example, in rust:

//~ some specification text

in Python:

#~ here's some spec

or in OCaml:

(*~ some spec *)

While cargo-spec is language-agnostic, it does not support all type of comments. Post an issue if it does not work for the language you're using.

Nested lists

It can be tiring to indent manually your comments to create nested lists:

//~ - a list
//~   - a nested list

instead, simply add ~ at the start of your spec comment to add indentation:

//~ - a list
//~~ - a nested list

Importing code

You can import blocks of code by surrounding them with //~ spec:startcode and //~ spec:endcode:

//~ spec:startcode
struct SomeStruct {
  a: u8,
  b: u64,
}
//~ spec:endcode

Continuous Integration

You'll most likely want to enforce that PRs contains up-to-date specification files checked-in. You can do this for example with this Github Action:

name: Check specifications

on:
  pull_request:

jobs:
  run_checks:
    runs-on: ubuntu-latest
    name: Enforce up-to-date specification files
    steps:

      - name: Checkout PR
        uses: actions/checkout@v2

      - name: Set up cargo/rust
        uses: actions-rs/toolchain@v1

      - name: Check that up-to-date specification is checked in
        run: |
          cargo install cargo-spec
          cd <spec_folder>
          cargo spec build
          git diff --exit-code

Projects making use of cargo-spec

License

The Cargo spec Project is dual-licensed under Apache 2.0 and MIT terms. See LICENSE-APACHE and LICENSE-MIT for details.

Dependencies

~13–25MB
~389K SLoC