dream_snippets/config

Reads what the check needs from the gleam.toml of a project.

The badge links of a project point at its own GitHub repository, at the tag of its own version, so the check reads name, version and repository from gleam.toml. The places that it reads, and the first version whose changelog section and release notes it checks, come from an optional [tools.dream_snippets] table. These are the defaults:

The default settings Tested snippet

[tools.dream_snippets]
markdown = ["README.md", "SECURITY.md", "docs", "examples"]
changelog = ["CHANGELOG.md"]
release_notes = ["releases"]
gleam_source = ["src"]
checked_from_version = "0.0.0"
guides = ["docs/guides"]
tested_files = ["test", ".github"]
badge_guide = "https://hexdocs.pm/dream_snippets/guides/tested-code-blocks.html"
release_notes_guide = "https://hexdocs.pm/dream_snippets/guides/release-notes-template.html"

Each setting is optional. The values above are the defaults. The default checked_from_version checks every changelog section and every release note. tested_files are the files and directories that the tests or CI of the project use: a file, command or output block links to a file that is one of them or under one of them. guides are the directories that hold the guides of the project: the guide badge of a README example links to a .md file under one of them. The default badge_guide is the “Tested code blocks” guide of this package on HexDocs, which shows the exact headings and badges to copy. The default release_notes_guide is the “Release notes template” guide of this package on HexDocs. A root list that is set replaces the default list of its kind, and each of its paths must exist. A default path that does not exist is skipped.

A release_notes path is a directory or one file. A directory is walked for release-<version>.md files, and files with other names are skipped. A file path must have a name of the form release-<version>.md, or the check reports one problem at line 1 of that file. In both cases, the notes of a version before checked_from_version are skipped.

Types

A file, or a directory that is walked for files of one kind, relative to the project root.

pub type CheckedRoot {
  CheckedRoot(
    path: String,
    kind: check.DocumentKind,
    presence: RootPresence,
  )
}

Constructors

Everything the check reads from gleam.toml.

pub type Config {
  Config(
    project: check.Project,
    roots: List(CheckedRoot),
    checked_from_version: check.Version,
  )
}

Constructors

Why gleam.toml cannot be used.

pub type ConfigError {
  InvalidToml(got: String, expected: String)
  RepeatedKey(key: String)
  VersionNotFound
  NameNotFound
  RepositoryNotFound
  UnsupportedRepository(repository_type: String)
  WrongSettingType(key: String, expected: String, found: String)
  RootNotAString(key: String)
  UnknownSetting(key: String)
  InvalidCheckedFromVersion(value: String)
}

Constructors

  • InvalidToml(got: String, expected: String)

    The file is not valid TOML: the parser found got where it expected expected.

  • RepeatedKey(key: String)

    The file sets the same key twice.

  • VersionNotFound

    The file has no version = "..." line before its first [section].

  • NameNotFound

    The file has no name = "..." line before its first [section].

  • RepositoryNotFound

    The file has no repository table with type, user and repo.

  • UnsupportedRepository(repository_type: String)

    The repository is not on GitHub. repository_type is its type.

  • WrongSettingType(key: String, expected: String, found: String)

    A key has a value of the wrong type. key is the dotted key, expected says what it must be, and found is the TOML type it has.

  • RootNotAString(key: String)

    A root list or guides in [tools.dream_snippets] holds a value that is not a string. key is the dotted key of the list.

  • UnknownSetting(key: String)

    [tools.dream_snippets] has a key that the check does not know.

  • InvalidCheckedFromVersion(value: String)

    checked_from_version is not a major.minor.patch version.

Whether a checked root must exist.

pub type RootPresence {
  MustExist
  MayBeMissing
}

Constructors

  • MustExist

    The root is set in [tools.dream_snippets], so a missing root is an error.

  • MayBeMissing

    The root is a default, so a missing root is skipped.

Values

pub const default_badge_guide: String

The document that a malformed badge message names when badge_guide is not set: the “Tested code blocks” guide of this package on HexDocs, which shows the exact headings and badges to copy.

pub const default_checked_from_version: check.Version

The first version whose release notes and changelog section are checked when checked_from_version is not set: 0.0.0, so every version is checked.

pub const default_guides: List(String)

The directories that hold the guides of a project when guides is not set. The guide badge of a README example links to a .md file under one of them.

pub const default_release_notes_guide: String

The document that a release notes template message names when release_notes_guide is not set: the “Release notes template” guide of this package on HexDocs, which shows the template.

pub fn default_roots() -> List(CheckedRoot)

The places that the check reads when [tools.dream_snippets] sets none: README.md, SECURITY.md, docs/ and examples/ as Markdown, CHANGELOG.md as the changelog, releases/ as release notes, and the doc comments in src/. Each one may be missing.

These are never walked, on purpose: build/, deps/, _build/ and node_modules/, which hold build output and the docs of other projects.

Examples

List the default roots Tested snippet

import dream_snippets/config
import gleam/list

pub fn default_paths() -> List(String) {
  // Returns [
  //   "README.md", "SECURITY.md", "docs", "examples", "CHANGELOG.md",
  //   "releases", "src",
  // ]
  list.map(config.default_roots(), fn(root) { root.path })
}
pub const default_tested_files: List(String)

The files and directories that the tests or CI of a project use when tested_files is not set: test, where the tests read their files, and .github, where CI reads its workflows and scripts. A file, command or output block links to a file that is one of them or under one of them.

pub fn describe_error(error: ConfigError) -> String

Describes why gleam.toml cannot be used, in one line (see check.one_line).

Examples

Describe a configuration error Tested snippet

import dream_snippets/config.{NameNotFound}

pub fn name_not_found_message() -> String {
  // Returns "gleam.toml has no name = \"...\" line before its first [section]"
  config.describe_error(NameNotFound)
}
pub const gleam_toml_path: String

The path of the file that the configuration is read from, relative to the project root.

pub fn parse(gleam_toml: String) -> Result(Config, ConfigError)

Reads the configuration from the text of gleam.toml.

Parameters

  • gleam_toml: the text of the file

Returns

The project (name, version, the GitHub user and repo of repository, badge_guide, release_notes_guide and guides), the checked roots and the checked_from_version.

Examples

Read the configuration of a project Tested snippet

import dream_snippets/config.{type Config, type ConfigError}

pub fn read_config() -> Result(Config, ConfigError) {
  config.parse(
    "name = \"my_app\"
version = \"1.2.0\"
repository = { type = \"github\", user = \"me\", repo = \"my_app\" }

[tools.dream_snippets]
checked_from_version = \"1.0.0\"
",
  )
}

Errors

A ConfigError when the text is not valid TOML, when name, version or a GitHub repository is missing, or when a setting in [tools.dream_snippets] is unknown or has the wrong type.

✨ Search Document