asic-rs 0.5.4

Simple ASIC management in Rust
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
#!/usr/bin/env python3
"""Generate the supported devices documentation page from Rust source."""

from __future__ import annotations

import argparse
import re
import sys
from dataclasses import dataclass
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parent.parent.parent
DEFAULT_OUTPUT = REPO_ROOT / "docs" / "supported-devices.md"

SUPPORT_COLUMNS = [
    ("SupportsPoolsConfig", "supports_pools_config", "Pools Config"),
    ("SupportsScalingConfig", "supports_scaling_config", "Scaling Config"),
    ("SupportsTuningConfig", "supports_tuning_config", "Tuning Config"),
    ("SupportsFanConfig", "supports_fan_config", "Fan Config"),
    ("SetFaultLight", "supports_set_fault_light", "Light"),
    ("SetPowerLimit", "supports_set_power_limit", "Power Limit"),
    ("Restart", "supports_restart", "Restart"),
    ("Resume", "supports_resume", "Pause/Resume"),
    ("UpgradeFirmware", "supports_upgrade_firmware", "Upgrade FW"),
    ("ChangePassword", "supports_change_password", "Change PWD"),
    ("FactoryReset", "supports_factory_reset", "Factory Reset"),
    ("ReadLogs", "supports_read_logs", "Read Logs"),
]

SUPPORT_TRAITS = {trait for trait, _, _ in SUPPORT_COLUMNS}


@dataclass(frozen=True)
class SupportRow:
    make: str
    firmware: str
    backend: str
    support: dict[str, str]


@dataclass(frozen=True)
class FirmwareSupportRow:
    firmware: str
    support: dict[str, str]


@dataclass(frozen=True)
class ModelEntry:
    variant: str
    names: tuple[str, ...]


@dataclass(frozen=True)
class MakeModels:
    make: str
    models: tuple[ModelEntry, ...]


@dataclass(frozen=True)
class ModelFamily:
    name: str
    models: tuple[ModelEntry, ...]


def read_text(path: Path) -> str:
    return path.read_text(encoding="utf-8")


def find_matching_brace(text: str, open_brace: int) -> int:
    depth = 0
    in_string = False
    escape = False
    line_comment = False
    block_comment = False

    for idx in range(open_brace, len(text)):
        char = text[idx]
        next_char = text[idx + 1] if idx + 1 < len(text) else ""

        if line_comment:
            if char == "\n":
                line_comment = False
            continue

        if block_comment:
            if char == "*" and next_char == "/":
                block_comment = False
            continue

        if in_string:
            if escape:
                escape = False
            elif char == "\\":
                escape = True
            elif char == '"':
                in_string = False
            continue

        if char == "/" and next_char == "/":
            line_comment = True
            continue
        if char == "/" and next_char == "*":
            block_comment = True
            continue
        if char == '"':
            in_string = True
            continue
        if char == "{":
            depth += 1
            continue
        if char == "}":
            depth -= 1
            if depth == 0:
                return idx

    raise ValueError("unmatched brace")


def strip_rust_comments(text: str) -> str:
    text = re.sub(r"//.*", "", text)
    return re.sub(r"/\*.*?\*/", "", text, flags=re.S)


def normalized_bool_body(body: str) -> str:
    body = strip_rust_comments(body).strip()
    body = body.removesuffix(";").strip()
    return " ".join(body.split())


def classify_support_body(body: str) -> str:
    normalized = normalized_bool_body(body)
    if normalized in {"true", "return true"}:
        return "Yes"
    if normalized in {"false", "return false"}:
        return "No"
    return "Conditional"


def extract_fn_body(block: str, method: str) -> str | None:
    match = re.search(
        rf"\bfn\s+{re.escape(method)}\s*\([^)]*\)\s*->\s*bool\s*\{{",
        block,
        flags=re.S,
    )
    if not match:
        return None

    open_brace = match.end() - 1
    close_brace = find_matching_brace(block, open_brace)
    return block[open_brace + 1: close_brace]


