dream_snippets/check

The checks: each code block in a document is backed by tested code.

Every fenced code block has a kind, which classify_fence reads from its info string, and each kind has its own proof (see BlockKind): gleam and diff blocks as below; file, command and output blocks as copies of files that the tests or CI use, with the same badge as a Gleam snippet; gleam add, gleam deps update and toml dependency lines with no file, from gleam.toml and the release of the document; and illustrations, which nothing proves, but which are counted and hold no Gleam code. A fence with no language is a problem. No block is skipped in silence.

Each ```gleam block in a Markdown doc, and each one in a /// or //// doc comment in src/, has a heading directly above it that ends with the “tested snippet” badge. The badge links to a file in the GitHub repository of the project, at the tag of the release of the document (see “The release of a document” below), for example https://github.com/TrustBound/dream_snippets/blob/1.0.0/test/snippets/name.gleam#L8-L10. The block is that whole file, or exactly that line range, byte for byte. The tests of the project compile and run the file, so the code in the docs is code that works.

An illustration must not hold Gleam code, because then the check would not prove it. A line in an illustration is Gleam code when it is one of these forms, which no other common language of the docs has (see looks_like_gleam):

A ```markdown fence holds an example document. Its fences are checked like the fences of any other document, and its other lines must not look like Gleam code.

A ```diff block shows how code changed between two releases, for example in an upgrade guide. Old code does not compile on the new version, so it cannot be a tested snippet. It is an excerpt of the real git diff instead. Its heading ends with the “tested diff” badge, which links to GitHub’s compare view of one file, for example https://github.com/TrustBound/dream/compare/2.4.1...3.0.0#diff-<digest>, where <digest> is the SHA-256 hex digest of the file’s path (GitHub’s own anchor for the file). The block starts with the --- a/<path> and +++ b/<path> lines, then holds lines of the diff, with or without @@ hunk headers. The new tag is the tag of the release of the document. For the version in gleam.toml, that tag does not exist before the release, so the check runs git diff <old tag> -- <path> against the working tree. For an older release, it runs git diff <old tag> <new tag> -- <path>. Each run of lines must be in that diff, in order and contiguous within one hunk.

A guide or release notes can also show code as it was at an older release, next to the new code. Old code does not compile on the new version, so it cannot be a tested snippet. It is proven against git instead. Its heading ends with the “code at an older release” badge, which links to a file at the older tag, for example https://github.com/TrustBound/dream_test/blob/2.1.3/src/dream_test/unit.gleam#L40-L52. The tag is a release before the release of the document. The check reads the file with git show <tag>:<path>, and the block is that whole file, or exactly that line range, byte for byte. The README, the changelog and doc comments show only code of the current version, so this badge is a problem there.

In the root README, the heading of each ```gleam block also has the guide badge after the tested snippet badge. It links to the section of a guide that shows the example (see check_readme). In every other document, a heading has no guide badge.

The release of a document: release notes, release-<version>.md, and a changelog section, ## [<version>], belong to their own version. Every other document, the ## [Unreleased] section and the text before the first changelog section belong to the version in gleam.toml. The badge links of a document use the tag of its release. When the release is the version in gleam.toml, its blocks are compared with the working tree. When it is an older release, they are compared with the files at its tag, so release notes and changelog sections stay right after a version bump. Versions are ordered as semver 2.0.0 orders them (see compare_versions).

Release notes and changelog entries are checked from the checked_from_version of the CheckContext on. The ones before it are skipped, for example the notes of releases from before the project used tested docs.

The functions here are pure. They get files and git through a CheckContext, so tests can give them fake files and a fake git history. dream_snippets.check_project lists and reads the files of a real project and gives the real git to the context.

Types

The kind of a fenced code block, read from its info string by classify_fence. Each kind has its own proof, and no kind is skipped.

pub type BlockKind {
  GleamBlock
  DiffBlock
  ExampleDocumentBlock
  CommandBlock(language: String)
  OutputBlock
  IllustrationBlock(language: String)
  FileBlock(language: String)
  UnlabelledBlock
}

Constructors

  • GleamBlock

    A gleam block: a copy of a tested snippet file, or of code at an older release.

  • DiffBlock

    A diff block: a part of the real git diff of one file.

  • ExampleDocumentBlock

    A markdown or md block: an example document. Its fences are checked like the fences of any other document, and its other lines must not look like Gleam code.

  • CommandBlock(language: String)

    A bash, sh, shell or console block: a copy of a script that CI or a test runs, or only gleam add and gleam deps update lines that name packages of gleam.toml.

  • OutputBlock

    A text block: a copy of an output file that a test compares with the real output.

  • IllustrationBlock(language: String)

    A block whose info string is a language and then the word illustration, such as text illustration: a diagram, a tree or a placeholder form that nothing proves. It is counted, and it must not hold Gleam code. A gleam, diff, markdown or md block is never an illustration.

  • FileBlock(language: String)

    A block with any other language, such as yaml, toml or sql: a copy of a file that the tests or CI use, or for toml only dependency lines of the package of the project.

  • UnlabelledBlock

    A block with no language. Nothing can prove it, so it is a problem.

What the checks need from the outside.

project names the repository that badge links must point at and the release tag that they must use. checked_from_version is the first version whose release notes and changelog section are checked. read_source reads a file by its path relative to the repository root. tag_exists tells whether a git tag is in the local clone. diff_from_tag returns the text of the git diff of one path (the second argument) from a tag (the first argument) to the working tree. diff_between_tags returns the text of the git diff of one path (the third argument) from an old tag (the first argument) to a new tag (the second argument). file_at_tag returns the text of one path (the second argument) as it was at a tag (the first argument), or None when the tag has no file at that path. dream_snippets.check_project gives them simplifile.read, git.tag_exists, git.diff_from_tag, git.diff_between_tags and git.file_at_tag. Tests can give them lookups in lists, so that they do not depend on real files or a real history.

pub type CheckContext {
  CheckContext(
    project: Project,
    checked_from_version: Version,
    read_source: fn(String) -> Result(
      String,
      simplifile.FileError,
    ),
    tag_exists: fn(String) -> Result(Bool, git.GitError),
    diff_from_tag: fn(String, String) -> Result(
      String,
      git.GitError,
    ),
    diff_between_tags: fn(String, String, String) -> Result(
      String,
      git.GitError,
    ),
    file_at_tag: fn(String, String) -> Result(
      option.Option(String),
      git.GitError,
    ),
  )
}

Constructors

Where the code of a release is read: the tested snippet files that its docs link to, and the new side of its diff blocks.

pub type CodeSource {
  WorkingTree
  FilesAtTag(tag: String)
}

Constructors

  • WorkingTree

    The working tree. The docs of the version in gleam.toml read it, because the tag of that version does not exist before the release.

  • FilesAtTag(tag: String)

    The files at the tag of an older release.

A link to GitHub’s compare view of one file, read from a diff badge.

anchor_digest is the part of the anchor after #diff-.

pub type CompareLink {
  CompareLink(
    old_tag: String,
    new_tag: String,
    anchor_digest: String,
  )
}

Constructors

  • CompareLink(
      old_tag: String,
      new_tag: String,
      anchor_digest: String,
    )

One hunk of a real git diff: its @@ header line and the lines after it, each with its -, + or space prefix.

pub type DiffHunk {
  DiffHunk(header: String, lines: List(String))
}

Constructors

  • DiffHunk(header: String, lines: List(String))

The kind of file that a checked root holds.

pub type DocumentKind {
  MarkdownDocument
  GleamSource
  ReleaseNotes
  Changelog
}

Constructors

  • MarkdownDocument

    A Markdown document (.md). Each fenced code block in it is checked.

  • GleamSource

    A Gleam source file (.gleam). Each fenced code block in its /// and //// doc comments is checked.

  • ReleaseNotes

    A release notes file, release-<version>.md. It is checked like a guide of the release <version> when that version is checked_from_version or later, and skipped when it is older. A directory walk skips a file with no version in its name. A file that is itself a root and has no version in its name gets one problem, MisnamedReleaseNotes.

  • Changelog

    The changelog. Its ## [<version>] sections for checked_from_version and later, its ## [Unreleased] section and the text before the first section are checked. A ## [<version>] section belongs to the release <version>. The sections of versions before checked_from_version are skipped.

