| 1 | #nullable enable |
| 2 | |
| 3 | using CascadeIDE.Services; |
| 4 | |
| 5 | namespace CascadeIDE.Models.Editor; |
| 6 | |
| 7 | /// <summary>Канонический путь активного документа редактора; строится через <see cref="CanonicalFilePath"/> для стыковки с деревом решения и DAP/MCP.</summary> |
| 8 | public readonly struct EditorDocumentPath : IEquatable<EditorDocumentPath> |
| 9 | { |
| 10 | public string Value { get; } |
| 11 | |
| 12 | private EditorDocumentPath(string normalized) => Value = normalized; |
| 13 | |
| 14 | /// <summary>Отказ при пустой строке или при невалидном пути для <see cref="CanonicalFilePath.TryNormalize"/>.</summary> |
| 15 | public static bool TryCreate(string? rawPath, out EditorDocumentPath path, out string error) |
| 16 | { |
| 17 | if (string.IsNullOrWhiteSpace(rawPath)) |
| 18 | { |
| 19 | path = default; |
| 20 | error = "Пустой file_path."; |
| 21 | return false; |
| 22 | } |
| 23 | |
| 24 | var trimmed = rawPath.Trim(); |
| 25 | if (!CanonicalFilePath.TryNormalize(trimmed, out var full)) |
| 26 | { |
| 27 | path = default; |
| 28 | error = "Не удалось нормализовать file_path."; |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | path = new EditorDocumentPath(full); |
| 33 | error = ""; |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | public bool Equals(EditorDocumentPath other) => |
| 38 | string.Equals(Value, other.Value, StringComparison.OrdinalIgnoreCase); |
| 39 | |
| 40 | public override bool Equals(object? obj) => obj is EditorDocumentPath other && Equals(other); |
| 41 | |
| 42 | public override int GetHashCode() => Value.GetHashCode(StringComparison.OrdinalIgnoreCase); |
| 43 | |
| 44 | public static bool operator ==(EditorDocumentPath left, EditorDocumentPath right) => left.Equals(right); |
| 45 | |
| 46 | public static bool operator !=(EditorDocumentPath left, EditorDocumentPath right) => !left.Equals(right); |
| 47 | } |
| 48 | |