-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathValueObjects.cs
More file actions
219 lines (168 loc) · 6.74 KB
/
Copy pathValueObjects.cs
File metadata and controls
219 lines (168 loc) · 6.74 KB
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
// -----------------------------------------------------------------------
// <copyright file="ValueObjects.cs" company="Petabridge, LLC">
// Copyright (C) 2026 - 2026 Petabridge, LLC <https://petabridge.com>
// </copyright>
// -----------------------------------------------------------------------
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
namespace SkillServer.Models;
/// <summary>
/// A valid skill name (1-64 lowercase alphanumeric + hyphens, no leading/trailing/consecutive hyphens).
/// </summary>
public readonly partial record struct SkillName
{
public string Value { get; }
private SkillName(string value) => Value = value;
public static bool TryCreate(string? value, [NotNullWhen(true)] out SkillName? result)
{
result = null;
if (string.IsNullOrWhiteSpace(value) || value.Length > 64)
return false;
var normalized = value.ToLowerInvariant();
if (!ValidNameRegex().IsMatch(normalized))
return false;
result = new SkillName(normalized);
return true;
}
public static SkillName Create(string value)
{
if (!TryCreate(value, out var result))
throw new ArgumentException($"Invalid skill name: '{value}'. Must be 1-64 lowercase alphanumeric characters and hyphens, no leading/trailing/consecutive hyphens.", nameof(value));
return result.Value;
}
public override string ToString() => Value;
public static implicit operator string(SkillName name) => name.Value;
[GeneratedRegex(@"^[a-z0-9]+(-[a-z0-9]+)*$", RegexOptions.Compiled)]
private static partial Regex ValidNameRegex();
}
/// <summary>
/// A semantic version string.
/// </summary>
public readonly record struct SkillVersionString
{
public string Value { get; }
private SkillVersionString(string value) => Value = value;
public static bool TryCreate(string? value, [NotNullWhen(true)] out SkillVersionString? result)
{
result = null;
if (string.IsNullOrWhiteSpace(value) || value.Length > 64)
return false;
// Basic semver-ish validation (allows 1.0.0, 1.0, 1.0.0-beta.1, etc.)
if (!value.All(c => char.IsLetterOrDigit(c) || c == '.' || c == '-' || c == '+'))
return false;
result = new SkillVersionString(value);
return true;
}
public static SkillVersionString Create(string value)
{
if (!TryCreate(value, out var result))
throw new ArgumentException($"Invalid version string: '{value}'.", nameof(value));
return result.Value;
}
public override string ToString() => Value;
public static implicit operator string(SkillVersionString version) => version.Value;
}
/// <summary>
/// A SHA-256 digest in the format "sha256:{64 hex chars}".
/// </summary>
public readonly partial record struct Sha256Digest
{
public string Value { get; }
/// <summary>
/// Returns just the hex portion without the "sha256:" prefix.
/// </summary>
public string HexValue => Value.StartsWith("sha256:", StringComparison.OrdinalIgnoreCase)
? Value[7..]
: Value;
private Sha256Digest(string value) => Value = value;
public static bool TryCreate(string? value, [NotNullWhen(true)] out Sha256Digest? result)
{
result = null;
if (string.IsNullOrWhiteSpace(value))
return false;
var normalized = value.StartsWith("sha256:", StringComparison.OrdinalIgnoreCase)
? value
: $"sha256:{value}";
if (!ValidDigestRegex().IsMatch(normalized))
return false;
result = new Sha256Digest(normalized.ToLowerInvariant());
return true;
}
public static Sha256Digest Create(string value)
{
if (!TryCreate(value, out var result))
throw new ArgumentException($"Invalid SHA-256 digest: '{value}'. Expected format: sha256:{{64 hex chars}}.", nameof(value));
return result.Value;
}
public static Sha256Digest FromHex(string hexValue)
{
return Create($"sha256:{hexValue}");
}
public override string ToString() => Value;
public static implicit operator string(Sha256Digest digest) => digest.Value;
[GeneratedRegex(@"^sha256:[a-f0-9]{64}$", RegexOptions.Compiled | RegexOptions.IgnoreCase)]
private static partial Regex ValidDigestRegex();
}
/// <summary>
/// A relative file path within a skill (e.g., "references/guide.md").
/// </summary>
public readonly record struct ResourcePath
{
public string Value { get; }
private ResourcePath(string value) => Value = value;
public static bool TryCreate(string? value, [NotNullWhen(true)] out ResourcePath? result)
{
result = null;
if (string.IsNullOrWhiteSpace(value))
return false;
if (value.Contains("..") || value.StartsWith('/') || value.StartsWith('\\'))
return false;
var normalized = value.Replace('\\', '/');
// Must be in a subdirectory — bare filenames at the root are not resources
if (!normalized.Contains('/') || normalized.IndexOf('/') == normalized.Length - 1)
return false;
result = new ResourcePath(normalized);
return true;
}
public static ResourcePath Create(string value)
{
if (!TryCreate(value, out var result))
throw new ArgumentException($"Invalid resource path: '{value}'. Must be a relative path in a subdirectory with no path traversal.", nameof(value));
return result.Value;
}
public override string ToString() => Value;
public static implicit operator string(ResourcePath path) => path.Value;
}
/// <summary>
/// A positive file size in bytes.
/// </summary>
public readonly record struct FileSize
{
public long Bytes { get; }
public FileSize(long bytes)
{
if (bytes < 0)
throw new ArgumentOutOfRangeException(nameof(bytes), "File size cannot be negative.");
Bytes = bytes;
}
public double Kilobytes => Bytes / 1024.0;
public double Megabytes => Bytes / (1024.0 * 1024.0);
public override string ToString() => Bytes switch
{
< 1024 => $"{Bytes} B",
< 1024 * 1024 => $"{Kilobytes:F1} KB",
_ => $"{Megabytes:F1} MB"
};
public static implicit operator long(FileSize size) => size.Bytes;
public static explicit operator FileSize(long bytes) => new(bytes);
}
/// <summary>
/// JSON converters for value objects (AOT-compatible).
/// </summary>
[JsonSerializable(typeof(SkillName))]
[JsonSerializable(typeof(SkillVersionString))]
[JsonSerializable(typeof(Sha256Digest))]
[JsonSerializable(typeof(ResourcePath))]
[JsonSerializable(typeof(FileSize))]
public partial class ValueObjectsJsonContext : JsonSerializerContext;