|
| 1 | +using ManagedCode.Tps.Models; |
| 2 | + |
| 3 | +namespace ManagedCode.Tps.Internal; |
| 4 | + |
| 5 | +internal static partial class CompiledScriptValidator |
| 6 | +{ |
| 7 | + private static void ValidateWords(IReadOnlyList<CompiledWord> words, ISet<string> wordIds) |
| 8 | + { |
| 9 | + CompiledWord? previousWord = null; |
| 10 | + for (var index = 0; index < words.Count; index++) |
| 11 | + { |
| 12 | + var word = words[index]; |
| 13 | + ValidateIdentifier(word.Id, "word", nameof(words), wordIds); |
| 14 | + |
| 15 | + if (word.Index != index) |
| 16 | + { |
| 17 | + throw new ArgumentException("Compiled TPS words must have sequential indexes that match their order.", nameof(words)); |
| 18 | + } |
| 19 | + |
| 20 | + if (string.IsNullOrWhiteSpace(word.SegmentId) || string.IsNullOrWhiteSpace(word.BlockId)) |
| 21 | + { |
| 22 | + throw new ArgumentException("Compiled TPS words must reference a segment and block.", nameof(words)); |
| 23 | + } |
| 24 | + |
| 25 | + if (string.Equals(word.Kind, TpsSpec.WordKinds.Word, StringComparison.Ordinal) && string.IsNullOrWhiteSpace(word.PhraseId)) |
| 26 | + { |
| 27 | + throw new ArgumentException("Compiled TPS spoken words must reference a phrase.", nameof(words)); |
| 28 | + } |
| 29 | + |
| 30 | + if (word.StartMs < 0 || word.EndMs < word.StartMs) |
| 31 | + { |
| 32 | + throw new ArgumentException("Compiled TPS words must define a non-negative time range.", nameof(words)); |
| 33 | + } |
| 34 | + |
| 35 | + if (word.EndMs - word.StartMs != word.DisplayDurationMs) |
| 36 | + { |
| 37 | + throw new ArgumentException("Compiled TPS words must keep display duration aligned with their start and end timestamps.", nameof(words)); |
| 38 | + } |
| 39 | + |
| 40 | + if (previousWord is not null && word.StartMs != previousWord.EndMs) |
| 41 | + { |
| 42 | + throw new ArgumentException("Compiled TPS words must form a contiguous timeline.", nameof(words)); |
| 43 | + } |
| 44 | + |
| 45 | + previousWord = word; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private static void ValidateTimeRange( |
| 50 | + string scope, |
| 51 | + int startWordIndex, |
| 52 | + int endWordIndex, |
| 53 | + int startMs, |
| 54 | + int endMs, |
| 55 | + int wordCount, |
| 56 | + string parameterName) |
| 57 | + { |
| 58 | + if (startWordIndex < 0 || endWordIndex < startWordIndex || startMs < 0 || endMs < startMs) |
| 59 | + { |
| 60 | + throw new ArgumentException($"Compiled TPS {scope} ranges must be non-negative and ordered.", parameterName); |
| 61 | + } |
| 62 | + |
| 63 | + if (wordCount == 0) |
| 64 | + { |
| 65 | + if (startWordIndex != 0 || endWordIndex != 0 || startMs != 0 || endMs != 0) |
| 66 | + { |
| 67 | + throw new ArgumentException($"Compiled TPS empty {scope} ranges must stay at zero.", parameterName); |
| 68 | + } |
| 69 | + |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + if (startWordIndex >= wordCount || endWordIndex >= wordCount) |
| 74 | + { |
| 75 | + throw new ArgumentException($"Compiled TPS {scope} ranges must reference words inside the canonical timeline.", parameterName); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private static void ValidateIdentifier(string id, string scope, string parameterName, ISet<string> seen) |
| 80 | + { |
| 81 | + if (string.IsNullOrWhiteSpace(id)) |
| 82 | + { |
| 83 | + throw new ArgumentException($"Compiled TPS {scope} identifiers cannot be empty.", parameterName); |
| 84 | + } |
| 85 | + |
| 86 | + if (!seen.Add(id)) |
| 87 | + { |
| 88 | + throw new ArgumentException($"Compiled TPS {scope} identifiers must be unique.", parameterName); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static void ValidateWordReferences( |
| 93 | + IReadOnlyList<CompiledWord> words, |
| 94 | + IReadOnlySet<string> segmentIds, |
| 95 | + IReadOnlySet<string> blockIds, |
| 96 | + IReadOnlySet<string> phraseIds) |
| 97 | + { |
| 98 | + foreach (var word in words) |
| 99 | + { |
| 100 | + if (!segmentIds.Contains(word.SegmentId)) |
| 101 | + { |
| 102 | + throw new ArgumentException($"Compiled TPS word '{word.Id}' references an unknown segment '{word.SegmentId}'.", nameof(words)); |
| 103 | + } |
| 104 | + |
| 105 | + if (!blockIds.Contains(word.BlockId)) |
| 106 | + { |
| 107 | + throw new ArgumentException($"Compiled TPS word '{word.Id}' references an unknown block '{word.BlockId}'.", nameof(words)); |
| 108 | + } |
| 109 | + |
| 110 | + if (!string.IsNullOrWhiteSpace(word.PhraseId) && !phraseIds.Contains(word.PhraseId)) |
| 111 | + { |
| 112 | + throw new ArgumentException($"Compiled TPS word '{word.Id}' references an unknown phrase '{word.PhraseId}'.", nameof(words)); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private static void ValidateCanonicalScopeWords( |
| 118 | + string scope, |
| 119 | + string ownerId, |
| 120 | + IReadOnlyList<CompiledWord> scopeWords, |
| 121 | + int startWordIndex, |
| 122 | + int endWordIndex, |
| 123 | + int startMs, |
| 124 | + int endMs, |
| 125 | + IReadOnlyList<CompiledWord> canonicalWords, |
| 126 | + string parameterName, |
| 127 | + string expectedSegmentId, |
| 128 | + string? expectedBlockId = null, |
| 129 | + string? expectedPhraseId = null) |
| 130 | + { |
| 131 | + if (canonicalWords.Count == 0) |
| 132 | + { |
| 133 | + if (scopeWords.Count != 0) |
| 134 | + { |
| 135 | + throw new ArgumentException($"Compiled TPS {scope} '{ownerId}' cannot reference words when the canonical timeline is empty.", parameterName); |
| 136 | + } |
| 137 | + |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + if (scopeWords.Count == 0) |
| 142 | + { |
| 143 | + if (startWordIndex != 0 || endWordIndex != 0 || startMs != 0 || endMs != 0) |
| 144 | + { |
| 145 | + throw new ArgumentException($"Compiled TPS empty {scope} '{ownerId}' ranges must stay at zero.", parameterName); |
| 146 | + } |
| 147 | + |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + var expectedWordCount = endWordIndex - startWordIndex + 1; |
| 152 | + if (scopeWords.Count != expectedWordCount) |
| 153 | + { |
| 154 | + throw new ArgumentException($"Compiled TPS {scope} '{ownerId}' words must match the canonical range they claim to cover.", parameterName); |
| 155 | + } |
| 156 | + |
| 157 | + if (startMs != canonicalWords[startWordIndex].StartMs || endMs != canonicalWords[endWordIndex].EndMs) |
| 158 | + { |
| 159 | + throw new ArgumentException($"Compiled TPS {scope} '{ownerId}' timestamps must match the canonical word range they claim to cover.", parameterName); |
| 160 | + } |
| 161 | + |
| 162 | + for (var offset = 0; offset < scopeWords.Count; offset++) |
| 163 | + { |
| 164 | + var expectedWord = canonicalWords[startWordIndex + offset]; |
| 165 | + var actualWord = scopeWords[offset]; |
| 166 | + |
| 167 | + if (!string.Equals(actualWord.Id, expectedWord.Id, StringComparison.Ordinal) || |
| 168 | + actualWord.Index != expectedWord.Index || |
| 169 | + actualWord.StartMs != expectedWord.StartMs || |
| 170 | + actualWord.EndMs != expectedWord.EndMs) |
| 171 | + { |
| 172 | + throw new ArgumentException($"Compiled TPS {scope} '{ownerId}' words must stay aligned with the canonical word timeline.", parameterName); |
| 173 | + } |
| 174 | + |
| 175 | + if (!string.Equals(actualWord.SegmentId, expectedSegmentId, StringComparison.Ordinal)) |
| 176 | + { |
| 177 | + throw new ArgumentException($"Compiled TPS {scope} '{ownerId}' words must reference segment '{expectedSegmentId}'.", parameterName); |
| 178 | + } |
| 179 | + |
| 180 | + if (expectedBlockId is not null && !string.Equals(actualWord.BlockId, expectedBlockId, StringComparison.Ordinal)) |
| 181 | + { |
| 182 | + throw new ArgumentException($"Compiled TPS {scope} '{ownerId}' words must reference block '{expectedBlockId}'.", parameterName); |
| 183 | + } |
| 184 | + |
| 185 | + if (expectedPhraseId is not null && !string.Equals(actualWord.PhraseId, expectedPhraseId, StringComparison.Ordinal)) |
| 186 | + { |
| 187 | + throw new ArgumentException($"Compiled TPS {scope} '{ownerId}' words must reference phrase '{expectedPhraseId}'.", parameterName); |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments