harn-stdlib 0.10.27

Embedded Harn standard library source catalog
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
// std/changelog — pure typed changelog parsing and fragment assembly.
/** A caller-defined changelog category in deterministic display order. */
pub type ChangelogCategory = {slug: string, heading: string}

/** One parsed `<id>.<category>.md` fragment. */
pub type ChangelogFragment = {id: string, category: string, body: string, file?: string}

/** One exact second-level Markdown section. Offsets are UTF-8 byte offsets. */
pub type ChangelogSection = {heading: string, body: string, start: int, end: int}

pub type ChangelogFailureKind = "invalid_category" \
  | "duplicate_category" \
  | "invalid_fragment" \
  | "malformed_filename" \
  | "unknown_category" \
  | "empty_body" \
  | "duplicate_fragment" \
  | "duplicate_heading" \
  | "missing_section"

/** A deterministic validation or parsing failure. */
pub type ChangelogFailure = {kind: ChangelogFailureKind, message: string, value?: string}

pub type ChangelogResult<T> = Result<T, ChangelogFailure>

/** A format-preserving changelog transformation. */
pub type ChangelogMerge = {text: string, changed: bool}

fn __changelog_failure(
  kind: ChangelogFailureKind,
  message: string,
  value: string? = nil,
) -> ChangelogFailure {
  let failure: ChangelogFailure = {kind: kind, message: message}
  if value != nil {
    failure = failure + {value: value}
  }
  return failure
}

fn __changelog_category_heading(category: ChangelogCategory) -> string {
  return "### " + category.heading
}

/**
 * Validate category configuration without changing caller order.
 * @effects: []
 * @errors: []
 */
pub fn changelog_validate_categories(
  categories: list<ChangelogCategory>,
) -> ChangelogResult<list<ChangelogCategory>> {
  let slugs: list<string> = []
  let headings: list<string> = []
  for category in categories {
    const slug = trim(category.slug)
    const heading = trim(category.heading)
    if slug == "" || category.slug != slug || regex_match("^[a-z][a-z0-9_-]*$", slug) == nil {
      return Err(
        __changelog_failure(
          "invalid_category",
          "category slug must be lowercase and non-empty",
          slug,
        ),
      )
    }
    const heading_is_markdown = starts_with(heading, "#")
    const plain_heading = heading != "" && category.heading == heading && !contains(heading, "\n")
      && !heading_is_markdown
    if !plain_heading {
      return Err(
        __changelog_failure(
          "invalid_category",
          "category heading must be plain non-empty text",
          heading,
        ),
      )
    }
    if contains(slugs, slug) {
      return Err(
        __changelog_failure("duplicate_category", "duplicate category slug: " + slug, slug),
      )
    }
    if contains(headings, heading) {
      return Err(
        __changelog_failure(
          "duplicate_category",
          "duplicate category heading: " + heading,
          heading,
        ),
      )
    }
    slugs = slugs.push(slug)
    headings = headings.push(heading)
  }
  return Ok(categories)
}

fn __changelog_category(categories: list<ChangelogCategory>, slug: string) -> ChangelogCategory? {
  for category in categories {
    if category.slug == slug {
      return category
    }
  }
  return nil
}

/**
 * Parse one `<id>.<category>.md` filename and its non-empty body.
 * @effects: []
 * @errors: []
 */
pub fn changelog_parse_fragment(
  filename: string,
  body: string,
  categories: list<ChangelogCategory>,
) -> ChangelogResult<ChangelogFragment> {
  const valid = changelog_validate_categories(categories)
  if is_err(valid) {
    return Err(unwrap_err(valid))
  }
  const name = trim(filename)
  const captures = regex_captures("^([A-Za-z0-9_-]+)\\.([a-z][a-z0-9_-]*)\\.md$", name)
  if len(captures) == 0 {
    return Err(
      __changelog_failure(
        "malformed_filename",
        "fragment filename must match <id>.<category>.md",
        name,
      ),
    )
  }
  const id = captures[0].groups[0]
  const category = captures[0].groups[1]
  if __changelog_category(categories, category) == nil {
    return Err(
      __changelog_failure("unknown_category", "unknown fragment category: " + category, category),
    )
  }
  const normalized_body = trim(body)
  if normalized_body == "" {
    return Err(__changelog_failure("empty_body", "fragment body must be non-empty", name))
  }
  return Ok({id: id, category: category, body: normalized_body, file: name})
}