def iter_impl_blocks(text: str) -> tuple[tuple[str, str, str], ...]:
    blocks: list[tuple[str, str, str]] = []
    pattern = re.compile(
        r"\bimpl\s+(?P<trait>[A-Za-z_][A-Za-z0-9_]*)\s+for\s+"
        r"(?P<target>[A-Za-z_][A-Za-z0-9_]*)\s*\{"
    )

    for match in pattern.finditer(text):
        open_brace = match.end() - 1
        close_brace = find_matching_brace(text, open_brace)
        blocks.append(
            (
                match.group("trait"),
                match.group("target"),
                text[open_brace + 1: close_brace],
            )
        )

    return tuple(blocks)


def collect_backend_supports() -> dict[str, dict[str, str]]:
    supports: dict[str, dict[str, str]] = {}
    firmwares_dir = REPO_ROOT / "asic-rs-firmwares"

    for path in firmwares_dir.rglob("*.rs"):
        text = read_text(path)
        for trait, target, block in iter_impl_blocks(text):
            if trait not in SUPPORT_TRAITS:
                continue

            method = next(
                method_name
                for trait_name, method_name, _ in SUPPORT_COLUMNS
                if trait_name == trait
            )
            body = extract_fn_body(block, method)
            value = "No" if body is None else classify_support_body(body)
            supports.setdefault(target, {})[trait] = value

    return supports


def parse_display_string(path: Path) -> str | None:
    if not path.exists():
        return None

    match = re.search(r'write!\(\s*f\s*,\s*"([^"]+)"\s*\)', read_text(path))
    return match.group(1) if match else None


def parse_make_crate_names(firmware_rs: Path) -> tuple[str, ...]:
    text = read_text(firmware_rs)
    names = re.findall(r"asic_rs_makes_([A-Za-z0-9_]+)::make::", text)
    names.extend(
        re.findall(r"asic_rs_makes_([A-Za-z0-9_]+)::\{[^}]*\bmake::", text, flags=re.S)
    )
    return tuple(dict.fromkeys(names))


def make_display_name(make_crate: str) -> str:
    path = REPO_ROOT / "asic-rs-makes" / make_crate / "src" / "make.rs"
    return parse_display_string(path) or make_crate.replace("_", " ").title()


def make_display_names(make_crates: tuple[str, ...]) -> str:
    if not make_crates:
        return "Unknown"

    return ", ".join(make_display_name(make_crate) for make_crate in make_crates)


def concrete_backend_structs(
        firmware_crate: Path, supports: dict[str, dict[str, str]]
) -> list[str]:
    backend_dir = firmware_crate / "src" / "backends"
    if not backend_dir.exists():
        return []

    structs: list[str] = []
    for path in sorted(backend_dir.rglob("*.rs")):
        if path == backend_dir / "mod.rs":
            continue

        text = read_text(path)
        for match in re.finditer(
                r"\bpub(?:\(crate\))?\s+struct\s+([A-Za-z_][A-Za-z0-9_]*)\s*\{",
                text,
        ):
            name = match.group(1)
            if name in supports:
                structs.append(name)

    return sorted(set(structs), key=str.casefold)


def collect_support_rows() -> tuple[SupportRow, ...]:
    supports = collect_backend_supports()
    rows: list[SupportRow] = []

    for crate in sorted((REPO_ROOT / "asic-rs-firmwares").iterdir()):
        firmware_rs = crate / "src" / "firmware.rs"
        if not firmware_rs.exists():
            continue

        make = make_display_names(parse_make_crate_names(firmware_rs))
        firmware = parse_display_string(firmware_rs) or crate.name

        for backend in concrete_backend_structs(crate, supports):
            values = {
                trait: supports.get(backend, {}).get(trait, "No")
                for trait, _, _ in SUPPORT_COLUMNS
            }
            rows.append(
                SupportRow(make=make, firmware=firmware, backend=backend, support=values)
            )

    return tuple(
        sorted(rows, key=lambda r: (r.make.casefold(), r.firmware.casefold(), r.backend))
    )


def support_icon(values: tuple[str, ...]) -> str:
    if all(value == "Yes" for value in values):
        return ":lucide-check-check:"
    if all(value == "No" for value in values):
        return ":lucide-x:"
    return ":lucide-list-todo:"


def collect_firmware_support_rows() -> tuple[FirmwareSupportRow, ...]:
    grouped: dict[str, list[SupportRow]] = {}

    for row in collect_support_rows():
        grouped.setdefault(row.firmware, []).append(row)

    rows: list[FirmwareSupportRow] = []
    for firmware, support_rows in grouped.items():
        rows.append(
            FirmwareSupportRow(
                firmware=firmware,
                support={
                    trait: support_icon(
                        tuple(row.support[trait] for row in support_rows)
                    )
                    for trait, _, _ in SUPPORT_COLUMNS
                },
            )
        )

    return tuple(sorted(rows, key=lambda r: r.firmware.casefold()))


def enum_body(text: str) -> str | None:
    match = re.search(r"\bpub\s+enum\s+[A-Za-z_][A-Za-z0-9_]*\s*\{", text)
    if not match:
        return None

    open_brace = match.end() - 1
    close_brace = find_matching_brace(text, open_brace)
    return text[open_brace + 1: close_brace]


def parse_models(models_rs: Path) -> tuple[ModelEntry, ...]:
    body = enum_body(read_text(models_rs))
    if body is None:
        return ()

    models: list[ModelEntry] = []
    pending_attrs: list[str] = []

    for raw_line in body.splitlines():
        line = raw_line.strip()
        if not line or line.startswith("//"):
            continue

        if line.startswith("#["):
            pending_attrs.append(line)
            continue

        match = re.match(r"([A-Za-z_][A-Za-z0-9_]*)(?:\([^)]*\))?\s*,", line)
        if not match:
            continue

        variant = match.group(1)
        attrs = "\n".join(pending_attrs)
        pending_attrs.clear()

        if variant == "Unknown":
            continue

        names = tuple(
            dict.fromkeys(
                re.findall(r'(?:alias|rename)\s*=\s*"([^"]+)"', attrs) or [variant]
            )
        )
        models.append(ModelEntry(variant=variant, names=names))

    return tuple(models)


def collect_make_models() -> tuple[MakeModels, ...]:
    makes: list[MakeModels] = []

    for crate in sorted((REPO_ROOT / "asic-rs-makes").iterdir()):
        models_rs = crate / "src" / "models.rs"
        if not models_rs.exists():
            continue

        make = parse_display_string(crate / "src" / "make.rs") or crate.name.replace(
            "_", " "
        ).title()
        models = parse_models(models_rs)
        if models:
            makes.append(MakeModels(make=make, models=models))

    return tuple(sorted(makes, key=lambda item: item.make.casefold()))


def table_escape(value: str) -> str:
    return value.replace("|", r"\|")


def natural_sort_key(value: str) -> tuple[object, ...]:
    parts = re.split(r"(\d+)", value.casefold())
    return tuple(int(part) if part.isdigit() else part for part in parts)


def slugify(value: str) -> str:
    slug = re.sub(r"[^a-z0-9]+", "-", value.casefold()).strip("-")
    return slug or "models"


def support_matrix_markdown(rows: tuple[FirmwareSupportRow, ...]) -> str:
    headers = ["Firmware type"]
    for column in SUPPORT_COLUMNS:
        _trait, _prop, label = column
        headers.append(label)

    lines = [
        "| " + " | ".join(table_escape(header) for header in headers) + " |",
        "| " + " | ".join("---" for _ in headers) + " |",
    ]
    for row in rows:
        values = [row.firmware]
        for trait, _, _ in SUPPORT_COLUMNS:
            values.append(row.support[trait])
        lines.append("| " + " | ".join(table_escape(value) for value in values) + " |")

    return "\n".join(lines)


def model_family_name(model: ModelEntry) -> str:
    text = model.names[0].replace("_", " ").upper()
    tokens = re.findall(r"[A-Z]*\d[A-Z0-9+.-]*|[A-Z]+", text)
    digit_index = next(
        (idx for idx, token in enumerate(tokens) if any(ch.isdigit() for ch in token)),
        None,
    )
    token = tokens[digit_index] if digit_index is not None else ""
    if not token:
        return tokens[0] if tokens else model.variant

    token = token.strip(".-")
    if match := re.match(r"^([A-Z]+)(\d+)", token):
        return f"{match.group(1)}{match.group(2)}"
    if match := re.match(r"^(\d+)", token):
        if digit_index and (previous := tokens[digit_index - 1]).isalpha():
            if len(previous) <= 5:
                return previous
        digits = match.group(1)
        if len(digits) >= 3:
            return f"{digits[:-2]}xx"
        return digits

    return token