The badges of an example heading, read by parse_example_heading.

snippet_address is the link address of the tested snippet badge, and guide_badge is what the heading has of the guide badge.

pub type ExampleHeading {
  ExampleHeading(
    snippet_address: String,
    guide_badge: GuideBadgeFound,
  )
}

Constructors

A GitHub repository, from the repository field of gleam.toml: owner is its user and name its repo.

pub type GitHubRepository {
  GitHubRepository(owner: String, name: String)
}

Constructors

  • GitHubRepository(owner: String, name: String)

What an example heading has of the guide badge.

pub type GuideBadgeFound {
  NoGuideBadge
  GuideBadgeLink(
    address: String,
    alt: String,
    title: option.Option(String),
  )
  BrokenGuideBadge
}

Constructors

  • NoGuideBadge

    The heading has no guide badge.

  • GuideBadgeLink(
      address: String,
      alt: String,
      title: option.Option(String),
    )

    The heading ends with the guide badge in the exact form, directly after the tested snippet badge. address is its link address, alt its alt text, and title its title, or None when it has none. The check compares alt and title with the title of the guide.

  • BrokenGuideBadge

    The heading has a guide badge, but not in the exact form, or not directly after the tested snippet badge.

A link to a section of a guide, read from a guide badge.

anchor is the fragment after the #, or an empty string when the link has none.

pub type GuideLink {
  GuideLink(tag: String, path: String, anchor: String)
}

Constructors

  • GuideLink(tag: String, path: String, anchor: String)

One line of a document, with its 1-based line number in the file.

pub type Line {
  Line(number: Int, text: String)
}

Constructors

  • Line(number: Int, text: String)

The part of a file that a badge link names.

pub type LineSelection {
  WholeFile
  LineRange(start: Int, end: Int)
}

Constructors

  • WholeFile

    The link has no line anchor: the block is the whole file.

  • LineRange(start: Int, end: Int)

    The link ends in #L<start>-L<end> (or #L<n>, where start and end are both n): the block is those lines, counted from 1.

One dot-separated identifier of the pre-release part of a version.

pub type PreReleaseIdentifier {
  NumericIdentifier(number: Int)
  AlphanumericIdentifier(text: String)
}

Constructors

  • NumericIdentifier(number: Int)

    An identifier of digits only, such as 1 in rc.1. It has no leading zero.

  • AlphanumericIdentifier(text: String)

    An identifier with a letter or a -, such as rc or beta-2.

One problem: the file, the 1-based line, and why.

pub type Problem {
  Problem(file: String, line: Int, reason: Reason)
}

Constructors

  • Problem(file: String, line: Int, reason: Reason)

The project whose docs are checked, as its gleam.toml describes it.

name is the package name, used in the message of a wrong tag. version is the release of every document that names no release of its own: the tag that its badge links must use. Release notes and changelog sections of an older release use their own tag. repository is the GitHub repository that every badge must link at. badge_guide is the document or address that a malformed badge message tells the reader to copy the badge from. release_notes_guide is the document or address that a release notes template message tells the reader to read. guides are the directories that hold the guides of the project, such as docs/guides: the guide badge of a README example must link to a .md file under one of them. tested_files are the files and directories that the tests or CI of the project use, such as test and .github: a file, command or output block must link to a file that is one of them or under one of them. dependencies are the keys of the [dependencies] and [dev-dependencies] tables of gleam.toml: a gleam add or gleam deps update block names only them and name.

pub type Project {
  Project(
    name: String,
    version: String,
    repository: GitHubRepository,
    badge_guide: String,
    release_notes_guide: String,
    guides: List(String),
    tested_files: List(String),
    dependencies: List(String),
  )
}

Constructors

  • Project(
      name: String,
      version: String,
      repository: GitHubRepository,
      badge_guide: String,
      release_notes_guide: String,
      guides: List(String),
      tested_files: List(String),
      dependencies: List(String),
    )

Why a code block fails the check.

pub type Reason {
  MissingBadgeHeading
  OldTestedSourceLine
  MalformedBadge
  ForeignLink(url: String)
  WrongTag(found: String, expected: String)
  MissingFile(path: String)
  UnreadableFile(path: String, reason: simplifile.FileError)
  MalformedLineRange(anchor: String)
  ReversedLineRange(start: Int, end: Int)
  LineRangePastEnd(end: Int, line_count: Int)
  BlockDiffers(
    path: String,
    anchor: String,
    first_difference_line: Int,
  )
  HiddenGleamCode(language: String)
  UnclosedFence
  MissingDiffBadgeHeading
  MalformedDiffBadge
  NotCompareLink(url: String)
  WrongNewTag(found: String, expected: String)
  MissingDiffFileHeader
  EmptyDiffBlock
  WrongDiffAnchor(path: String, expected: String)
  MissingOldTag(tag: String)
  GitUnavailable(error: git.GitError)
  NoDiffForPath(
    path: String,
    old_tag: String,
    new_side: CodeSource,
  )
  HunkHeaderNotFound(header: String)
  DiffLineNotFound(
    path: String,
    old_tag: String,
    new_side: CodeSource,
  )
  ReleaseNotesTitleMismatch(found: String, version: String)
  MissingReleaseDate
  UnknownReleaseNotesSection(section: String)
  ReleaseNotesSectionOutOfOrder(section: String, after: String)
  RepeatedReleaseNotesSection(section: String, first_line: Int)
  ConflictingReleaseNotesSections(section: String, other: String)
  MissingReleaseNotesSection(section: String)
  MissingReleaseNotesChoice(sections: List(String))
  MissingReleaseNotesSubsection(
    section: String,
    subsection: String,
  )
  MisnamedReleaseNotes
  MissingGuideBadge
  MalformedGuideBadge
  GuideBadgeNotAllowed
  ForeignGuideLink(url: String)
  WrongGuideTag(found: String, expected: String)
  NotAGuidePath(path: String, guides: List(String))
  MissingGuideAnchor(path: String)
  MissingGuideFile(path: String)
  GuideAnchorNotFound(path: String, anchor: String)
  WrongGuideBadgeName(path: String, expected: String)
  UntitledGuide(path: String)
  GuideTextLink
  ExampleNotInGuide(path: String, snippet: String)
  MalformedOldCodeBadge
  OldCodeBadgeWithOtherBadge
  OldCodeNotAllowed(guides: List(String))
  UnsafeOldCodeLink(tag: String, path: String)
  NotAnOlderRelease(tag: String, version: String)
  MissingFileAtTag(path: String, tag: String)
  OldCodeDiffers(
    path: String,
    anchor: String,
    tag: String,
    first_difference_line: Int,
  )
  MissingReleaseTag(tag: String)
  ReleaseNotBeforeVersion(release: String, version: String)
  MissingFileBadgeHeading(language: String)
  MissingCommandBadgeHeading(language: String)
  MissingOutputBadgeHeading
  UnlabelledFence
  UntestedFile(path: String, tested_files: List(String))
  UnknownPackage(package: String)
  UnsupportedPackageArgument(argument: String)
  GleamAddWithoutPackage
  ForeignDependency(package: String)
  WrongDependencyRequirement(
    package: String,
    found: String,
    expected: String,
  )
}