fn __changelog_is_numeric_id(id: string) -> bool {
  return regex_match("^[0-9]+$", id) != nil
}

fn __changelog_numeric_canonical(id: string) -> string {
  let out = id
  while len(out) > 1 && starts_with(out, "0") {
    out = substring(out, 1, len(out))
  }
  return out
}

fn __changelog_lex_compare(a: string, b: string) -> int {
  if a == b {
    return 0
  }
  return [a, b].sort()[0] == a ? -1 : 1
}

fn __changelog_fragment_compare(a: ChangelogFragment, b: ChangelogFragment) -> int {
  const a_numeric = __changelog_is_numeric_id(a.id)
  const b_numeric = __changelog_is_numeric_id(b.id)
  if a_numeric != b_numeric {
    return a_numeric ? -1 : 1
  }
  if a_numeric {
    const an = __changelog_numeric_canonical(a.id)
    const bn = __changelog_numeric_canonical(b.id)
    if len(an) != len(bn) {
      return len(an) < len(bn) ? -1 : 1
    }
    const numeric_order = __changelog_lex_compare(an, bn)
    if numeric_order != 0 {
      return numeric_order
    }
  }
  const id_order = __changelog_lex_compare(a.id, b.id)
  if id_order != 0 {
    return id_order
  }
  return __changelog_lex_compare(a.file ?? "", b.file ?? "")
}

fn __changelog_insert_fragment(
  sorted: list<ChangelogFragment>,
  fragment: ChangelogFragment,
) -> list<ChangelogFragment> {
  let out: list<ChangelogFragment> = []
  let inserted = false
  for current in sorted {
    if !inserted && __changelog_fragment_compare(fragment, current) < 0 {
      out = out.push(fragment)
      inserted = true
    }
    out = out.push(current)
  }
  return inserted ? out : out.push(fragment)
}

/**
 * Order fragments by category, natural ID, and filename tie-breaker.
 * @effects: []
 * @errors: []
 */
pub fn changelog_order_fragments(
  fragments: list<ChangelogFragment>,
  categories: list<ChangelogCategory>,
) -> ChangelogResult<list<ChangelogFragment>> {
  const valid = changelog_validate_categories(categories)
  if is_err(valid) {
    return Err(unwrap_err(valid))
  }
  let identities: list<string> = []
  for fragment in fragments {
    if regex_match("^[A-Za-z0-9_-]+$", fragment.id) == nil {
      return Err(
        __changelog_failure(
          "invalid_fragment",
          "fragment id must contain only letters, digits, underscore, or hyphen",
          fragment.id,
        ),
      )
    }
    if __changelog_category(categories, fragment.category) == nil {
      return Err(
        __changelog_failure(
          "unknown_category",
          "unknown fragment category: " + fragment.category,
          fragment.category,
        ),
      )
    }
    if trim(fragment.body) == "" {
      return Err(
        __changelog_failure("empty_body", "fragment body must be non-empty", fragment.file),
      )
    }
    const identity = fragment.category + "\n" + fragment.id
    if contains(identities, identity) {
      return Err(
        __changelog_failure(
          "duplicate_fragment",
          "duplicate fragment id in category: " + fragment.id + "." + fragment.category,
          fragment.id,
        ),
      )
    }
    identities = identities.push(identity)
  }
  let ordered: list<ChangelogFragment> = []
  for category in categories {
    let in_category: list<ChangelogFragment> = []
    for fragment in fragments {
      if fragment.category == category.slug {
        in_category = __changelog_insert_fragment(in_category, fragment)
      }
    }
    ordered = ordered + in_category
  }
  return Ok(ordered)
}

