From a9967c002cf4591a424a21074a9ec5cf350e7e9d Mon Sep 17 00:00:00 2001
From: Sakari Malkki <sakari.malkki@stat.fi>
Date: Thu, 12 Sep 2024 14:10:07 +0300
Subject: [PATCH] Removes wrapping error in benchmark and makes rule violation
 coordinates nullable

---
 Px.Utils.TestingApp/Commands/PxFileValidationBenchmark.cs | 7 ++++---
 .../ContentValidation/ContentValidator.UtilityMethods.cs  | 6 +++---
 .../ContentValidator.ValidationFunctions.cs               | 2 +-
 .../Validation/DatabaseValidation/DatabaseValidator.cs    | 4 ++--
 .../DatabaseValidation/DatabaseValidatorFunctions.cs      | 8 ++++----
 Px.Utils/Validation/ValidationFeedback.cs                 | 6 +++---
 6 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/Px.Utils.TestingApp/Commands/PxFileValidationBenchmark.cs b/Px.Utils.TestingApp/Commands/PxFileValidationBenchmark.cs
index 49ce752..492d8bc 100644
--- a/Px.Utils.TestingApp/Commands/PxFileValidationBenchmark.cs
+++ b/Px.Utils.TestingApp/Commands/PxFileValidationBenchmark.cs
@@ -1,4 +1,5 @@
-using Px.Utils.PxFile.Metadata;
+using Px.Utils.Exceptions;
+using Px.Utils.PxFile.Metadata;
 using Px.Utils.Validation;
 using System.Text;
 
@@ -30,10 +31,10 @@ protected override void OneTimeBenchmarkSetup()
             {
                 encoding = reader.GetEncoding(stream);
             }
-            catch (Exception e)
+            catch (InvalidPxFileMetadataException)
             {
-                Console.WriteLine($"Error while reading the encoding of the file: {e.Message}");
                 encoding = Encoding.Default;
+                throw;
             }
         }
 
diff --git a/Px.Utils/Validation/ContentValidation/ContentValidator.UtilityMethods.cs b/Px.Utils/Validation/ContentValidation/ContentValidator.UtilityMethods.cs
index 38c7151..1d77178 100644
--- a/Px.Utils/Validation/ContentValidation/ContentValidator.UtilityMethods.cs
+++ b/Px.Utils/Validation/ContentValidation/ContentValidator.UtilityMethods.cs
@@ -56,7 +56,7 @@ private static Dictionary<KeyValuePair<string, string>, string[]> FindDimensionV
                         KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue> feedback = new(
                             new(ValidationFeedbackLevel.Error,
                                 ValidationFeedbackRule.VariableValuesMissing),
-                            new(filename, 0, 0, $"{dimension}, {language}")
+                            new(filename, additionalInfo: $"{dimension}, {language}")
                         );
 
                         feedbackItems.Add(feedback);
@@ -98,7 +98,7 @@ private static Dictionary<KeyValuePair<string, string>, string[]> FindDimensionV
                 return new KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue> (
                     new(recommended ? ValidationFeedbackLevel.Warning : ValidationFeedbackLevel.Error,
                     recommended ? ValidationFeedbackRule.RecommendedKeyMissing : ValidationFeedbackRule.RequiredKeyMissing),
-                    new(validator._filename, 0, 0, $"{keyword}, {language}, {dimensionName}, {dimensionValueName}")
+                    new(validator._filename, additionalInfo: $"{keyword}, {language}, {dimensionName}, {dimensionValueName}")
                     );
             }
             else if (entry.Key.FirstSpecifier is null || entry.Key.SecondSpecifier is null)
@@ -143,7 +143,7 @@ private static Dictionary<KeyValuePair<string, string>, string[]> FindDimensionV
                 return new(
                     new(ValidationFeedbackLevel.Warning,
                     ValidationFeedbackRule.RecommendedKeyMissing),
-                    new(validator._filename, 0, 0, $"{keyword}, {language}, {dimensionName}")
+                    new(validator._filename, additionalInfo: $"{keyword}, {language}, {dimensionName}")
                     );
             }
 
diff --git a/Px.Utils/Validation/ContentValidation/ContentValidator.ValidationFunctions.cs b/Px.Utils/Validation/ContentValidation/ContentValidator.ValidationFunctions.cs
index 3ab7e6a..9b31682 100644
--- a/Px.Utils/Validation/ContentValidation/ContentValidator.ValidationFunctions.cs
+++ b/Px.Utils/Validation/ContentValidation/ContentValidator.ValidationFunctions.cs
@@ -425,7 +425,7 @@ validator._headingDimensionNames is not null &&
 
                     feedbackItems.Add(new(
                         new(ValidationFeedbackLevel.Warning, ValidationFeedbackRule.DuplicateEntry),
-                        new(validator._filename, 0, 0, $"{item.Key.Key}, {item.Key.Value}")
+                        new(validator._filename, additionalInfo: $"{item.Key.Key}, {item.Key.Value}")
                     ));
                 }
             }
