Registry: parse and read through small helpers so definite assignment survives mutation

Stryker flagged ParseManifest, ReadString and RunAsync as compile errors in safe
mode (out vars and try-assigned locals), leaving them unmutated. Behaviour unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-10 20:34:03 +02:00
commit 46d92200e5
2 changed files with 62 additions and 47 deletions

View file

@ -23,17 +23,7 @@ public static class ConceptCommand
string[] retire = hasRetire ? args[4].Split(',', StringSplitOptions.RemoveEmptyEntries) : [];
try
{
string json;
try
{
json = await File.ReadAllTextAsync(args[2]);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
throw new ManifestRefusedException("manifest", "Unreadable");
}
var ships = ConceptSubjectRegistry.ParseManifest(json);
var ships = ConceptSubjectRegistry.ParseManifest(await ReadManifestAsync(args[2]));
await using var scope = services.CreateAsyncScope();
var registry = scope.ServiceProvider.GetRequiredService<ConceptSubjectRegistry>();
var plan = args[0] == "apply"
@ -53,4 +43,16 @@ public static class ConceptCommand
return Refused;
}
}
private static async Task<string> ReadManifestAsync(string path)
{
try
{
return await File.ReadAllTextAsync(path);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
throw new ManifestRefusedException("manifest", "Unreadable");
}
}
}

View file

@ -42,57 +42,65 @@ public sealed class ConceptSubjectRegistry(
// The Mill manifest is JSON from outside the Verse. Only ships[].id and ships[].name are read.
public static IReadOnlyList<ManifestShip> ParseManifest(string json)
{
JsonDocument document;
using var document = ParseDocument(json);
if (document.RootElement.ValueKind != JsonValueKind.Object)
{
throw new ManifestRefusedException("ships", "Missing");
}
if (!document.RootElement.TryGetProperty("ships", out var ships))
{
throw new ManifestRefusedException("ships", "Missing");
}
if (ships.ValueKind != JsonValueKind.Array)
{
throw new ManifestRefusedException("ships", "Missing");
}
var result = new List<ManifestShip>();
var seen = new HashSet<string>(StringComparer.Ordinal);
foreach (var ship in ships.EnumerateArray())
{
var at = $"ships[{result.Count}]";
var key = ReadString(ship, "id", at);
var title = ReadString(ship, "name", at);
if (!ConceptKey.IsValid(key))
{
throw new ManifestRefusedException($"{at}.id", "InvalidKey");
}
if (!seen.Add(key))
{
throw new ManifestRefusedException($"{at}.id", "Duplicate");
}
if (string.IsNullOrWhiteSpace(title) || title.Any(char.IsControl))
{
throw new ManifestRefusedException($"{at}.name", "InvalidTitle");
}
if (title.Length > MaxTitleLength)
{
throw new ManifestRefusedException($"{at}.name", "TooLong");
}
result.Add(new ManifestShip(key, title));
}
return result;
}
private static JsonDocument ParseDocument(string json)
{
try
{
document = JsonDocument.Parse(json);
return JsonDocument.Parse(json);
}
catch (JsonException)
{
throw new ManifestRefusedException("manifest", "NotJson");
}
using (document)
{
if (document.RootElement.ValueKind != JsonValueKind.Object ||
!document.RootElement.TryGetProperty("ships", out var ships) ||
ships.ValueKind != JsonValueKind.Array)
{
throw new ManifestRefusedException("ships", "Missing");
}
var result = new List<ManifestShip>();
var seen = new HashSet<string>(StringComparer.Ordinal);
foreach (var ship in ships.EnumerateArray())
{
var at = $"ships[{result.Count}]";
var key = ReadString(ship, "id", at);
var title = ReadString(ship, "name", at);
if (!ConceptKey.IsValid(key))
{
throw new ManifestRefusedException($"{at}.id", "InvalidKey");
}
if (!seen.Add(key))
{
throw new ManifestRefusedException($"{at}.id", "Duplicate");
}
if (string.IsNullOrWhiteSpace(title) || title.Any(char.IsControl))
{
throw new ManifestRefusedException($"{at}.name", "InvalidTitle");
}
if (title.Length > MaxTitleLength)
{
throw new ManifestRefusedException($"{at}.name", "TooLong");
}
result.Add(new ManifestShip(key, title));
}
return result;
}
}
public async Task<SubjectPlan> PlanAsync(
@ -213,7 +221,12 @@ public sealed class ConceptSubjectRegistry(
private static string ReadString(JsonElement ship, string name, string at)
{
if (ship.ValueKind != JsonValueKind.Object || !ship.TryGetProperty(name, out var value))
if (ship.ValueKind != JsonValueKind.Object)
{
throw new ManifestRefusedException($"{at}.{name}", "Missing");
}
if (!ship.TryGetProperty(name, out var value))
{
throw new ManifestRefusedException($"{at}.{name}", "Missing");
}