fn __changelog_starts_with_bullet(body: string) -> bool {
  for line in split(body, "\n") {
    const content = trim(line)
    if content != "" {
      return starts_with(content, "- ") || starts_with(content, "* ")
    }
  }
  return false
}

/**
 * Normalize prose to a Markdown bullet while preserving existing bullet bodies.
 * @effects: []
 * @errors: []
 */
pub fn changelog_normalize_fragment_body(body: string) -> ChangelogResult<string> {
  const content = trim(body)
  if content == "" {
    return Err(__changelog_failure("empty_body", "fragment body must be non-empty"))
  }
  if __changelog_starts_with_bullet(content) {
    return Ok(content)
  }
  let lines: list<string> = []
  let first = true
  for line in split(content, "\n") {
    const normalized = trim(line)
    if normalized == "" {
      lines = lines.push("")
    } else if first {
      lines = lines.push("- " + normalized)
      first = false
    } else {
      lines = lines.push("  " + normalized)
    }
  }
  return Ok(lines.join("\n"))
}

/**
 * Assemble fragments into deterministic categorized `###` sections.
 * @effects: []
 * @errors: []
 */
pub fn changelog_assemble_fragments(
  fragments: list<ChangelogFragment>,
  categories: list<ChangelogCategory>,
) -> ChangelogResult<string> {
  const ordered = changelog_order_fragments(fragments, categories)
  if is_err(ordered) {
    return Err(unwrap_err(ordered))
  }
  let parts: list<string> = []
  for category in categories {
    let bodies: list<string> = []
    for fragment in unwrap(ordered) {
      if fragment.category == category.slug {
        const normalized = changelog_normalize_fragment_body(fragment.body)
        if is_err(normalized) {
          return Err(unwrap_err(normalized))
        }
        bodies = bodies.push(unwrap(normalized))
      }
    }
    if len(bodies) > 0 {
      parts = parts.push(__changelog_category_heading(category) + "\n\n" + bodies.join("\n"))
    }
  }
  return Ok(parts.join("\n\n"))
}

fn __changelog_fence_marker(line: string) -> string? {
  const content = trim(line)
  if regex_match("^`{3,}.*$", content) != nil {
    return "`"
  }
  if regex_match("^~{3,}.*$", content) != nil {
    return "~"
  }
  return nil
}

fn __changelog_fence_closes(line: string, marker: string) -> bool {
  const content = trim(line)
  const pattern = marker == "`" ? "^`{3,}[ \\t]*$" : "^~{3,}[ \\t]*$"
  return regex_match(pattern, content) != nil
}

fn __changelog_heading(line: string) -> string? {
  const content = ends_with(line, "\r") ? substring(line, 0, len(line) - 1) : line
  if regex_match("^## [^#].*$", content) == nil {
    return nil
  }
  return content
}