Constructors

  • MissingBadgeHeading

    A Gleam block has no heading with the badge directly above it.

  • OldTestedSourceLine

    A Gleam block uses the old 🧪 Tested source line next to it.

  • MalformedBadge

    The heading has a badge, but not in the exact format.

  • ForeignLink(url: String)

    The badge links to another repository or host, or out of the repository.

  • WrongTag(found: String, expected: String)

    The tag in the badge link is not the tag of the release of the document: the version in gleam.toml, or for release notes and a changelog section, their own release.

  • MissingFile(path: String)

    The linked file does not exist in this repository.

  • UnreadableFile(path: String, reason: simplifile.FileError)

    The linked file exists but cannot be read.

  • MalformedLineRange(anchor: String)

    The line anchor is not #L<n> or #L<start>-L<end>.

  • ReversedLineRange(start: Int, end: Int)

    The line range starts after it ends.

  • LineRangePastEnd(end: Int, line_count: Int)

    The line range ends after the last line of the file.

  • BlockDiffers(
      path: String,
      anchor: String,
      first_difference_line: Int,
    )

    The block is not the same as the linked file or range. first_difference_line is the line in the document where the first difference is.

  • HiddenGleamCode(language: String)

    A fence with another language holds a line that looks like Gleam. language is the fence language, or an empty string for none.

  • UnclosedFence

    A fence has no closing fence.

  • MissingDiffBadgeHeading

    A diff block has no heading with the diff badge directly above it.

  • MalformedDiffBadge

    The heading has a diff badge, but not in the exact format.

  • NotCompareLink(url: String)

    The diff badge does not link to a compare view of this repository, as <old tag>...<new tag>#diff-<digest>, or a tag has characters that a tag name in this repository does not have.

  • WrongNewTag(found: String, expected: String)

    The new tag in the compare link is not the tag of the release of the document (see WrongTag).

  • MissingDiffFileHeader

    The diff block does not start with --- a/<path> and +++ b/<path> for one path, or the path has characters that are not allowed.

  • EmptyDiffBlock

    The diff block has no lines after its file header.

  • WrongDiffAnchor(path: String, expected: String)

    The anchor in the compare link is not the digest of the path in the block. expected is the right anchor, with its #.

  • MissingOldTag(tag: String)

    The old tag in the compare link is not in the local clone.

  • GitUnavailable(error: git.GitError)

    git could not be run, or it failed.

  • NoDiffForPath(
      path: String,
      old_tag: String,
      new_side: CodeSource,
    )

    The file did not change between the old tag and new_side.

  • HunkHeaderNotFound(header: String)

    A hunk header in the block is not a hunk header of the real diff, or it is out of order.

  • DiffLineNotFound(
      path: String,
      old_tag: String,
      new_side: CodeSource,
    )

    A line of the block is not in the real diff from the old tag to new_side at that place.

  • ReleaseNotesTitleMismatch(found: String, version: String)

    The first line of release notes is not # <Name> <version> Release Notes. found is the first line, and version is the version in the file name (or <version> when the name has none).

  • MissingReleaseDate

    Release notes have no line that starts with **Release Date:** between the title and the first ## section.

  • UnknownReleaseNotesSection(section: String)

    A ## section of release notes is not a section of the template.

  • ReleaseNotesSectionOutOfOrder(section: String, after: String)

    A ## section of release notes comes after after, but the template has it before after.

  • RepeatedReleaseNotesSection(section: String, first_line: Int)

    A ## section of release notes is in the file again. first_line is the line where it is first.

  • ConflictingReleaseNotesSections(section: String, other: String)

    Release notes have two ## sections that the template has as a choice of one, such as Upgrading and Installing. other is the one that comes first.

  • MissingReleaseNotesSection(section: String)

    Release notes do not have a ## section that the template requires.

  • MissingReleaseNotesChoice(sections: List(String))

    Release notes have none of the ## sections of a required choice, such as Upgrading or Installing.

  • MissingReleaseNotesSubsection(
      section: String,
      subsection: String,
    )

    A ## section of release notes does not have a ### heading that the template requires in it.

  • MisnamedReleaseNotes

    A file that a release_notes setting names is not named release-<version>.md, so the check cannot read its version.

  • MissingGuideBadge

    An example heading in the README has no guide badge after its tested snippet badge.

  • MalformedGuideBadge

    An example heading in the README has a guide badge, but not in the exact form, or not directly after the tested snippet badge.

  • GuideBadgeNotAllowed

    A heading outside the README has a guide badge. Only README examples have one.

  • ForeignGuideLink(url: String)

    The guide badge links to another repository or host, or out of the repository.

  • WrongGuideTag(found: String, expected: String)

    The tag in the guide badge link is not the release tag of the document (see CheckContext).

  • NotAGuidePath(path: String, guides: List(String))

    The guide badge links to a path that is not a .md file under one of the guide directories. guides are those directories.

  • MissingGuideAnchor(path: String)

    The guide badge link has no #<anchor> for the section of the example.

  • MissingGuideFile(path: String)

    The guide file of the guide badge does not exist in this repository.

  • GuideAnchorNotFound(path: String, anchor: String)

    The guide file has no heading whose anchor is anchor (without its #). See heading_anchors.

  • WrongGuideBadgeName(path: String, expected: String)

    The alt text or the title of the guide badge is not expected: Guide: and the title of the guide file at path.

  • UntitledGuide(path: String)

    The guide file at path has no # title line outside code fences, so the guide badge cannot name it.

  • GuideTextLink

    A <sub>📖 [...](...)</sub> line links to a guide from under an example. The guide badge on the example heading is the only link from an example to its guide.

  • ExampleNotInGuide(path: String, snippet: String)

    The guide file has no tested snippet badge that links to snippet, so it does not show the example.

  • MalformedOldCodeBadge

    The heading has a “code at an older release” badge, but not in the exact form, or the tag in its alt text or image is not the tag of its link.

  • OldCodeBadgeWithOtherBadge

    The heading has a “code at an older release” badge and another badge.

  • OldCodeNotAllowed(guides: List(String))

    A “code at an older release” badge is in a document that is not a guide or release notes. guides are the guide directories.

  • UnsafeOldCodeLink(tag: String, path: String)

    The tag or the path of a “code at an older release” link has characters that the check does not give to git (see is_safe_tag and is_safe_path).

  • NotAnOlderRelease(tag: String, version: String)

    The tag of a “code at an older release” link is not a release before version, the release of the document (see WrongTag).

  • MissingFileAtTag(path: String, tag: String)

    The tag of a “code at an older release” link has no file at path.

  • OldCodeDiffers(
      path: String,
      anchor: String,
      tag: String,
      first_difference_line: Int,
    )

    The block is not the same as the linked file or range at the old tag. first_difference_line is the line in the document where the first difference is.

  • MissingReleaseTag(tag: String)

    Release notes or a changelog section belong to the older release tag, so their blocks are compared with the files at that tag, but the tag is not in the local clone.

  • ReleaseNotBeforeVersion(release: String, version: String)

    Release notes or a changelog section belong to release, which is not version, the version in gleam.toml, and not a release before it.

  • MissingFileBadgeHeading(language: String)

    A file block (see FileBlock) has no heading with the tested snippet badge directly above it, and it is not a block of dependency lines.

  • MissingCommandBadgeHeading(language: String)

    A command block (see CommandBlock) has no heading with the tested snippet badge directly above it, and it is not a block of only gleam add and gleam deps update lines.

  • MissingOutputBadgeHeading

    An output block (see OutputBlock) has no heading with the tested snippet badge directly above it.

  • UnlabelledFence

    A code fence has no language, so nothing can prove its block.

  • UntestedFile(path: String, tested_files: List(String))

    The badge of a file, command or output block links to path, which is not one of the tested_files paths of the project and not under one of them.

  • UnknownPackage(package: String)

    A gleam add or gleam deps update line names package, which is not the package of the project and not a key of [dependencies] or [dev-dependencies] in gleam.toml.

  • UnsupportedPackageArgument(argument: String)

    A gleam add or gleam deps update line has argument, which is not a package name, and not --dev on a gleam add line.

  • GleamAddWithoutPackage

    A gleam add line names no package.

  • ForeignDependency(package: String)

    A block of dependency lines names package, which is not the package of the project.

  • WrongDependencyRequirement(
      package: String,
      found: String,
      expected: String,
    )

    The requirement of the package of the project in a block of dependency lines is found, but the release of the document wants expected.

The result of checking one or more documents.

blocks_checked counts the Gleam code blocks that were checked, and diff_blocks_checked the diff blocks. file_blocks_checked, command_blocks_checked and output_blocks_checked count the blocks of those kinds (see BlockKind), and illustration_blocks the blocks marked as an illustration, which nothing proves.

pub type Report {
  Report(
    blocks_checked: Int,
    diff_blocks_checked: Int,
    file_blocks_checked: Int,
    command_blocks_checked: Int,
    output_blocks_checked: Int,
    illustration_blocks: Int,
    problems: List(Problem),
  )
}

Constructors

  • Report(
      blocks_checked: Int,
      diff_blocks_checked: Int,
      file_blocks_checked: Int,
      command_blocks_checked: Int,
      output_blocks_checked: Int,
      illustration_blocks: Int,
      problems: List(Problem),
    )

A ## heading that the release notes template allows.

name is the text of the heading after ## . subsections are the ### headings that the section must have when the file has it.

pub type SectionHeading {
  SectionHeading(name: String, subsections: List(String))
}

Constructors

  • SectionHeading(name: String, subsections: List(String))

Whether the release notes template requires a section.

pub type SectionPresence {
  RequiredSection
  OptionalSection
}

Constructors

  • RequiredSection

    The file must have the section. For a choice of headings, it must have one of them.

  • OptionalSection

    The file may leave the section out.

A link to a tested snippet, read from a badge.

anchor is the line anchor with its #, or an empty string when the link names the whole file.

pub type SnippetLink {
  SnippetLink(tag: String, path: String, anchor: String)
}

Constructors

  • SnippetLink(tag: String, path: String, anchor: String)

One place in the order of the release notes template. choices are the headings that can fill the place, and a file has one of them at most. With RequiredSection, a file has exactly one of them.

pub type TemplateSection {
  TemplateSection(
    choices: List(SectionHeading),
    presence: SectionPresence,
  )
}

Constructors

A release version as semver 2.0.0 writes it: major.minor.patch, then an optional pre-release part after a -. pre_release is empty for a release, and holds the identifiers of the pre-release part in order, for example [AlphanumericIdentifier("rc"), NumericIdentifier(1)] for 3.0.0-rc.1. See compare_versions for the order of versions.

pub type Version {
  Version(
    major: Int,
    minor: Int,
    patch: Int,
    pre_release: List(PreReleaseIdentifier),
  )
}

Constructors

Values

pub fn add_report(total: Report, report: Report) -> Report

Adds the counts and the problems of two reports. The problems of report come after those of total.

Examples

Add two reports Tested snippet

import dream_snippets/check.{type Report}

pub fn two_reports() -> Report {
  let first = check.Report(..check.empty_report(), blocks_checked: 2)
  let second = check.Report(..check.empty_report(), output_blocks_checked: 1)
  // Returns Report(blocks_checked: 2, diff_blocks_checked: 0,
  //   file_blocks_checked: 0, command_blocks_checked: 0,
  //   output_blocks_checked: 1, illustration_blocks: 0, problems: [])
  check.add_report(first, second)
}
pub fn changelog_section_name(
  text: String,
) -> Result(String, Nil)

Reads the name of a changelog section heading, for example 3.0.0 from ## [3.0.0] - 2026-11-20.

Examples

Read the name of a changelog section Tested snippet

import dream_snippets/check

pub fn section_name() -> Result(String, Nil) {
  // Returns Ok("3.0.0")
  check.changelog_section_name("## [3.0.0] - 2026-11-20")
}
pub fn check_changelog(
  file: String,
  text: String,
  context: CheckContext,
) -> Report

Checks the changelog. Its sections start at ## [<name>] lines outside code fences. The sections of versions before checked_from_version are left out. Each other section, and the text before the first section, is checked like a Markdown document, and each line keeps its line number in the file.

A section whose name is a version belongs to that release: its badge links use the tag of that version, and when the version is older than the one in gleam.toml, its blocks are compared with the files at that tag. So the section of an older release stays right after a version bump. The Unreleased section, a section with another name and the text before the first section belong to the version in gleam.toml.

Examples

Check a changelog Tested snippet

import dream_snippets/check.{
  type Report, CheckContext, GitHubRepository, Project, Version,
}
import dream_snippets/git
import simplifile

pub fn check_my_changelog() -> Report {
  let project =
    Project(
      name: "my_app",
      version: "1.2.0",
      repository: GitHubRepository(owner: "me", name: "my_app"),
      badge_guide: "CONTRIBUTING.md",
      release_notes_guide: "CONTRIBUTING.md",
      guides: ["docs/guides"],
      tested_files: ["test", ".github"],
      dependencies: [],
    )
  let context =
    CheckContext(
      project:,
      checked_from_version: Version(1, 0, 0, []),
      read_source: simplifile.read,
      tag_exists: fn(_tag) { Error(git.GitNotFound) },
      diff_from_tag: fn(_tag, _path) { Error(git.GitNotFound) },
      diff_between_tags: fn(_old_tag, _new_tag, _path) {
        Error(git.GitNotFound)
      },
      file_at_tag: fn(_tag, _path) { Error(git.GitNotFound) },
    )
  let changelog =
    "# Changelog\n\n## [Unreleased]\n\n```gleam\nnew_code()\n```\n\n"
    <> "## [0.9.0] - 2026-01-10\n\n```gleam\nold_code()\n```\n"
  // Returns Report(..empty_report(), blocks_checked: 1, problems: [
  //   Problem(file: "CHANGELOG.md", line: 5, reason: MissingBadgeHeading),
  // ])
  check.check_changelog("CHANGELOG.md", changelog, context)
}
pub fn check_gleam_source(
  file: String,
  text: String,
  context: CheckContext,
) -> Report

Checks every fenced code block in the /// and //// doc comments of a Gleam source file.

Each run of doc comment lines of one kind is read as a Markdown document: the comment prefix (/// , //// , or /// and //// on a blank comment line) is removed, and each line keeps its line number in the file. Leading spaces before the prefix are allowed, so indented doc comments are checked too.

Examples

Check the doc comments of a source file Tested snippet

import dream_snippets/check.{
  type Report, CheckContext, GitHubRepository, Project, Version,
}
import dream_snippets/git
import simplifile

pub fn check_my_source() -> Report {
  let project =
    Project(
      name: "my_app",
      version: "1.2.0",
      repository: GitHubRepository(owner: "me", name: "my_app"),
      badge_guide: "CONTRIBUTING.md",
      release_notes_guide: "CONTRIBUTING.md",
      guides: ["docs/guides"],
      tested_files: ["test", ".github"],
      dependencies: [],
    )
  let context =
    CheckContext(
      project:,
      checked_from_version: Version(1, 0, 0, []),
      read_source: simplifile.read,
      tag_exists: fn(_tag) { Error(git.GitNotFound) },
      diff_from_tag: fn(_tag, _path) { Error(git.GitNotFound) },
      diff_between_tags: fn(_old_tag, _new_tag, _path) {
        Error(git.GitNotFound)
      },
      file_at_tag: fn(_tag, _path) { Error(git.GitNotFound) },
    )
  let source =
    "/// Says hello.
///
/// ```gleam
/// hello()
/// ```
pub fn hello() -> Nil {
  Nil
}
"
  // Returns Report(..empty_report(), blocks_checked: 1, problems: [
  //   Problem(file: "src/app.gleam", line: 3, reason: MissingBadgeHeading),
  // ])
  check.check_gleam_source("src/app.gleam", source, context)
}
pub fn check_markdown(
  file: String,
  text: String,
  context: CheckContext,
) -> Report

Checks every fenced code block in a Markdown document. A document under one of the guides directories of the project can show code at an older release (see parse_old_code_heading). In any other document, that badge is a problem.

Parameters

  • file: the path of the document, used in each problem
  • text: the content of the document
  • context: the project, the snippet files and git (see CheckContext)

Returns

A Report with the number of Gleam blocks and diff blocks checked, and each problem found, in the order of the document.

Examples

Check one document Tested snippet

import dream_snippets/check.{
  type Report, CheckContext, GitHubRepository, Project, Version,
}
import dream_snippets/git
import simplifile

pub fn check_guide(guide: String) -> Report {
  let project =
    Project(
      name: "my_app",
      version: "1.2.0",
      repository: GitHubRepository(owner: "me", name: "my_app"),
      badge_guide: "CONTRIBUTING.md",
      release_notes_guide: "CONTRIBUTING.md",
      guides: ["docs/guides"],
      tested_files: ["test", ".github"],
      dependencies: [],
    )
  let context =
    CheckContext(
      project:,
      checked_from_version: Version(1, 0, 0, []),
      read_source: simplifile.read,
      tag_exists: fn(_tag) { Error(git.GitNotFound) },
      diff_from_tag: fn(_tag, _path) { Error(git.GitNotFound) },
      diff_between_tags: fn(_old_tag, _new_tag, _path) {
        Error(git.GitNotFound)
      },
      file_at_tag: fn(_tag, _path) { Error(git.GitNotFound) },
    )
  check.check_markdown("docs/guide.md", guide, context)
}
pub fn check_readme(
  file: String,
  text: String,
  context: CheckContext,
) -> Report

Checks the root README of a project: every fenced code block, as check_markdown does, and the guide badge of each example.

In the README, the heading of each gleam block has two badges: the tested snippet badge, then one space and the guide badge. Both images float right, so the guide badge shows to the left of the tested snippet badge:

### Title <a href="https://github.com/<owner>/<repo>/blob/<version>/<snippet path>"><img align="right" alt="Tested snippet" src="https://img.shields.io/badge/%F0%9F%A7%AA_tested-snippet-lightgreen.svg?style=flat-square"></a> <a href="https://github.com/<owner>/<repo>/blob/<version>/docs/guides/<name>.md#<anchor>"><img align="right" alt="Guide" src="https://img.shields.io/badge/%F0%9F%93%96_read-guide-blue.svg?style=flat-square"></a>

The guide badge links to a .md file under one of the guides directories of the project, at the release tag, with the anchor of a heading of that guide (see heading_anchors). The guide must have a tested snippet badge that links to the same snippet file, so it shows the example. The guide badge is checked when the tested snippet badge is a link of the repository. Its problems are at the line of the heading. A diff block heading has no guide badge.

Examples

Find a missing guide badge Tested snippet

import dream_snippets/check.{
  type Report, CheckContext, GitHubRepository, Project, Version,
}
import dream_snippets/git

pub fn check_my_readme() -> Report {
  let project =
    Project(
      name: "my_app",
      version: "1.2.0",
      repository: GitHubRepository(owner: "me", name: "my_app"),
      badge_guide: "CONTRIBUTING.md",
      release_notes_guide: "CONTRIBUTING.md",
      guides: ["docs/guides"],
      tested_files: ["test", ".github"],
      dependencies: [],
    )
  let context =
    CheckContext(
      project:,
      checked_from_version: Version(1, 0, 0, []),
      read_source: fn(_path) { Ok("pub const answer = 42\n") },
      tag_exists: fn(_tag) { Error(git.GitNotFound) },
      diff_from_tag: fn(_tag, _path) { Error(git.GitNotFound) },
      diff_between_tags: fn(_old_tag, _new_tag, _path) {
        Error(git.GitNotFound)
      },
      file_at_tag: fn(_tag, _path) { Error(git.GitNotFound) },
    )
  let heading =
    "### The answer <a href=\"https://github.com/me/my_app/blob/1.2.0/test/snippets/answer.gleam\"><img align=\"right\" alt=\"Tested snippet\" src=\"https://img.shields.io/badge/%F0%9F%A7%AA_tested-snippet-lightgreen.svg?style=flat-square\"></a>"
  let readme =
    "# My app\n\n" <> heading <> "\n\n```gleam\npub const answer = 42\n```\n"
  // Returns Report(..empty_report(), blocks_checked: 1, problems: [
  //   Problem(file: "README.md", line: 3, reason: MissingGuideBadge),
  // ])
  check.check_readme("README.md", readme, context)
}
pub fn check_release_notes(
  file: String,
  text: String,
  context: CheckContext,
) -> Report

Checks release notes: every fenced code block, as check_markdown does for a guide, so the notes can show code at an older release (see parse_old_code_heading), and the release notes template (see check_release_notes_template). The problems of the template come after the problems of the code blocks.

The blocks belong to the release in the file name, for example 1.1.0 for releases/release-1.1.0.md. Their badge links use that tag. When it is the version in gleam.toml, the blocks are compared with the working tree. When it is an older release, they are compared with the files at its tag, and a diff ends at its tag. A file name with no version belongs to the version in gleam.toml.

Examples

Check release notes Tested snippet

import dream_snippets/check.{
  type Report, CheckContext, GitHubRepository, Project, Version,
}
import gleam/option.{Some}

pub fn check_my_release_notes() -> Report {
  let project =
    Project(
      name: "my_app",
      version: "1.2.0",
      repository: GitHubRepository(owner: "me", name: "my_app"),
      badge_guide: "CONTRIBUTING.md",
      release_notes_guide: "CONTRIBUTING.md",
      guides: ["docs/guides"],
      tested_files: ["test", ".github"],
      dependencies: [],
    )
  let context =
    CheckContext(
      project:,
      checked_from_version: Version(1, 0, 0, []),
      read_source: fn(_path) { Ok("") },
      tag_exists: fn(tag) { Ok(tag == "1.1.0") },
      diff_from_tag: fn(_tag, _path) { Ok("") },
      diff_between_tags: fn(_old_tag, _new_tag, _path) { Ok("") },
      file_at_tag: fn(_tag, _path) { Ok(Some("pub const answer = 41\n")) },
    )
  let heading =
    "### The answer in 1.1 <a href=\"https://github.com/me/my_app/blob/1.1.0/src/app.gleam\"><img align=\"right\" alt=\"Code at 1.1.0\" src=\"https://img.shields.io/badge/%F0%9F%93%9C_code_at-1.1.0-lightgrey.svg?style=flat-square\"></a>"
  let notes =
    "# my_app 1.2.0 Release Notes\n\n**Release Date:** 2026-11-20\n\n"
    <> heading
    <> "\n\n```gleam\npub const answer = 41\n```\n"
  // The old code block passes. The notes have no sections, so it returns
  // Report(..empty_report(), blocks_checked: 1, problems: [
  //   Problem(file, 1, MissingReleaseNotesSection("Key Highlights")),
  //   Problem(file, 1, MissingReleaseNotesSection("What's New")),
  //   Problem(file, 1, MissingReleaseNotesSection("Testing")),
  //   Problem(file, 1, MissingReleaseNotesChoice(["Upgrading", "Installing"])),
  //   Problem(file, 1, MissingReleaseNotesSection("Documentation")),
  //   Problem(file, 1, MissingReleaseNotesSection("Community")),
  // ])
  // where file is "releases/release-1.2.0.md"
  check.check_release_notes("releases/release-1.2.0.md", notes, context)
}
pub fn check_release_notes_template(
  file: String,
  text: String,
) -> List(Problem)

Checks that release notes follow the release notes template.

Headings are read outside code fences only, and trailing HTML after a heading, such as a badge link, is ignored. The rules are:

  • The first line is # <Name> <version> Release Notes, where <version> is the version in the file name and <Name> is any text.
  • A line that starts with **Release Date:** comes after the title and before the first ## section.
  • The ## sections are only those of release_notes_template, each one time at most, in its order, and each required one is there.
  • Each section has the ### headings that the template requires in it.

Parameters

  • file: the path of the release notes, for example releases/release-3.0.0.md. The title must use its version.
  • text: the content of the release notes

Returns

Each problem, sorted by line, with the line where it is, or line 1 for a missing title, release date or section. A missing ### heading is at the line of its ## section. An empty list when the release notes follow the template.

Examples

Find a missing section Tested snippet

import dream_snippets/check.{type Problem}

pub fn check_my_release_notes() -> List(Problem) {
  let release_notes =
    "# My App 1.2.0 Release Notes

**Release Date:** 2026-11-20

## Key Highlights

## What's New

## Testing

## Installing

## Documentation
"
  // Returns [
  //   Problem(
  //     file: "releases/release-1.2.0.md",
  //     line: 1,
  //     reason: MissingReleaseNotesSection("Community"),
  //   ),
  // ]
  check.check_release_notes_template("releases/release-1.2.0.md", release_notes)
}
pub fn classify_fence(info: String) -> BlockKind

Reads the kind of a fenced code block from its info string, the text after the opening backticks or tildes. Every check of a block gets its kind from here, so each block has exactly one kind, and a language that the check does not know is a file block that needs a proof.

The language is the first word, in lowercase. gleam, diff, markdown and md always give their own kind. Any other language with exactly one more word, illustration, gives an illustration. Then bash, sh, shell and console give a command block, text an output block, and every other language a file block. An empty info string gives an unlabelled block.

Examples

Read the kind of each fence Tested snippet

import dream_snippets/check.{type BlockKind}
import gleam/list

pub fn fence_kinds() -> List(BlockKind) {
  // Returns [GleamBlock, CommandBlock("sh"), OutputBlock,
  //   IllustrationBlock("text"), FileBlock("yaml"), UnlabelledBlock]
  list.map(
    ["gleam", "sh", "text", "text illustration", "yaml", ""],
    check.classify_fence,
  )
}
pub fn compare_versions(
  first: Version,
  second: Version,
) -> order.Order

Compares two versions in the order of semver 2.0.0 (section 11 of semver.org): by major, then minor, then patch number. When those are equal, a pre-release comes before its release. Two pre-releases compare their identifiers from left to right: numeric identifiers as numbers, letters in ASCII order, a numeric identifier before an alphanumeric one, and a shorter list before a longer one that starts with it. This is the one place where the check orders versions.

Examples

Compare two versions Tested snippet

import dream_snippets/check.{AlphanumericIdentifier, NumericIdentifier, Version}
import gleam/order.{type Order}

pub fn compare_release_candidate() -> Order {
  let release_candidate =
    Version(2, 0, 0, [AlphanumericIdentifier("rc"), NumericIdentifier(1)])
  // Returns order.Lt, because a pre-release comes before its release
  check.compare_versions(release_candidate, Version(2, 0, 0, []))
}
pub fn describe_problem(
  problem: Problem,
  project: Project,
) -> String

Describes a problem in one line, as file:line: reason. A control character in the file name or the reason is shown in a visible form (see one_line).

Examples

Describe a problem in one line Tested snippet

import dream_snippets/check.{GitHubRepository, Problem, Project, UnclosedFence}

pub fn problem_line() -> String {
  let project =
    Project(
      name: "my_app",
      version: "1.2.0",
      repository: GitHubRepository(owner: "me", name: "my_app"),
      badge_guide: "CONTRIBUTING.md",
      release_notes_guide: "CONTRIBUTING.md",
      guides: ["docs/guides"],
      tested_files: ["test", ".github"],
      dependencies: [],
    )
  // Returns "README.md:14: this code fence has no closing fence"
  check.describe_problem(Problem("README.md", 14, UnclosedFence), project)
}
pub fn describe_reason(
  reason: Reason,
  project: Project,
) -> String

Describes why a code block fails the check.

Examples

Describe why a block fails Tested snippet

import dream_snippets/check.{GitHubRepository, Project, WrongTag}

pub fn wrong_tag_reason() -> String {
  let project =
    Project(
      name: "my_app",
      version: "1.2.0",
      repository: GitHubRepository(owner: "me", name: "my_app"),
      badge_guide: "CONTRIBUTING.md",
      release_notes_guide: "CONTRIBUTING.md",
      guides: ["docs/guides"],
      tested_files: ["test", ".github"],
      dependencies: [],
    )
  // Returns "the badge link uses the tag 1.1.0, but this file must link at the
  // tag 1.2.0 (the docs of my_app use the version in gleam.toml)"
  check.describe_reason(WrongTag(found: "1.1.0", expected: "1.2.0"), project)
}
pub fn empty_report() -> Report

A report with no block and no problem: each count is 0.

Examples

Start with an empty report Tested snippet

import dream_snippets/check

pub fn no_problems() -> Int {
  // Returns 0
  check.empty_report().blocks_checked
}
pub fn heading_anchors(markdown: String) -> List(String)

The anchors of the headings of a Markdown document, in order, as GitHub makes them.

A heading is a line outside code fences that starts with 1 to 6 # and a space. A closing run of # after a space is not part of its text. Each heading gets the anchor heading_slug of its text. When an anchor is already used by an earlier heading, the second heading gets the anchor with -1 after it, the third -2, and so on.

Examples

List the anchors of a document Tested snippet

import dream_snippets/check

pub fn anchors() -> List(String) {
  // Returns ["my-app", "usage", "usage-1"]
  check.heading_anchors("# My app\n\n## Usage\n\n## Usage\n")
}
pub fn heading_slug(heading: String) -> String

The anchor that GitHub gives a heading with this text (one heading, with no -1 for a repeated anchor; see heading_anchors).

The rule:

  1. Remove the spaces at the start and the end of the heading text.
  2. Remove the Markdown and HTML marks. Inline HTML tags, such as a badge link, are removed, and the text between them is kept. The spaces around a tag stay. A link [text](address) keeps its text. Backticks are removed, and the text of a code span is kept as it is. A _ at the start or end of a word outside a code span is an emphasis mark, and is removed.
  3. Change the text to lowercase.
  4. Remove each character that is not a letter, a digit, a space, a hyphen or an underscore. Letters and digits of every script are kept, and so are combining marks.
  5. Change each space to a hyphen. The spaces are not trimmed after the tags are removed, so each space before a badge link at the end of a heading gives a hyphen at the end of the anchor.

For example, The `RequestBody` type gives the-requestbody-type, What's New gives whats-new and HTTP/2 over TLS gives http2-over-tls. Check one document <a href="..."><img ...></a> gives check-one-document-, and a heading with two badge links after its text, each after a space, gets two hyphens at the end.

Examples

Make the anchor of a heading Tested snippet

import dream_snippets/check

pub fn anchor() -> String {
  // Returns "the-requestbody-type"
  check.heading_slug("The `RequestBody` type")
}
pub const illustration_mark: String

The word after the language in the info string of a fence that marks its block as an illustration, as in ```text illustration.

pub fn is_checked_changelog_section(
  name: String,
  checked_from_version: Version,
) -> Bool

Tells whether a changelog section is checked: Unreleased, a version from checked_from_version on, or a name that is not a version.

Examples

Pick the changelog sections to check Tested snippet

import dream_snippets/check.{Version}
import gleam/list

pub fn checked_sections() -> List(Bool) {
  let checked_from_version = Version(1, 0, 0, [])
  // Returns [True, False, True]
  list.map(["Unreleased", "0.9.0", "1.2.0"], check.is_checked_changelog_section(
    _,
    checked_from_version,
  ))
}
pub fn is_checked_release_notes(
  path: String,
  checked_from_version: Version,
) -> Bool

Tells whether a release notes file is checked: its name is release-<version>.md and the version is checked_from_version or later. Versions are compared as numbers, so 10.0.0 is after 3.0.0.

Examples

Pick the release notes to check Tested snippet

import dream_snippets/check.{Version}
import gleam/list

pub fn checked_release_notes() -> List(Bool) {
  let checked_from_version = Version(2, 0, 0, [])
  // Returns [True, False, False]
  list.map(
    ["releases/release-10.0.0.md", "releases/release-1.4.0.md", "README.md"],
    check.is_checked_release_notes(_, checked_from_version),
  )
}
pub fn is_guide_path(path: String, guides: List(String)) -> Bool

Tells whether a path is a .md file under one of the guide directories. A trailing / of a directory is ignored.

Examples

Find the guides Tested snippet

import dream_snippets/check
import gleam/list

pub fn guide_paths() -> List(Bool) {
  // Returns [True, False, False]
  list.map(
    ["docs/guides/hello.md", "docs/hello.md", "docs/guides/hello.txt"],
    check.is_guide_path(_, ["docs/guides/"]),
  )
}
pub fn is_safe_path(path: String) -> Bool

Tells whether a text is a safe repository path to give to git: a path inside the repository (see is_repository_path) made of letters, digits, ., _, - and /, not starting with -.

Examples

Find the safe paths Tested snippet

import dream_snippets/check
import gleam/list

pub fn safe_paths() -> List(Bool) {
  // Returns [True, False, False]
  list.map(["src/app.gleam", "../secret.txt", "-p"], check.is_safe_path)
}
pub fn is_safe_tag(tag: String) -> Bool

Tells whether a text is a safe tag name to give to git: letters, digits, ., _ and -, not starting with - and with no ...

Examples

Find the safe tags Tested snippet

import dream_snippets/check
import gleam/list

pub fn safe_tags() -> List(Bool) {
  // Returns [True, False, False]
  list.map(["1.2.0", "-x", "1..2"], check.is_safe_tag)
}
pub fn is_tested_file(
  path: String,
  tested_files: List(String),
) -> Bool

Tells whether a path is one of the tested_files paths, or a file under one of them. A trailing / of a path in the list is ignored.

Examples

Find the files that a file block can link to Tested snippet

import dream_snippets/check
import gleam/list

pub fn tested_paths() -> List(Bool) {
  // Returns [True, True, False]
  list.map(
    [".github/workflows/ci.yml", "Makefile", "docs/example.yml"],
    check.is_tested_file(_, ["test", ".github/", "Makefile"]),
  )
}
pub fn looks_like_gleam(text: String) -> Bool

Tells whether a line looks like Gleam code.

It matches only forms that the other languages in the docs (shell, TOML, YAML, JavaScript, TypeScript, SQL, Elixir, Gherkin, Markdown, Dockerfile, diagrams) do not have, so that their fences pass:

  • import a/b or import a.{...}. JavaScript imports use quotes and from, and Python imports have no / and no .{.
  • fn name( or pub fn name(. Elixir writes fn x -> and fn(, JavaScript writes function.
  • pub type Name, pub opaque type Name, or type Name with no =. TypeScript type aliases always have =.
  • use name <- .... Rust use lines have no <-.
  • let assert ....
  • |> module.function(, a pipe into a lowercase module. Elixir and F# pipes call capitalised modules (|> Enum.map().

Rust also has fn name(; the docs have no Rust code.

Examples

Find the lines of Gleam code Tested snippet

import dream_snippets/check
import gleam/list

pub fn gleam_lines() -> List(Bool) {
  // Returns [True, False, True]
  list.map(
    ["import gleam/io", "npm install", "pub fn main() {"],
    check.looks_like_gleam,
  )
}
pub fn one_line(text: String) -> String

Writes each control character of a text in a visible form, so that a message is exactly one line: a line feed as \n, a carriage return as \r, a tab as \t, and any other control character (U+0000 to U+001F, U+007F to U+009F) as \u{<hex>}. Every message of the check passes through this function: describe_problem, describe_reason, config.describe_error and dream_snippets.describe_error return their text through it. A text with no control character is returned as it is, so a second pass changes nothing.

Examples

Keep a message on one line Tested snippet

import dream_snippets/check

pub fn visible_line_break() -> String {
  // Returns "found \\n where \" was expected", with a backslash and an n in
  // place of the line break
  check.one_line("found \n where \" was expected")
}
pub fn parse_badge_heading(
  text: String,
) -> Result(String, Reason)

Reads a heading line that ends with the badge, and returns the link address of the badge.

The line is a Markdown heading (# to ######, a space and a title), then badge_start, the address, and exactly badge_end.

Examples

Read the badge of a heading Tested snippet

import dream_snippets/check.{type Reason}

pub fn badge_address() -> Result(String, Reason) {
  // Returns Ok("https://github.com/me/my_app/blob/1.2.0/test/snippets/hello.gleam")
  check.parse_badge_heading(
    "### Say hello <a href=\"https://github.com/me/my_app/blob/1.2.0/test/snippets/hello.gleam\"><img align=\"right\" alt=\"Tested snippet\" src=\"https://img.shields.io/badge/%F0%9F%A7%AA_tested-snippet-lightgreen.svg?style=flat-square\"></a>",
  )
}
pub fn parse_compare_link(
  address: String,
  repository: GitHubRepository,
) -> Result(CompareLink, Reason)

Reads a diff badge link address into a compare link. The address must be https://github.com/<owner>/<name>/compare/<old>...<new>#diff-<hex> for repository, and both tags must be safe tag names (see is_safe_tag).

Examples

Read a compare link Tested snippet

import dream_snippets/check.{type CompareLink, type Reason, GitHubRepository}

pub fn compare_link() -> Result(CompareLink, Reason) {
  // Returns Ok(CompareLink(
  //   old_tag: "1.1.0",
  //   new_tag: "1.2.0",
  //   anchor_digest: "8b96d7e46c8e7f38e04701586133706ffc625db241b80e9c73922c10b89804f8",
  // ))
  check.parse_compare_link(
    "https://github.com/me/my_app/compare/1.1.0...1.2.0#diff-8b96d7e46c8e7f38e04701586133706ffc625db241b80e9c73922c10b89804f8",
    GitHubRepository(owner: "me", name: "my_app"),
  )
}

Errors

NotCompareLink(address) when the address has another form.

pub fn parse_diff_badge_heading(
  text: String,
) -> Result(String, Reason)

Reads a heading line that ends with the diff badge, and returns the link address of the badge. The rules are those of parse_badge_heading, with diff_badge_end in place of badge_end.

Examples

Read the diff badge of a heading Tested snippet

import dream_snippets/check.{type Reason}

pub fn diff_badge_address() -> Result(String, Reason) {
  // Returns Ok("https://github.com/me/my_app/compare/1.1.0...1.2.0#diff-8b96d7e46c8e7f38e04701586133706ffc625db241b80e9c73922c10b89804f8")
  check.parse_diff_badge_heading(
    "### Rename the function <a href=\"https://github.com/me/my_app/compare/1.1.0...1.2.0#diff-8b96d7e46c8e7f38e04701586133706ffc625db241b80e9c73922c10b89804f8\"><img align=\"right\" alt=\"Tested diff\" src=\"https://img.shields.io/badge/%F0%9F%A7%AA_tested-diff-lightgreen.svg?style=flat-square\"></a>",
  )
}
pub fn parse_example_heading(
  text: String,
) -> Result(ExampleHeading, Reason)

Reads the heading of a gleam block: the tested snippet badge, and the guide badge after it, if any.

When the last badge of the heading is a guide badge, the text before it must be a heading that ends with the tested snippet badge (see parse_badge_heading). The guide badge is badge_start, the address, and exactly guide_badge_end. When the last badge is not a guide badge, the whole heading must end with the tested snippet badge, and a guide badge in another place is a BrokenGuideBadge.

Examples

Read both badges of a README heading Tested snippet

import dream_snippets/check.{type ExampleHeading, type Reason}

pub fn example_heading() -> Result(ExampleHeading, Reason) {
  // Returns Ok(ExampleHeading(
  //   snippet_address: "https://github.com/me/my_app/blob/1.2.0/test/snippets/hello.gleam",
  //   guide_badge: GuideBadgeLink(
  //     address: "https://github.com/me/my_app/blob/1.2.0/docs/guides/hello.md#say-hello",
  //     alt: "Guide: Hello",
  //     title: Some("Guide: Hello"),
  //   ),
  // ))
  check.parse_example_heading(
    "### Say hello <a href=\"https://github.com/me/my_app/blob/1.2.0/test/snippets/hello.gleam\"><img align=\"right\" alt=\"Tested snippet\" src=\"https://img.shields.io/badge/%F0%9F%A7%AA_tested-snippet-lightgreen.svg?style=flat-square\"></a> <a href=\"https://github.com/me/my_app/blob/1.2.0/docs/guides/hello.md#say-hello\"><img align=\"right\" alt=\"Guide: Hello\" title=\"Guide: Hello\" src=\"https://img.shields.io/badge/%F0%9F%93%96_read-guide-blue.svg?style=flat-square\"></a>",
  )
}

Errors

The Reason of parse_badge_heading when the tested snippet badge is missing or not in the exact format.

pub fn parse_guide_link(
  address: String,
  repository: GitHubRepository,
) -> Result(GuideLink, Reason)

Reads a guide badge link address into a guide link. The address must be a file in repository on GitHub, as https://github.com/<owner>/<name>/blob/<tag>/<path>#<anchor>, with a path that stays inside the repository (the rule of parse_snippet_link).

Examples

Read a guide link Tested snippet

import dream_snippets/check.{type GuideLink, type Reason, GitHubRepository}

pub fn guide_link() -> Result(GuideLink, Reason) {
  // Returns Ok(GuideLink(
  //   tag: "1.2.0",
  //   path: "docs/guides/hello.md",
  //   anchor: "say-hello",
  // ))
  check.parse_guide_link(
    "https://github.com/me/my_app/blob/1.2.0/docs/guides/hello.md#say-hello",
    GitHubRepository(owner: "me", name: "my_app"),
  )
}

Errors

ForeignGuideLink(address) when the address names another repository or host, or a path out of the repository.

pub fn parse_hunks(diff_text: String) -> List(DiffHunk)

Splits the text of a git diff into its hunks. The lines before the first @@ header (diff --git, index, --- and +++) are left out.

Examples

Split a diff into hunks Tested snippet

import dream_snippets/check.{type DiffHunk}

pub fn hunks() -> List(DiffHunk) {
  let diff_text =
    "diff --git a/src/app.gleam b/src/app.gleam
--- a/src/app.gleam
+++ b/src/app.gleam
@@ -1,3 +1,3 @@
 pub fn name() {
-  \"old\"
+  \"new\"
 }
"
  // Returns [
  //   DiffHunk(header: "@@ -1,3 +1,3 @@", lines: [
  //     " pub fn name() {",
  //     "-  \"old\"",
  //     "+  \"new\"",
  //     " }",
  //   ]),
  // ]
  check.parse_hunks(diff_text)
}
pub fn parse_line_selection(
  anchor: String,
) -> Result(LineSelection, Reason)

Reads a line anchor: an empty string for the whole file, #L<n> for one line, or #L<start>-L<end> for a range. Line numbers are positive and have no leading zero.

Examples

Read a line range Tested snippet

import dream_snippets/check.{type LineSelection, type Reason}

pub fn line_range() -> Result(LineSelection, Reason) {
  // Returns Ok(LineRange(start: 3, end: 5))
  check.parse_line_selection("#L3-L5")
}
pub fn parse_old_code_heading(
  text: String,
  repository: GitHubRepository,
) -> Result(SnippetLink, Reason)

Reads a heading line that ends with the “code at an older release” badge, and returns its link.

The line is a Markdown heading (# to ######, a space and a title), then one badge and nothing after it. The badge links to a file in repository at the old tag, with an optional line range:

### Title <a href="https://github.com/<owner>/<repo>/blob/<old tag>/<path>#L<start>-L<end>"><img align="right" alt="Code at <old tag>" src="https://img.shields.io/badge/%F0%9F%93%9C_code_at-<old tag>-lightgrey.svg?style=flat-square"></a>

In the image address, each - and _ of the tag is doubled, as shields.io needs. The tag in the alt text, in the image address and in the link is the same tag.

Examples

Read the badge of code at an older release Tested snippet

import dream_snippets/check.{type Reason, type SnippetLink, GitHubRepository}

pub fn old_code_link() -> Result(SnippetLink, Reason) {
  // Returns Ok(SnippetLink(
  //   tag: "1.1.0",
  //   path: "src/app.gleam",
  //   anchor: "#L3-L5",
  // ))
  check.parse_old_code_heading(
    "### Say hello in 1.1 <a href=\"https://github.com/me/my_app/blob/1.1.0/src/app.gleam#L3-L5\"><img align=\"right\" alt=\"Code at 1.1.0\" src=\"https://img.shields.io/badge/%F0%9F%93%9C_code_at-1.1.0-lightgrey.svg?style=flat-square\"></a>",
    GitHubRepository(owner: "me", name: "my_app"),
  )
}

Errors

  • MalformedOldCodeBadge when the line is not a heading, or the badge is not in the exact form for the tag of its link
  • OldCodeBadgeWithOtherBadge when the heading has another badge
  • ForeignLink(address) when the link names another repository or host, or a path out of the repository
pub fn parse_release_version(
  text: String,
) -> Result(Version, Nil)

Reads a semver 2.0.0 version: major.minor.patch, then an optional pre-release part after a -, such as -rc.1. The numbers have no leading zero. The pre-release part is one or more identifiers separated by ., each made of letters, digits and -, and an identifier of digits only has no leading zero.

Examples

Read a pre-release version Tested snippet

import dream_snippets/check.{type Version}

pub fn release_candidate_version() -> Result(Version, Nil) {
  // Returns Ok(Version(major: 3, minor: 1, patch: 0, pre_release: [
  //   AlphanumericIdentifier("rc"),
  //   NumericIdentifier(1),
  // ]))
  check.parse_release_version("3.1.0-rc.1")
}
pub fn parse_snippet_link(
  address: String,
  repository: GitHubRepository,
) -> Result(SnippetLink, Reason)

Reads a badge link address into a snippet link. The address must be a file in repository on GitHub, as https://github.com/<owner>/<name>/blob/<tag>/<path>, with a path that stays inside the repository.

Examples

Read a snippet link with a line range Tested snippet

import dream_snippets/check.{type Reason, type SnippetLink, GitHubRepository}

pub fn snippet_link() -> Result(SnippetLink, Reason) {
  // Returns Ok(SnippetLink(
  //   tag: "1.2.0",
  //   path: "test/snippets/hello.gleam",
  //   anchor: "#L3-L5",
  // ))
  check.parse_snippet_link(
    "https://github.com/me/my_app/blob/1.2.0/test/snippets/hello.gleam#L3-L5",
    GitHubRepository(owner: "me", name: "my_app"),
  )
}

Errors

ForeignLink(address) when the address names another repository or host, or a path out of the repository.

pub fn path_digest(path: String) -> String

The SHA-256 hex digest of a repository path. GitHub’s compare view gives each file the anchor #diff- followed by this digest.

Examples

Make the digest of a path Tested snippet

import dream_snippets/check

pub fn digest() -> String {
  // Returns "8b96d7e46c8e7f38e04701586133706ffc625db241b80e9c73922c10b89804f8"
  check.path_digest("src/app.gleam")
}
pub const release_date_prefix: String

The start of the line that gives the release date of release notes.

pub const release_notes_template: List(TemplateSection)

The ## sections of the release notes template, in order. This is the only place that holds the template. check_release_notes_template reads it, and the “Release notes template” guide of this package shows it.

Upgrading is for a release that replaces an earlier one, and must have a ### Migration Guide. Installing is for a first release.

pub const release_notes_title_end: String

The text after the version in the title of release notes. The title is # <Name> <version> Release Notes.

pub fn release_notes_version(
  path: String,
) -> Result(Version, Nil)

Reads the version from the name of a release notes file, for example Version(3, 0, 0, []) from releases/release-3.0.0.md.

Examples

Read the version of release notes Tested snippet

import dream_snippets/check.{type Version}

pub fn version_of_release_notes() -> Result(Version, Nil) {
  // Returns Ok(Version(major: 3, minor: 0, patch: 0, pre_release: []))
  check.release_notes_version("releases/release-3.0.0.md")
}
pub fn release_requirement(release: String) -> String

The requirement that gleam add writes for a release: >= <release> and < <next major>.0.0.

Examples

Write the requirement of a release Tested snippet

import dream_snippets/check

pub fn requirement() -> String {
  // Returns ">= 2.1.0-rc.1 and < 3.0.0"
  check.release_requirement("2.1.0-rc.1")
}
pub fn repository_url(repository: GitHubRepository) -> String

The address of the GitHub repository of a project, for example https://github.com/TrustBound/dream.

Examples

Get the address of a repository Tested snippet

import dream_snippets/check.{GitHubRepository}

pub fn dream_url() -> String {
  // Returns "https://github.com/TrustBound/dream"
  check.repository_url(GitHubRepository(owner: "TrustBound", name: "dream"))
}
pub fn sort_problems(problems: List(Problem)) -> List(Problem)

Sorts problems by file, then by line. Problems on the same line keep their order.

Examples

Sort problems by file and line Tested snippet

import dream_snippets/check.{
  type Problem, MissingBadgeHeading, MissingReleaseDate, Problem, UnclosedFence,
}

pub fn sorted_problems() -> List(Problem) {
  // Returns [
  //   Problem("CHANGELOG.md", 4, UnclosedFence),
  //   Problem("README.md", 2, MissingReleaseDate),
  //   Problem("README.md", 9, MissingBadgeHeading),
  // ]
  check.sort_problems([
    Problem("README.md", 9, MissingBadgeHeading),
    Problem("CHANGELOG.md", 4, UnclosedFence),
    Problem("README.md", 2, MissingReleaseDate),
  ])
}
pub fn summary(report: Report, files: Int) -> String

The last line of the output, for a report of the blocks of files files, for example 412 blocks and 5 diff blocks checked in 230 files, 0 problems; 3 file blocks, 40 command blocks and 52 output blocks checked; 2 illustration blocks. The part before the ; is the line of earlier versions. blocks counts the gleam blocks. A count of 1 takes the singular, as in 1 block.

Examples

Write the summary line Tested snippet

import dream_snippets/check

pub fn summary_line() -> String {
  let report =
    check.Report(
      ..check.empty_report(),
      blocks_checked: 12,
      diff_blocks_checked: 2,
      command_blocks_checked: 1,
      output_blocks_checked: 4,
      problems: [check.Problem("README.md", 3, check.UnlabelledFence)],
    )
  // Returns "12 blocks and 2 diff blocks checked in 9 files, 1 problem; 0 file blocks, 1 command block and 4 output blocks checked; 0 illustration blocks"
  check.summary(report, 9)
}
✨ Search Document