Skip to content

Tracklist API

import { applyEdits, parseTracklist } from "trackparse/tracklist";
// CommonJS: const { applyEdits, parseTracklist } = require("trackparse/tracklist");

Neither function fails on string input. What to know for each language:

  • JavaScript (trackparse on npm). The functions live in the entry trackparse/tracklist, so code that only uses parse never loads them. Results are plain objects, and JSON.stringify gives the spec's JSON.
  • Python (trackparse on PyPI). Results are frozen dataclasses with snake_case attributes (spec_version, first_time) and a to_dict() that returns the spec's JSON shape. Edits are plain dicts with the same keys as in JavaScript, "from" included. A text that isn't a str raises TypeError.
  • Rust (trackparse::tracklist on docs.rs). Results are structs with public snake_case fields and to_json(). Edits are values of the Edit enum (listed under Types). Options go through the _with functions, and nothing panics.
  • Go (pkg.go.dev). The functions are in the package trackparse itself, with no separate import. Results are structs with exported fields (SpecVersion, FirstTime) whose MarshalJSON writes the spec's JSON. Edits are Edit structs made with a constructor per edit (listed under Types). Options go through the With functions, and nothing panics.
  • C (trackparse.h). The functions are in the same header as trackparse_parse. Text is UTF-8 with a byte length (or TRACKPARSE_NUL_TERMINATED), and results are read-only structs that you free with trackparse_tracklist_free and trackparse_edit_result_free. Edits go in a trackparse_edits handle, filled by one function per edit (listed under Types). trackparse_apply_edits takes no options, since edits don't depend on them. Every function returns NULL only when memory runs out.

The guides Tracklists and Editing tracklists walk through examples. The rules are R13 in the spec.

(Python: parse_tracklist. Rust: parse_tracklist(text) and parse_tracklist_with(text, &options). Go: ParseTracklist(text) and ParseTracklistWith(text, options). C: trackparse_parse_tracklist(s, len) and trackparse_parse_tracklist_with(s, len, options))

Returns a Tracklist (Tracklist in Rust, Tracklist in Go, a trackparse_tracklist * in C). Every key is always present (R13.8). The playground shows every entry of a list as a table, with its JSON.

{
specVersion: string;
entries: TracklistEntry[];
warnings: TracklistWarning[];
stats: { lines, tracks, markers, timed, firstTime, lastTime };
}

One per line, in order. The table uses the JavaScript and JSON types. Python has None for null. In Rust (TracklistEntry), line is a usize, kind and eol are the enums EntryKind and Eol (as_str() gives the strings below), time is an Option<TimeValue> (a TimeValue) whose seconds is an f64, and track is an Option<ParsedTrack>. Go calls the type Entry: Kind and Eol are string types (EntryKind, Eol) whose values are the strings below, Time is a *TracklistTime whose Seconds is a float64, and Track is a *Track, nil where the table says null. C calls it trackparse_entry: line is an int, kind and eol are the enums trackparse_entry_kind and trackparse_eol (their _name() functions give the strings below), time is a const trackparse_tracklist_time * whose seconds is a double, and track is a const trackparse_track *, NULL where the table says null.

Field Type Description
line number 1-based
kind "track" | "marker" | "header" | "link" | "note" | "blank" See line kinds (R13.3)
eol "\n" | "\r\n" | "\r" | "" The line's own terminator. "" on a last line without one (R13.1)
raw string The line exactly as written, never normalized
parts { lead, time, gap, rest } raw cut into four strings that join back into it (R13.1.3)
time { raw, seconds } | null The start time. See Times (R13.2)
track ParsedTrack | null Parsed from rest in tracklist mode, without its Markdown emphasis, a leading w/ and a bare track number (R13.4). Only track and marker lines have one
layered boolean The track text (after any time) starts with w/ and a space: a layered track, played over the previous one (R13.13)
{ code: TracklistWarningCode; line: number; severity: "info" | "warning"; suggestion: { line, raw, seconds } | null }

Sorted by line (R13.7). The codes are listed under Warnings, and Suggestions explains suggestion. In Rust the type is TracklistWarning with a WarningCode, in Go TracklistWarning with a WarningCode, in C trackparse_tracklist_warning with a trackparse_tracklist_warning_code (TRACKPARSE_TL_TIME_OUT_OF_ORDER and so on). Warnings from each line's own ParsedTrack stay on entries[i].track.warnings and aren't repeated here.

Field
lines number of entries
tracks, markers entries of those kinds
timed entries with a time
firstTime, lastTime seconds of the first and last time, or null

Python and Rust call the last two first_time and last_time. In Rust the counts are usize and the two times are Option<f64> (Stats). In Go (Stats) the fields are FirstTime and LastTime, of type *float64. In C (trackparse_stats) the counts are int and first_time and last_time are const double *, NULL when there is no time.

(Python: apply_edits. Rust: apply_edits(text, &edits) and apply_edits_with(text, &edits, &options). Go: ApplyEdits(text, edits) and ApplyEditsWith(text, edits, options). C: trackparse_apply_edits(s, len, edits))

Returns { text, changed, warnings } (Python, Rust and Go: an EditResult; C: a trackparse_edit_result *).

Field
text the edited text
changed final line numbers of every line an edit wrote, ascending
warnings { code, edit, line }[]: edit is the index in edits, line may be null

In Rust (EditResult), changed is a Vec<usize> and each warning is an EditWarning { code: EditWarningCode, edit: usize, line: Option<usize> } (EditWarning). In Go (EditResult), Changed is an []int and each warning is an EditWarning{Code EditWarningCode, Edit int, Line *int} (EditWarning). In C, changed is a const long long * with changed_count, and each warning is a trackparse_edit_warning with a code, a size_t edit and a const long long *line. The edit rules are R13.9.

JavaScript and Python Effect
{ op: "set-time", line, seconds } Rewrite the time on that line
{ op: "shift", seconds } Add seconds to every timed line
{ op: "shift", seconds, from } Add seconds to every timed line from from on
{ op: "shift", seconds, lines } Add seconds to the listed lines
{ op: "sort-by-time" } Stable sort by time. Untimed lines move with the timed line above them
{ op: "set-rest", line, rest } Replace everything after the time
{ op: "insert", after, text } Insert a line after after (0 = at the top)
{ op: "delete", line } Remove a line

Pass the objects in the table as an array: applyEdits(text, [{ op: "shift", seconds: 45, from: 5 }]).

With the serde feature, the Rust Edit reads and writes the JSON of the JavaScript objects. The Go Edit writes it with json.Marshal.

Edits run in sequence, each on the result of the one before. Times are written in their line's style (R13.9.2), up to 100 hours (or 999 minutes for lists that never use hours). Editing tracklists has examples of each edit.

Code When
style-upgraded a time without hours passed one hour and gained them
time-clamped a time would go below zero and was set to zero
edit-invalid the edit could not apply and changed nothing

In Rust the codes are EditWarningCode::StyleUpgraded, TimeClamped and EditInvalid. In Go they are EditWarningCodeStyleUpgraded, EditWarningCodeTimeClamped and EditWarningCodeEditInvalid. In C they are TRACKPARSE_EDIT_WARNING_STYLE_UPGRADED, TRACKPARSE_EDIT_WARNING_TIME_CLAMPED and TRACKPARSE_EDIT_WARNING_EDIT_INVALID. Warnings shows how to check them.

Both functions take the same options (R13.10). applyEdits accepts them for symmetry; edits don't depend on them, and the C trackparse_apply_edits has no options argument.

JavaScript Python and Rust
Go
Default Description
mode mode
Mode
"tracklist" Mode for each line's parse(). See Tracklist mode
knownArtists known_artists
KnownArtists
empty As for parse()
splitAnd split_and
SplitAnd
"auto" As for parse()
keywords keywords
Keywords
none As for parse()
audioSeconds audio_seconds
AudioSeconds
none Length of the audio. Enables time-after-audio-end
markerWords marker_words
MarkerWords
empty Extra marker words

The C setters are named after the option, after trackparse_tracklist_options_: set_mode, set_split_and and set_audio_seconds take a value, and the list options take one entry per call, named in the singular: add_known_artist and add_marker_word. keywords is five setters, as on the parse options: add_version_head, add_descriptor, add_genre, add_junk and add_feat_marker.

Python uses keyword arguments. Rust has a setter of the same name for each on TracklistOptions, for example TracklistOptions::new().audio_seconds(3600.0).marker_words(["drop"]), and passes it to parse_tracklist_with or apply_edits_with. Go takes a TracklistOptions struct, for example trackparse.TracklistOptions{AudioSeconds: trackparse.Ptr(3600.0), MarkerWords: []string{"drop"}}, in ParseTracklistWith or ApplyEditsWith. Its zero value is the defaults: an empty Mode means ModeTracklist (ModeAuto asks for auto), and a nil AudioSeconds means no length, while trackparse.Ptr(0.0) is an audio of length 0. C takes a trackparse_tracklist_options handle from trackparse_tracklist_options_new() in trackparse_parse_tracklist_with; an option never set keeps its default, so an audio length never set is none and trackparse_tracklist_options_set_audio_seconds(o, 0) is an audio of length 0. Tracklists has a full example.

Exported from trackparse/tracklist: Tracklist, TracklistEntry, EntryKind, EntryParts, Eol, TracklistTime, TimeSuggestion, TracklistStats, TracklistWarning, TracklistWarningCode, TracklistOptions, Edit, EditResult, EditWarning, EditWarningCode.

The JSON Schema is tracklist.schema.json. The Rust items are also on docs.rs, and the Go items on pkg.go.dev.