fn __changelog_scan_sections(text: string) -> ChangelogResult<list<dict>> {
  const lines = split(text, "\n")
  let scanned: list<dict> = []
  let seen: list<string> = []
  let current = nil
  let char_offset = 0
  let byte_offset = 0
  let fence: string? = nil
  for indexed in lines.enumerate() {
    const line = indexed.value
    const has_newline = indexed.index + 1 < len(lines)
    const newline_width = has_newline ? 1 : 0
    const line_char_end = char_offset + len(line)
    const line_byte_end = byte_offset + bytes_len(bytes_from_string(line))
    const marker = __changelog_fence_marker(line)
    if fence != nil {
      if __changelog_fence_closes(line, fence ?? "") {
        fence = nil
      }
    } else if marker != nil {
      fence = marker
    } else {
      const heading = __changelog_heading(line)
      if heading != nil {
        if contains(seen, heading ?? "") {
          return Err(
            __changelog_failure(
              "duplicate_heading",
              "duplicate changelog heading: " + (heading ?? ""),
              heading,
            ),
          )
        }
        seen = seen.push(heading ?? "")
        if current != nil {
          scanned = scanned.push(current + {end: byte_offset, end_char: char_offset})
        }
        const body_char = line_char_end + newline_width
        current = {
          heading: heading,
          start: byte_offset,
          start_char: char_offset,
          body_char: body_char,
        }
      }
    }
    char_offset = line_char_end + newline_width
    byte_offset = line_byte_end + newline_width
  }
  if current != nil {
    scanned = scanned.push(current + {end: byte_offset, end_char: char_offset})
  }
  return Ok(scanned)
}

/**
 * Parse exact `##` Markdown sections outside fenced code blocks.
 * @effects: []
 * @errors: []
 */
pub fn changelog_parse_sections(text: string) -> ChangelogResult<list<ChangelogSection>> {
  const scanned = __changelog_scan_sections(text)
  if is_err(scanned) {
    return Err(unwrap_err(scanned))
  }
  let sections: list<ChangelogSection> = []
  for section in unwrap(scanned) {
    sections = sections.push(
      {
        heading: section.heading,
        body: trim(substring(text, section.body_char, section.end_char)),
        start: section.start,
        end: section.end,
      },
    )
  }
  return Ok(sections)
}

fn __changelog_full_heading(heading: string) -> string {
  const normalized = trim(heading)
  return starts_with(normalized, "## ") ? normalized : "## " + normalized
}

/**
 * Find one named section, returning an explicit missing-section failure.
 * @effects: []
 * @errors: []
 */
pub fn changelog_find_section(text: string, heading: string) -> ChangelogResult<ChangelogSection> {
  const sections = changelog_parse_sections(text)
  if is_err(sections) {
    return Err(unwrap_err(sections))
  }
  const target = __changelog_full_heading(heading)
  for section in unwrap(sections) {
    if section.heading == target {
      return Ok(section)
    }
  }
  return Err(__changelog_failure("missing_section", "missing changelog section: " + target, target))
}

fn __changelog_parse_subsections(body: string) -> list<dict> {
  let sections: list<dict> = []
  let heading = ""
  let lines: list<string> = []
  for line in split(trim(body), "\n") {
    const normalized = ends_with(line, "\r") ? substring(line, 0, len(line) - 1) : line
    if regex_match("^### [^#].*$", normalized) != nil {
      if heading != "" || trim(lines.join("\n")) != "" {
        sections = sections.push({heading: heading, body: trim(lines.join("\n"))})
      }
      heading = normalized
      lines = []
    } else {
      lines = lines.push(normalized)
    }
  }
  if heading != "" || trim(lines.join("\n")) != "" {
    sections = sections.push({heading: heading, body: trim(lines.join("\n"))})
  }
  return sections
}

fn __changelog_append_unique(current: string, addition: string) -> string {
  const left = trim(current)
  const right = trim(addition)
  const contains_block = contains("\n\n" + left + "\n\n", "\n\n" + right + "\n\n")
  if right == "" || left == right || contains_block {
    return left
  }
  return left == "" ? right : left + "\n\n" + right
}