diff --git a/Px.Utils/Validation/DatabaseValidation/DatabaseValidator.cs b/Px.Utils/Validation/DatabaseValidation/DatabaseValidator.cs
index a14ceea..e603f19 100644
--- a/Px.Utils/Validation/DatabaseValidation/DatabaseValidator.cs
+++ b/Px.Utils/Validation/DatabaseValidation/DatabaseValidator.cs
@@ -237,7 +237,7 @@ private DatabaseFileInfo GetPxFileInfo(string filename, Stream stream)
                 feedbacks.Add(new(
                     new(ValidationFeedbackLevel.Error,
                     ValidationFeedbackRule.NoEncoding),
-                    new(filename, 0, 0, $"Error while reading the encoding of the file {filename}: {e.Message}"))
+                    new(filename, additionalInfo: $"Error while reading the encoding of the file {filename}: {e.Message}"))
                 );
             }
             stream.Position = 0;
@@ -299,7 +299,7 @@ private async Task<DatabaseFileInfo> GetPxFileInfoAsync(string filename, Stream
                 feedbacks.Add(new(
                     new(ValidationFeedbackLevel.Error,
                     ValidationFeedbackRule.NoEncoding),
-                    new(filename, 0, 0, $"Error while reading the encoding of the file {filename}: {e.Message}"))
+                    new(filename, additionalInfo: $"Error while reading the encoding of the file {filename}: {e.Message}"))
                 );
             }
             stream.Position = 0;
diff --git a/Px.Utils/Validation/DatabaseValidation/DatabaseValidatorFunctions.cs b/Px.Utils/Validation/DatabaseValidation/DatabaseValidatorFunctions.cs
index e466f4d..c6b1fc1 100644
--- a/Px.Utils/Validation/DatabaseValidation/DatabaseValidatorFunctions.cs
+++ b/Px.Utils/Validation/DatabaseValidation/DatabaseValidatorFunctions.cs
@@ -24,7 +24,7 @@ public class DuplicatePxFileName(List<DatabaseFileInfo> pxFiles) : IDatabaseVali
                 return new(
                     new(ValidationFeedbackLevel.Warning,
                         ValidationFeedbackRule.DuplicateFileNames),
-                    new(fileInfo.Name, 0, 0, $"Duplicate file name: {fileInfo.Name}")
+                    new(fileInfo.Name)
                 );
             }
             else
@@ -55,7 +55,7 @@ public class MissingPxFileLanguages(IEnumerable<string> allLanguages) : IDatabas
                 return new(
                     new(ValidationFeedbackLevel.Warning,
                     ValidationFeedbackRule.FileLanguageDiffersFromDatabase),
-                    new(fileInfo.Name, 0, 0, $"Missing languages in file {fileInfo.Name}: {string.Join(", ", _allLanguages.Except(fileInfo.Languages))}")
+                    new(fileInfo.Name, additionalInfo: $"Missing languages: {string.Join(", ", _allLanguages.Except(fileInfo.Languages))}")
                 );
             }
             else
@@ -86,7 +86,7 @@ public class MismatchingEncoding(Encoding mostCommonEncoding) : IDatabaseValidat
                 return new (
                     new(ValidationFeedbackLevel.Warning,
                     ValidationFeedbackRule.FileEncodingDiffersFromDatabase),
-                    new(fileInfo.Name, 0, 0, $"Inconsistent encoding in file {fileInfo.Name}: {fileInfo.Encoding.EncodingName}. " +
+                    new(fileInfo.Name, additionalInfo: $"Inconsistent encoding: {fileInfo.Encoding.EncodingName}. " +
                     $"Most commonly used encoding is {_mostCommonEncoding.EncodingName}"));
             }
             else
@@ -120,7 +120,7 @@ public class MissingAliasFiles(List<DatabaseFileInfo> aliasFiles, IEnumerable<st
                     return new(
                         new(ValidationFeedbackLevel.Warning,
                         ValidationFeedbackRule.AliasFileMissing),
-                        new(item.Path, 0, 0, $"Alias file for {language} in {item.Path} is missing")
+                        new(item.Path, additionalInfo: $"Alias file for {language} is missing")
                     );
                 }
             }
diff --git a/Px.Utils/Validation/ValidationFeedback.cs b/Px.Utils/Validation/ValidationFeedback.cs
index d9cde7e..6710ccf 100644
--- a/Px.Utils/Validation/ValidationFeedback.cs
+++ b/Px.Utils/Validation/ValidationFeedback.cs
@@ -64,7 +64,7 @@ public readonly struct ValidationFeedbackKey(ValidationFeedbackLevel level, Vali
     /// <summary>
     /// Stores information about a specific instance of a validation feedback rule violation.
     /// </summary>
-    public readonly struct ValidationFeedbackValue(string filename, int line = 0, int character = 0, string? additionalInfo = null)
+    public readonly struct ValidationFeedbackValue(string filename, int? line = null, int? character = null, string? additionalInfo = null)
     {
         /// <summary>
         /// Name of the file where the violation occurred.
@@ -73,11 +73,11 @@ public readonly struct ValidationFeedbackValue(string filename, int line = 0, in
         /// <summary>
         /// Line number where the violation occurred.
         /// </summary>
-        public int Line { get; } = line;
+        public int? Line { get; } = line;
         /// <summary>
         /// Character position where the violation occurred.
         /// </summary>
-        public int Character { get; } = character;
+        public int? Character { get; } = character;
         /// <summary>
         /// Additional information about the violation.
         /// </summary>