Module: Browserctl::FormatVersion

Defined in:
lib/browserctl/format_version.rb

Overview

Format-version header convention.

Every persisted browserctl artifact declares its format version on the very first line as ‘version: <int>`. This module is the convention’s single source of truth; per-format adoption (bundle, recording, workflow) lands in later WS-1 PRs. See ‘docs/reference/format-versions.md`.

Constant Summary collapse

HEADER_RE =
/\Aversion:\s*(\d+)\s*\z/

Class Method Summary collapse

Class Method Details

.parse(io_or_string) ⇒ Object

Parse the version header from an IO or String. Returns the Integer version. Raises Browserctl::ProtocolMismatch if the header is missing or malformed.

Raises:



20
21
22
23
24
25
26
27
28
# File 'lib/browserctl/format_version.rb', line 20

def parse(io_or_string)
  first_line = io_or_string.respond_to?(:gets) ? io_or_string.gets : io_or_string.to_s.each_line.first
  raise ProtocolMismatch, "missing version header" if first_line.nil?

  match = HEADER_RE.match(first_line.chomp)
  raise ProtocolMismatch, "malformed version header: #{first_line.inspect}" unless match

  Integer(match[1])
end

.stamp(version:) ⇒ Object

Returns the canonical header string for a given integer version.

Raises:

  • (ArgumentError)


31
32
33
34
35
# File 'lib/browserctl/format_version.rb', line 31

def stamp(version:)
  raise ArgumentError, "version must be a non-negative Integer" unless version.is_a?(Integer) && version >= 0

  "version: #{version}\n"
end