fn __changelog_merge_bodies(
  current: string,
  addition: string,
  categories: list<ChangelogCategory>,
) -> string {
  let existing = __changelog_parse_subsections(current)
  const incoming = __changelog_parse_subsections(addition)
  let preamble = ""
  for section in existing {
    if section.heading == "" {
      preamble = __changelog_append_unique(preamble, section.body)
    }
  }
  for added in incoming {
    if added.heading == "" {
      preamble = __changelog_append_unique(preamble, added.body)
      continue
    }
    let merged = false
    let next: list<dict> = []
    for section in existing {
      if !merged && section.heading == added.heading {
        next = next.push(
          {heading: section.heading, body: __changelog_append_unique(section.body, added.body)},
        )
        merged = true
      } else {
        next = next.push(section)
      }
    }
    if !merged {
      next = next.push(added)
    }
    existing = next
  }
  let rendered: list<string> = []
  if preamble != "" {
    rendered = rendered.push(preamble)
  }
  let emitted: list<string> = []
  for category in categories {
    const category_heading = __changelog_category_heading(category)
    let category_body = ""
    for section in existing {
      if section.heading == category_heading {
        category_body = __changelog_append_unique(category_body, section.body)
      }
    }
    if category_body != "" {
      rendered = rendered.push(category_heading + "\n\n" + category_body)
      emitted = emitted.push(category_heading)
    }
  }
  for section in existing {
    if section.heading != "" && !contains(emitted, section.heading) {
      let section_body = ""
      for matching in existing {
        if matching.heading == section.heading {
          section_body = __changelog_append_unique(section_body, matching.body)
        }
      }
      rendered = rendered.push(section.heading + "\n\n" + section_body)
      emitted = emitted.push(section.heading)
    }
  }
  return rendered.join("\n\n")
}

fn __changelog_newline(text: string) -> string {
  return contains(text, "\r\n") ? "\r\n" : "\n"
}

fn __changelog_with_newlines(text: string, newline: string) -> string {
  return newline == "\r\n" ? replace(text, "\n", "\r\n") : text
}

/**
 * Merge assembled fragments into `## Unreleased`, preserving operator content.
 * @effects: []
 * @errors: []
 */
pub fn changelog_merge_unreleased(
  text: string,
  assembled: string,
  categories: list<ChangelogCategory>,
) -> ChangelogResult<ChangelogMerge> {
  const valid = changelog_validate_categories(categories)
  if is_err(valid) {
    return Err(unwrap_err(valid))
  }
  const addition = trim(replace(assembled, "\r\n", "\n"))
  if addition == "" {
    return Ok({text: text, changed: false})
  }
  const scanned = __changelog_scan_sections(text)
  if is_err(scanned) {
    return Err(unwrap_err(scanned))
  }
  const newline = __changelog_newline(text)
  let unreleased = nil
  for section in unwrap(scanned) {
    if section.heading == "## Unreleased" {
      unreleased = section
    }
  }
  if unreleased != nil {
    const current = replace(
      substring(text, unreleased.body_char, unreleased.end_char),
      "\r\n",
      "\n",
    )
    const merged = __changelog_merge_bodies(current, addition, categories)
    if trim(current) == merged {
      return Ok({text: text, changed: false})
    }
    const terminal = unreleased.end_char == len(text) && ends_with(text, "\n")
    let block = "## Unreleased\n\n" + merged
    if unreleased.end_char < len(text) {
      block = block + "\n\n"
    } else if terminal {
      block = block + "\n"
    }
    const normalized_block = __changelog_with_newlines(block, newline)
    const updated = substring(text, 0, unreleased.start_char)
      + normalized_block
      + substring(text, unreleased.end_char, len(text))
    return Ok({text: updated, changed: updated != text})
  }
  const sections = unwrap(scanned)
  const insert_char = len(sections) == 0 ? len(text) : sections[0].start_char
  const prefix = substring(text, 0, insert_char)
  const suffix = substring(text, insert_char, len(text))
  const before = if prefix == "" || ends_with(prefix, newline + newline) {
    ""
  } else if ends_with(prefix, newline) {
    newline
  } else {
    newline + newline
  }
  const after = suffix == "" ? "" : newline + newline
  const block = __changelog_with_newlines("## Unreleased\n\n" + addition, newline)
  const updated = prefix + before + block + after + suffix
  return Ok({text: updated, changed: true})
}