def group_model_families(models: tuple[ModelEntry, ...]) -> tuple[ModelFamily, ...]:
    grouped: dict[str, list[ModelEntry]] = {}

    for model in models:
        grouped.setdefault(model_family_name(model), []).append(model)

    return tuple(
        ModelFamily(
            name=name,
            models=tuple(sorted(items, key=lambda item: natural_sort_key(item.names[0]))),
        )
        for name, items in sorted(grouped.items(), key=lambda item: natural_sort_key(item[0]))
    )


def family_summary_text(families: tuple[ModelFamily, ...]) -> str:
    family_label = "family" if len(families) == 1 else "families"
    visible = ", ".join(f"`{family.name}`" for family in families[:8])
    if len(families) > 8:
        visible = f"{visible}, ..."
    return f"{len(families)} {family_label}: {visible}"

def model_display(model: ModelEntry) -> str:
    primary, *aliases = model.names
    if aliases:
        alias_text = ", ".join(f"`{alias}`" for alias in aliases)
        return f"`{primary}` (also: {alias_text})"
    return f"`{primary}`"


def wrapped_inline_models(models: tuple[ModelEntry, ...], line_width: int = 120) -> str:
    lines: list[str] = []
    for model in models:
        lines.append(f"\t\t - [x] {model_display(model)}")
    return "\n".join(lines)


def exact_models_markdown(makes: tuple[MakeModels, ...]) -> str:
    lines: list[str] = []

    for make in makes:
        families = group_model_families(make.models)
        count = len(make.models)
        model_label = "model" if count == 1 else "models"
        family_label = "family" if len(families) == 1 else "families"
        lines.extend(
            [
                (
                    f"??? quote \"{make.make} ({count} {model_label} across "
                    f"{len(families)} {family_label})\""
                ),
                "",
            ]
        )

        for family in families:
            family_count = len(family.models)
            family_model_label = "model" if family_count == 1 else "models"
            lines.extend(
                [
                    (
                        f"\t??? note \"{family.name} family "
                        f"({family_count} {family_model_label})\""
                    ),
                    wrapped_inline_models(family.models),
                ]
            )

    return "\n".join(lines).rstrip()


def build_markdown() -> str:
    rows = collect_firmware_support_rows()
    makes = collect_make_models()

    return (
            "\n\n".join(
                [
                    "# Supported Devices",
                    (
                        "<!-- Generated by scripts/generate_supported_devices.py; "
                        "do not edit manually. -->"
                    ),
                    (
                        "This page is generated from the Rust source. The support matrix is "
                        "derived from each backend's `supports_...` methods; model lists are "
                        "derived from the make model enums."
                    ),
                    (
                        "Legend:\n\n"
                        " - :lucide-check-check: means every backend subtype for that firmware type supports the function\n"
                        " - :lucide-list-todo: means support is mixed or conditional\n"
                        " - :lucide-x: means no backend subtype supports it\n"
                    ),
                    "## Support Matrix\n\n" + support_matrix_markdown(rows),
                    "## Exact Supported Models\n\n" + exact_models_markdown(makes),
                    ]
            )
            + "\n"
    )


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--output",
        type=Path,
        default=DEFAULT_OUTPUT,
        help=(
            "Markdown file to write. Defaults to "
            f"{DEFAULT_OUTPUT.relative_to(REPO_ROOT)}."
        ),
    )
    parser.add_argument(
        "--check",
        action="store_true",
        help="Fail if the output file is not up to date.",
    )
    return parser.parse_args()


def main() -> int:
    args = parse_args()
    output = args.output if args.output.is_absolute() else REPO_ROOT / args.output
    content = build_markdown()

    if args.check:
        current = output.read_text(encoding="utf-8") if output.exists() else None
        if current != content:
            print(f"{output.relative_to(REPO_ROOT)} is not up to date", file=sys.stderr)
            return 1
        return 0

    output.parent.mkdir(parents=True, exist_ok=True)
    output.write_text(content, encoding="utf-8", newline="\n")
    print(f"Wrote {output.relative_to(REPO_ROOT)}")
    return 0


if __name__ == "__main__":
    sys.exit(main())