diff --git a/src/.editorconfig b/src/.editorconfig new file mode 100644 index 0000000..6075caf --- /dev/null +++ b/src/.editorconfig @@ -0,0 +1,2 @@ +[*.cs] +csharp_style_namespace_declarations=file_scoped:suggestion \ No newline at end of file diff --git a/src/SeztionParser/Constants/ExceptionMessages.cs b/src/SeztionParser/Constants/ExceptionMessages.cs index 94a8849..0ddcc64 100644 --- a/src/SeztionParser/Constants/ExceptionMessages.cs +++ b/src/SeztionParser/Constants/ExceptionMessages.cs @@ -2,24 +2,23 @@ using System.Collections.Generic; using System.Text; -namespace SeztionParser.Constants +namespace SeztionParser.Constants; + +/// +/// Represents error messages that can be used by the library. +/// +public class ExceptionMessages { - /// - /// Represents error messages that can be used by the library. - /// - public class ExceptionMessages - { - /// - public const string SectionNameIsEmptyMessage = "Parser found an empty section name"; - /// - public const string SectionWithoutDataMessage = "Parser found a section without data"; - /// - public const string SpecifiedSectionDoesNotExistMessage = "The specified section does not exist"; - /// - public const string SeccionIsRepeatedMessage = "Parser found a repeated section"; - /// - public const string ElementThatIsNotPartAnySectionMessage = "Parser found an element that is not part of any section"; - /// - public const string DataSourceIsEmptyMessage = "Parser found an empty data source"; - } + /// + public const string SectionNameIsEmptyMessage = "Parser found an empty section name"; + /// + public const string SectionWithoutDataMessage = "Parser found a section without data"; + /// + public const string SpecifiedSectionDoesNotExistMessage = "The specified section does not exist"; + /// + public const string SeccionIsRepeatedMessage = "Parser found a repeated section"; + /// + public const string ElementThatIsNotPartAnySectionMessage = "Parser found an element that is not part of any section"; + /// + public const string DataSourceIsEmptyMessage = "Parser found an empty data source"; } diff --git a/src/SeztionParser/Exceptions/ParserException.cs b/src/SeztionParser/Exceptions/ParserException.cs index 85efe99..1e15311 100644 --- a/src/SeztionParser/Exceptions/ParserException.cs +++ b/src/SeztionParser/Exceptions/ParserException.cs @@ -2,41 +2,40 @@ using System.Collections.Generic; using System.Text; -namespace SeztionParser.Exceptions +namespace SeztionParser.Exceptions; + +/// +/// The exception that is thrown when the parser finds an error during the parsing, such as a duplicate section. +/// +public class ParserException : Exception { + private readonly object actualValue; + /// - /// The exception that is thrown when the parser finds an error during the parsing, such as a duplicate section. + /// Allows access to the actual value causing the exception. /// - public class ParserException : Exception - { - private readonly object actualValue; - - /// - /// Allows access to the actual value causing the exception. - /// - public object ActualValue => actualValue; + public object ActualValue => actualValue; - /// - /// Initializes a new instance of the class with a specified error message. - /// - /// The message that describes the error. - public ParserException(string message) : base(message) - { - - } + /// + /// Initializes a new instance of the class with a specified error message. + /// + /// The message that describes the error. + public ParserException(string message) : base(message) + { - /// - /// Initializes a new instance of the class with a specified error message and the actual value that caused the exception. - /// - /// The message that describes the error. - /// The actual value that caused the exception. - public ParserException(string message, object actualValue) : base(message) - { - this.actualValue = actualValue; - } + } - /// - public override string Message - => actualValue != null ? base.Message + " (Actual Value: " + actualValue + ")" : base.Message; + /// + /// Initializes a new instance of the class with a specified error message and the actual value that caused the exception. + /// + /// The message that describes the error. + /// The actual value that caused the exception. + public ParserException(string message, object actualValue) : base(message) + { + this.actualValue = actualValue; } + + /// + public override string Message + => actualValue != null ? base.Message + " (Actual Value: " + actualValue + ")" : base.Message; } \ No newline at end of file diff --git a/src/SeztionParser/Exceptions/SectionNotFoundException.cs b/src/SeztionParser/Exceptions/SectionNotFoundException.cs index f518655..008e744 100644 --- a/src/SeztionParser/Exceptions/SectionNotFoundException.cs +++ b/src/SeztionParser/Exceptions/SectionNotFoundException.cs @@ -2,21 +2,20 @@ using System.Collections.Generic; using System.Text; -namespace SeztionParser.Exceptions +namespace SeztionParser.Exceptions; + +/// +/// The exception that is thrown when the specified section does not exist in the collection. +/// +public class SectionNotFoundException : ArgumentException { /// - /// The exception that is thrown when the specified section does not exist in the collection. + /// Initializes a new instance of the class with a specified error message and the name of the parameter that causes this exception. /// - public class SectionNotFoundException : ArgumentException + /// The message that describes the error. + /// The name of the parameter that caused the exception. + public SectionNotFoundException(string message, string paramName) : base(message, paramName) { - /// - /// Initializes a new instance of the class with a specified error message and the name of the parameter that causes this exception. - /// - /// The message that describes the error. - /// The name of the parameter that caused the exception. - public SectionNotFoundException(string message, string paramName) : base(message, paramName) - { - } } } diff --git a/src/SeztionParser/Facades/SectionsFile.cs b/src/SeztionParser/Facades/SectionsFile.cs index 54355a5..0b3c894 100644 --- a/src/SeztionParser/Facades/SectionsFile.cs +++ b/src/SeztionParser/Facades/SectionsFile.cs @@ -5,21 +5,20 @@ using System.IO; using System.Text; -namespace SeztionParser.Facades +namespace SeztionParser.Facades; + +/// +/// Defines the operations that can be applied to a sections file. +/// +public static class SectionsFile { + /// + /// /// - /// Defines the operations that can be applied to a sections file. + /// Load the sections file. /// - public static class SectionsFile - { - /// - /// - /// - /// Load the sections file. - /// - /// The path of the file to load. - /// An instance with the data of each section. - public static ISectionsData Load(string path) - => new SectionsParser().Parse(File.ReadAllText(path)); - } + /// The path of the file to load. + /// An instance with the data of each section. + public static ISectionsData Load(string path) + => new SectionsParser().Parse(File.ReadAllText(path)); } diff --git a/src/SeztionParser/Helpers/SectionDataConversion.cs b/src/SeztionParser/Helpers/SectionDataConversion.cs index ca59031..cee0fc8 100644 --- a/src/SeztionParser/Helpers/SectionDataConversion.cs +++ b/src/SeztionParser/Helpers/SectionDataConversion.cs @@ -6,87 +6,86 @@ using System.Globalization; using SeztionParser.Exceptions; -namespace SeztionParser.Helpers +namespace SeztionParser.Helpers; + +/// +/// Defines operations that serve to convert a section data to another data type. +/// +public static class SectionDataConversion { /// - /// Defines operations that serve to convert a section data to another data type. + /// Convert section data to decimal. /// - public static class SectionDataConversion + /// The data of sections. + /// The name of the section to convert. + /// An object that provides culture-specific formatting information. + /// If the data in the section are not decimals. + /// If the section does not exist. + /// The section data in decimal format. + /// provider is null, the default culture is . + public static IEnumerable ToDecimal(this ISectionsData sections, string sectionName, IFormatProvider provider = null) { - /// - /// Convert section data to decimal. - /// - /// The data of sections. - /// The name of the section to convert. - /// An object that provides culture-specific formatting information. - /// If the data in the section are not decimals. - /// If the section does not exist. - /// The section data in decimal format. - /// provider is null, the default culture is . - public static IEnumerable ToDecimal(this ISectionsData sections, string sectionName, IFormatProvider provider = null) - { - foreach (var data in sections[sectionName]) - yield return decimal.Parse(data, provider ?? CultureInfo.InvariantCulture); - } + foreach (var data in sections[sectionName]) + yield return decimal.Parse(data, provider ?? CultureInfo.InvariantCulture); + } - /// - /// Convert section data to doubles. - /// - /// The data of sections. - /// The name of the section to convert. - /// An object that provides culture-specific formatting information. - /// If the data in the section are not doubles. - /// If the section does not exist. - /// The section data in double format. - /// provider is null, the default culture is . - public static IEnumerable ToDouble(this ISectionsData sections, string sectionName, IFormatProvider provider = null) - { - foreach (var data in sections[sectionName]) - yield return double.Parse(data, provider ?? CultureInfo.InvariantCulture); - } + /// + /// Convert section data to doubles. + /// + /// The data of sections. + /// The name of the section to convert. + /// An object that provides culture-specific formatting information. + /// If the data in the section are not doubles. + /// If the section does not exist. + /// The section data in double format. + /// provider is null, the default culture is . + public static IEnumerable ToDouble(this ISectionsData sections, string sectionName, IFormatProvider provider = null) + { + foreach (var data in sections[sectionName]) + yield return double.Parse(data, provider ?? CultureInfo.InvariantCulture); + } - /// - /// Convert section data to floats. - /// - /// The data of sections. - /// The name of the section to convert. - /// An object that provides culture-specific formatting information. - /// If the data in the section are not floats. - /// If the section does not exist. - /// The section data in float format. - /// provider is null, the default culture is . - public static IEnumerable ToFloat(this ISectionsData sections, string sectionName, IFormatProvider provider = null) - { - foreach (var data in sections[sectionName]) - yield return float.Parse(data, provider ?? CultureInfo.InvariantCulture); - } + /// + /// Convert section data to floats. + /// + /// The data of sections. + /// The name of the section to convert. + /// An object that provides culture-specific formatting information. + /// If the data in the section are not floats. + /// If the section does not exist. + /// The section data in float format. + /// provider is null, the default culture is . + public static IEnumerable ToFloat(this ISectionsData sections, string sectionName, IFormatProvider provider = null) + { + foreach (var data in sections[sectionName]) + yield return float.Parse(data, provider ?? CultureInfo.InvariantCulture); + } - /// - /// Convert section data to integers. - /// - /// The data of sections. - /// The name of the section to convert. - /// If the data in the section are not integers. - /// If the section does not exist. - /// The section data in integer format. - public static IEnumerable ToInt(this ISectionsData sections, string sectionName) - { - foreach (var data in sections[sectionName]) - yield return int.Parse(data); - } + /// + /// Convert section data to integers. + /// + /// The data of sections. + /// The name of the section to convert. + /// If the data in the section are not integers. + /// If the section does not exist. + /// The section data in integer format. + public static IEnumerable ToInt(this ISectionsData sections, string sectionName) + { + foreach (var data in sections[sectionName]) + yield return int.Parse(data); + } - /// - /// Convert section data to longs. - /// - /// The data of sections. - /// The name of the section to convert. - /// If the data in the section are not longs. - /// If the section does not exist. - /// The section data in long format. - public static IEnumerable ToLong(this ISectionsData sections, string sectionName) - { - foreach (var data in sections[sectionName]) - yield return long.Parse(data); - } + /// + /// Convert section data to longs. + /// + /// The data of sections. + /// The name of the section to convert. + /// If the data in the section are not longs. + /// If the section does not exist. + /// The section data in long format. + public static IEnumerable ToLong(this ISectionsData sections, string sectionName) + { + foreach (var data in sections[sectionName]) + yield return long.Parse(data); } } diff --git a/src/SeztionParser/Helpers/SingleLineSection.cs b/src/SeztionParser/Helpers/SingleLineSection.cs index 034b4ac..a074704 100644 --- a/src/SeztionParser/Helpers/SingleLineSection.cs +++ b/src/SeztionParser/Helpers/SingleLineSection.cs @@ -5,82 +5,81 @@ using System.Globalization; using System.Text; -namespace SeztionParser.Helpers +namespace SeztionParser.Helpers; + +/// +/// Defines operations used to gets the first line of a section in a specific format. +/// +public static class SingleLineSection { /// - /// Defines operations used to gets the first line of a section in a specific format. + /// Gets the first line of the section in decimal format. /// - public static class SingleLineSection - { - /// - /// Gets the first line of the section in decimal format. - /// - /// The data of sections. - /// The name of the section to gets in a specific format. - /// An object that provides culture-specific formatting information. - /// If the first line is not an decimal. - /// If the section does not exist. - /// The first line in decimal format. - /// provider is null, the default culture is . - public static decimal GetFirstLineDecimal(this ISectionsData sections, string sectionName, IFormatProvider provider = null) - => decimal.Parse(sections[sectionName][0], provider ?? CultureInfo.InvariantCulture); + /// The data of sections. + /// The name of the section to gets in a specific format. + /// An object that provides culture-specific formatting information. + /// If the first line is not an decimal. + /// If the section does not exist. + /// The first line in decimal format. + /// provider is null, the default culture is . + public static decimal GetFirstLineDecimal(this ISectionsData sections, string sectionName, IFormatProvider provider = null) + => decimal.Parse(sections[sectionName][0], provider ?? CultureInfo.InvariantCulture); - /// - /// Gets the first line of the section in double format. - /// - /// The data of sections. - /// The name of the section to gets in a specific format. - /// An object that provides culture-specific formatting information. - /// If the first line is not an double. - /// If the section does not exist. - /// The first line in double format. - /// provider is null, the default culture is . - public static double GetFirstLineDouble(this ISectionsData sections, string sectionName, IFormatProvider provider = null) - => double.Parse(sections[sectionName][0], provider ?? CultureInfo.InvariantCulture); + /// + /// Gets the first line of the section in double format. + /// + /// The data of sections. + /// The name of the section to gets in a specific format. + /// An object that provides culture-specific formatting information. + /// If the first line is not an double. + /// If the section does not exist. + /// The first line in double format. + /// provider is null, the default culture is . + public static double GetFirstLineDouble(this ISectionsData sections, string sectionName, IFormatProvider provider = null) + => double.Parse(sections[sectionName][0], provider ?? CultureInfo.InvariantCulture); - /// - /// Gets the first line of the section in float format. - /// - /// The data of sections. - /// The name of the section to gets in a specific format. - /// An object that provides culture-specific formatting information. - /// If the first line is not an float. - /// If the section does not exist. - /// The first line in float format. - /// provider is null, the default culture is . - public static float GetFirstLineFloat(this ISectionsData sections, string sectionName, IFormatProvider provider = null) - => float.Parse(sections[sectionName][0], provider ?? CultureInfo.InvariantCulture); + /// + /// Gets the first line of the section in float format. + /// + /// The data of sections. + /// The name of the section to gets in a specific format. + /// An object that provides culture-specific formatting information. + /// If the first line is not an float. + /// If the section does not exist. + /// The first line in float format. + /// provider is null, the default culture is . + public static float GetFirstLineFloat(this ISectionsData sections, string sectionName, IFormatProvider provider = null) + => float.Parse(sections[sectionName][0], provider ?? CultureInfo.InvariantCulture); - /// - /// Gets the first line of the section in integer format. - /// - /// The data of sections. - /// The name of the section to gets in a specific format. - /// If the first line is not an integer. - /// If the section does not exist. - /// The first line in integer format. - public static int GetFirstLineInt(this ISectionsData sections, string sectionName) - => int.Parse(sections[sectionName][0]); + /// + /// Gets the first line of the section in integer format. + /// + /// The data of sections. + /// The name of the section to gets in a specific format. + /// If the first line is not an integer. + /// If the section does not exist. + /// The first line in integer format. + public static int GetFirstLineInt(this ISectionsData sections, string sectionName) + => int.Parse(sections[sectionName][0]); - /// - /// Gets the first line of the section in long format. - /// - /// The data of sections. - /// The name of the section to gets in a specific format. - /// If the first line is not an long. - /// If the section does not exist. - /// The first line in long format. - public static long GetFirstLineLong(this ISectionsData sections, string sectionName) - => long.Parse(sections[sectionName][0]); + /// + /// Gets the first line of the section in long format. + /// + /// The data of sections. + /// The name of the section to gets in a specific format. + /// If the first line is not an long. + /// If the section does not exist. + /// The first line in long format. + public static long GetFirstLineLong(this ISectionsData sections, string sectionName) + => long.Parse(sections[sectionName][0]); - /// - /// Gets the first line of the section in string format. - /// - /// The data of sections. - /// The name of the section to gets in a specific format. - /// If the section does not exist. - /// The first line in string format. - public static string GetFirstLineString(this ISectionsData sections, string sectionName) - => sections[sectionName][0]; - } + /// + /// Gets the first line of the section in string format. + /// + /// The data of sections. + /// The name of the section to gets in a specific format. + /// If the section does not exist. + /// The first line in string format. + public static string GetFirstLineString(this ISectionsData sections, string sectionName) + => sections[sectionName][0]; } diff --git a/src/SeztionParser/Interfaces/ISectionData.cs b/src/SeztionParser/Interfaces/ISectionData.cs index aa57139..bd277a3 100644 --- a/src/SeztionParser/Interfaces/ISectionData.cs +++ b/src/SeztionParser/Interfaces/ISectionData.cs @@ -2,31 +2,30 @@ using System.Collections.Generic; using System.Text; -namespace SeztionParser.Interfaces +namespace SeztionParser.Interfaces; + +/// +/// Represents a collection of data from a specific section. +/// +public interface ISectionData : IEnumerable { /// - /// Represents a collection of data from a specific section. + /// Gets an element of a section at the specified index. /// - public interface ISectionData : IEnumerable - { - /// - /// Gets an element of a section at the specified index. - /// - /// The zero-based index of the element to get from a section. - /// The element of a section at the specified index. - string this[int index] { get; } + /// The zero-based index of the element to get from a section. + /// The element of a section at the specified index. + string this[int index] { get; } - /// - /// Gets the number of elements in a section. - /// - /// The number of elements contained in the section. - int Count { get; } + /// + /// Gets the number of elements in a section. + /// + /// The number of elements contained in the section. + int Count { get; } - /// - /// Determine if the item is in the section. - /// - /// The item to locate in the section. - /// true if item is found in the section; otherwise, false. - bool Contains(string item); - } + /// + /// Determine if the item is in the section. + /// + /// The item to locate in the section. + /// true if item is found in the section; otherwise, false. + bool Contains(string item); } diff --git a/src/SeztionParser/Interfaces/ISectionsData.cs b/src/SeztionParser/Interfaces/ISectionsData.cs index e88d142..d3d3819 100644 --- a/src/SeztionParser/Interfaces/ISectionsData.cs +++ b/src/SeztionParser/Interfaces/ISectionsData.cs @@ -4,48 +4,47 @@ using SeztionParser.Exceptions; using SeztionParser.Models; -namespace SeztionParser.Interfaces +namespace SeztionParser.Interfaces; + +/// +/// Represents a collection of sections. +/// +public interface ISectionsData : IEnumerable { /// - /// Represents a collection of sections. + /// Gets the data of a section. /// - public interface ISectionsData : IEnumerable - { - /// - /// Gets the data of a section. - /// - /// The name of the section to get. - /// - /// When this method returns, the data associated with the specified section, if the section is found; otherwise, the default value is null. - /// This parameter is passed uninitialized. - /// - /// section is null. - /// - /// true if the section exists, otherwise, returns false. - /// - bool TryGetData(string section, out ISectionData data); + /// The name of the section to get. + /// + /// When this method returns, the data associated with the specified section, if the section is found; otherwise, the default value is null. + /// This parameter is passed uninitialized. + /// + /// section is null. + /// + /// true if the section exists, otherwise, returns false. + /// + bool TryGetData(string section, out ISectionData data); - /// - /// Gets the data of a section with the specified section. - /// - /// The name of the section to get. - /// section is null. - /// If the section does not exist. - /// - /// The collection with the specified section. - /// - ISectionData this[string section] { get; } + /// + /// Gets the data of a section with the specified section. + /// + /// The name of the section to get. + /// section is null. + /// If the section does not exist. + /// + /// The collection with the specified section. + /// + ISectionData this[string section] { get; } - /// - /// Gets the names of all sections. - /// - /// The names of all sections. - ICollection GetNames(); + /// + /// Gets the names of all sections. + /// + /// The names of all sections. + ICollection GetNames(); - /// - /// Gets the data from all sections. - /// - /// The data from all sections. - ICollection GetAll(); - } + /// + /// Gets the data from all sections. + /// + /// The data from all sections. + ICollection GetAll(); } diff --git a/src/SeztionParser/Interfaces/ISectionsParser.cs b/src/SeztionParser/Interfaces/ISectionsParser.cs index de61b1e..9498cab 100644 --- a/src/SeztionParser/Interfaces/ISectionsParser.cs +++ b/src/SeztionParser/Interfaces/ISectionsParser.cs @@ -3,19 +3,18 @@ using System.Text; using SeztionParser.Exceptions; -namespace SeztionParser.Interfaces +namespace SeztionParser.Interfaces; + +/// +/// Represents the sections parser. +/// +public interface ISectionsParser { /// - /// Represents the sections parser. + /// Starts the parsing to extract the data of each section. /// - public interface ISectionsParser - { - /// - /// Starts the parsing to extract the data of each section. - /// - /// Data to parsing. - /// If the parser find an error during the parsing. - /// An instance with the parsed data. - ISectionsData Parse(string data); - } + /// Data to parsing. + /// If the parser find an error during the parsing. + /// An instance with the parsed data. + ISectionsData Parse(string data); } diff --git a/src/SeztionParser/Models/SectionModel.cs b/src/SeztionParser/Models/SectionModel.cs index df4f892..c31b0da 100644 --- a/src/SeztionParser/Models/SectionModel.cs +++ b/src/SeztionParser/Models/SectionModel.cs @@ -4,59 +4,58 @@ using System.Text; using static System.Environment; -namespace SeztionParser.Models +namespace SeztionParser.Models; + +/// +/// Represents a section. +/// +public struct SectionModel { + private string name; + private ISectionData data; + /// - /// Represents a section. + /// Allows access to the section name. /// - public struct SectionModel - { - private string name; - private ISectionData data; - - /// - /// Allows access to the section name. - /// - /// - /// The name of the section. - /// - public string Name => name; - - /// - /// Allows access to the section data. - /// - /// - /// An collection of section data. - /// - public ISectionData Data => data; + /// + /// The name of the section. + /// + public string Name => name; - /// - /// Initializes a new instance of the structure with the specified name and data. - /// - /// The name of the section. - /// The section data. - public SectionModel(string name, ISectionData data) - { - this.name = name; - this.data = data; - } + /// + /// Allows access to the section data. + /// + /// + /// An collection of section data. + /// + public ISectionData Data => data; - /// - /// Deconstructs the current . - /// - /// The section name of the current . - /// The sections data of the current . - public void Deconstruct(out string name, out ISectionData data) - { - name = Name; - data = Data; - } + /// + /// Initializes a new instance of the structure with the specified name and data. + /// + /// The name of the section. + /// The section data. + public SectionModel(string name, ISectionData data) + { + this.name = name; + this.data = data; + } - /// - /// Converts the instance to a object. - /// - /// A string that represents the current object. - public override string ToString() => - $"Section: {Name} ->{(Data.Count > 1 ? NewLine : " ")}{Data}"; + /// + /// Deconstructs the current . + /// + /// The section name of the current . + /// The sections data of the current . + public void Deconstruct(out string name, out ISectionData data) + { + name = Name; + data = Data; } + + /// + /// Converts the instance to a object. + /// + /// A string that represents the current object. + public override string ToString() => + $"Section: {Name} ->{(Data.Count > 1 ? NewLine : " ")}{Data}"; } diff --git a/src/SeztionParser/Providers/SectionData.cs b/src/SeztionParser/Providers/SectionData.cs index 3c79597..6cd3e91 100644 --- a/src/SeztionParser/Providers/SectionData.cs +++ b/src/SeztionParser/Providers/SectionData.cs @@ -5,66 +5,65 @@ using System.Text; using static System.Environment; -namespace SeztionParser.Providers +namespace SeztionParser.Providers; + +/// +class SectionData : ISectionData { - /// - class SectionData : ISectionData - { - /// - /// Allows access to the data of a specific section. - /// - /// - /// The list with the data of a specific section. - /// - private readonly List section = new List(16); + /// + /// Allows access to the data of a specific section. + /// + /// + /// The list with the data of a specific section. + /// + private readonly List section = new List(16); - /// - /// Adds an element in the section. - /// - /// The item to add in the section. - public void Add(string item) - => section.Add(item); + /// + /// Adds an element in the section. + /// + /// The item to add in the section. + public void Add(string item) + => section.Add(item); - /// - public string this[int index] => section[index]; + /// + public string this[int index] => section[index]; - /// - public int Count => section.Count; + /// + public int Count => section.Count; - /// - public bool Contains(string item) - => section.Contains(item); + /// + public bool Contains(string item) + => section.Contains(item); - /// - /// Returns an enumerator used to traverse the data of a specified section. - /// - /// An enumerator used to traverse the data of a specified section. - public IEnumerator GetEnumerator() - => section.GetEnumerator(); + /// + /// Returns an enumerator used to traverse the data of a specified section. + /// + /// An enumerator used to traverse the data of a specified section. + public IEnumerator GetEnumerator() + => section.GetEnumerator(); - /// - /// Returns an enumerator used to traverse the data of a specified section. - /// - /// An enumerator used to traverse the data of a specified section. - IEnumerator IEnumerable.GetEnumerator() - => GetEnumerator(); + /// + /// Returns an enumerator used to traverse the data of a specified section. + /// + /// An enumerator used to traverse the data of a specified section. + IEnumerator IEnumerable.GetEnumerator() + => GetEnumerator(); - /// - /// Converts the instance to a object. - /// - /// A string that represents the current object. - public override string ToString() + /// + /// Converts the instance to a object. + /// + /// A string that represents the current object. + public override string ToString() + { + if (Count > 1) { - if (Count > 1) - { - var sb = new StringBuilder(); - sb.Append($"[{NewLine}"); - foreach (var data in this) - sb.Append($" {data}{NewLine}"); - sb.Append($"]{NewLine}"); - return sb.ToString(); - } - return Count == 0 ? $"[]{NewLine}" : $"[{this[0]}]{NewLine}"; + var sb = new StringBuilder(); + sb.Append($"[{NewLine}"); + foreach (var data in this) + sb.Append($" {data}{NewLine}"); + sb.Append($"]{NewLine}"); + return sb.ToString(); } + return Count == 0 ? $"[]{NewLine}" : $"[{this[0]}]{NewLine}"; } } diff --git a/src/SeztionParser/Providers/SectionsData.cs b/src/SeztionParser/Providers/SectionsData.cs index 4d084ac..465b1f6 100644 --- a/src/SeztionParser/Providers/SectionsData.cs +++ b/src/SeztionParser/Providers/SectionsData.cs @@ -7,94 +7,93 @@ using System.Text; using static SeztionParser.Constants.ExceptionMessages; -namespace SeztionParser.Providers +namespace SeztionParser.Providers; + +/// +/// +/// This class uses the interface, where TKey represents the name of the section and the TValue represents the data of the section. +/// +class SectionsData : ISectionsData { - /// + /// + /// Allows access to the data of each section. + /// + /// + /// The dictionary with the data of each section. + /// + private readonly Dictionary sections = new Dictionary(); + + /// + public ISectionData this[string section] + { + get + { + if (sections.TryGetValue(section, out var value)) + return value; + throw new SectionNotFoundException(SpecifiedSectionDoesNotExistMessage, nameof(section)); + } + } + + /// + /// Adds data to a section. + /// + /// The name of the section to add. + /// The data to be added to the section. + /// true if the data can be added, otherwise false. /// - /// This class uses the interface, where TKey represents the name of the section and the TValue represents the data of the section. + /// If the data cannot be added, it is because the section parameter is null or the section already exists in the collection. /// - class SectionsData : ISectionsData + public bool Add(string section, ISectionData data) { - /// - /// Allows access to the data of each section. - /// - /// - /// The dictionary with the data of each section. - /// - private readonly Dictionary sections = new Dictionary(); - - /// - public ISectionData this[string section] + try { - get - { - if (sections.TryGetValue(section, out var value)) - return value; - throw new SectionNotFoundException(SpecifiedSectionDoesNotExistMessage, nameof(section)); - } + sections.Add(section, data); + return true; } - - /// - /// Adds data to a section. - /// - /// The name of the section to add. - /// The data to be added to the section. - /// true if the data can be added, otherwise false. - /// - /// If the data cannot be added, it is because the section parameter is null or the section already exists in the collection. - /// - public bool Add(string section, ISectionData data) + catch(ArgumentException) { - try - { - sections.Add(section, data); - return true; - } - catch(ArgumentException) - { - return false; - } + return false; } + } - /// - public bool TryGetData(string section, out ISectionData data) - => sections.TryGetValue(section, out data); + /// + public bool TryGetData(string section, out ISectionData data) + => sections.TryGetValue(section, out data); - /// - public ICollection GetNames() - => sections.Keys; + /// + public ICollection GetNames() + => sections.Keys; - /// - public ICollection GetAll() - => sections.Values; + /// + public ICollection GetAll() + => sections.Values; - /// - /// Return an enumerator used to traverse all sections of the collection. - /// - /// An enumerator used to traverse all sections of the collection. - public IEnumerator GetEnumerator() - { - foreach(var section in sections) - yield return new SectionModel(section.Key, section.Value); - } + /// + /// Return an enumerator used to traverse all sections of the collection. + /// + /// An enumerator used to traverse all sections of the collection. + public IEnumerator GetEnumerator() + { + foreach(var section in sections) + yield return new SectionModel(section.Key, section.Value); + } - /// - /// Return an enumerator used to traverse all sections of the collection. - /// - /// An enumerator used to traverse all sections of the collection. - IEnumerator IEnumerable.GetEnumerator() - => GetEnumerator(); + /// + /// Return an enumerator used to traverse all sections of the collection. + /// + /// An enumerator used to traverse all sections of the collection. + IEnumerator IEnumerable.GetEnumerator() + => GetEnumerator(); - /// - /// Converts the instance to a object. - /// - /// A string that represents the current object. - public override string ToString() - { - var sb = new StringBuilder(); - foreach(var section in this) - sb.Append(section.ToString()); - return sb.ToString(); - } + /// + /// Converts the instance to a object. + /// + /// A string that represents the current object. + public override string ToString() + { + var sb = new StringBuilder(); + foreach(var section in this) + sb.Append(section.ToString()); + return sb.ToString(); } } diff --git a/src/SeztionParser/Providers/SectionsParser.cs b/src/SeztionParser/Providers/SectionsParser.cs index 227f494..53ceb16 100644 --- a/src/SeztionParser/Providers/SectionsParser.cs +++ b/src/SeztionParser/Providers/SectionsParser.cs @@ -6,82 +6,81 @@ using System.Collections.Generic; using System.Text; -namespace SeztionParser.Providers +namespace SeztionParser.Providers; + +/// +/// Defines the operations that control the parser behavior. +/// +public class SectionsParser : ISectionsParser { /// - /// Defines the operations that control the parser behavior. + /// Check if the text is a comment. /// - public class SectionsParser : ISectionsParser - { - /// - /// Check if the text is a comment. - /// - /// The text to validate. - /// true if is comment, otherwise false. - protected virtual bool IsComment(string text) - => text.TrimStart()[0] == '#'; + /// The text to validate. + /// true if is comment, otherwise false. + protected virtual bool IsComment(string text) + => text.TrimStart()[0] == '#'; - /// - /// Check if the section has the correct format. - /// - /// The text to validate. - /// true if the section is correctly format, otherwise false. - protected virtual bool IsSection(ref string text) - { - text = text.Trim(); - return text[0] == '[' && text[text.Length - 1] == ']'; - } + /// + /// Check if the section has the correct format. + /// + /// The text to validate. + /// true if the section is correctly format, otherwise false. + protected virtual bool IsSection(ref string text) + { + text = text.Trim(); + return text[0] == '[' && text[text.Length - 1] == ']'; + } - /// - /// Extracts the name of the section. - /// - /// The section to extract. - /// The name of the extracted section. - protected virtual string ExtractSection(string text) - => text.Substring(1, text.Length - 2); + /// + /// Extracts the name of the section. + /// + /// The section to extract. + /// The name of the extracted section. + protected virtual string ExtractSection(string text) + => text.Substring(1, text.Length - 2); - /// - public ISectionsData Parse(string data) + /// + public ISectionsData Parse(string data) + { + if (string.IsNullOrWhiteSpace(data)) + throw Create(ExceptionMessages.DataSourceIsEmptyMessage); + var lines = data.Split(NewLine.ToCharArray()); + var sections = new SectionsData(); + SectionData sectionData = null; + string sectionName = null; + int len = lines.Length; + for (int i = 0; i < len; ++i) { - if (string.IsNullOrWhiteSpace(data)) - throw Create(ExceptionMessages.DataSourceIsEmptyMessage); - var lines = data.Split(NewLine.ToCharArray()); - var sections = new SectionsData(); - SectionData sectionData = null; - string sectionName = null; - int len = lines.Length; - for (int i = 0; i < len; ++i) + if (string.IsNullOrWhiteSpace(lines[i]) || IsComment(lines[i])) + continue; + if (IsSection(ref lines[i])) { - if (string.IsNullOrWhiteSpace(lines[i]) || IsComment(lines[i])) - continue; - if (IsSection(ref lines[i])) - { - bool IsEmptyPreviousSection = sectionData?.Count == 0; - if (IsEmptyPreviousSection) - throw Create(ExceptionMessages.SectionWithoutDataMessage, sectionName); + bool IsEmptyPreviousSection = sectionData?.Count == 0; + if (IsEmptyPreviousSection) + throw Create(ExceptionMessages.SectionWithoutDataMessage, sectionName); - sectionName = ExtractSection(lines[i]); - if (string.IsNullOrWhiteSpace(sectionName)) - throw Create(ExceptionMessages.SectionNameIsEmptyMessage); - sectionData = new SectionData(); - bool isRepeatedSection = !sections.Add(sectionName, sectionData); - if (isRepeatedSection) - throw Create(ExceptionMessages.SeccionIsRepeatedMessage, sectionName); - } - else - { - if (sectionData == null) - throw Create(ExceptionMessages.ElementThatIsNotPartAnySectionMessage, lines[i]); - sectionData.Add(lines[i]); - } + sectionName = ExtractSection(lines[i]); + if (string.IsNullOrWhiteSpace(sectionName)) + throw Create(ExceptionMessages.SectionNameIsEmptyMessage); + sectionData = new SectionData(); + bool isRepeatedSection = !sections.Add(sectionName, sectionData); + if (isRepeatedSection) + throw Create(ExceptionMessages.SeccionIsRepeatedMessage, sectionName); + } + else + { + if (sectionData == null) + throw Create(ExceptionMessages.ElementThatIsNotPartAnySectionMessage, lines[i]); + sectionData.Add(lines[i]); } - bool IsEmptyLastSection = sectionData.Count == 0; - if (IsEmptyLastSection) - throw Create(ExceptionMessages.SectionWithoutDataMessage, sectionName); - return sections; } - - private Exception Create(string message) => new ParserException(message); - private Exception Create(string message, object actualValue) => new ParserException(message, actualValue); + bool IsEmptyLastSection = sectionData.Count == 0; + if (IsEmptyLastSection) + throw Create(ExceptionMessages.SectionWithoutDataMessage, sectionName); + return sections; } + + private Exception Create(string message) => new ParserException(message); + private Exception Create(string message, object actualValue) => new ParserException(message, actualValue); } diff --git a/src/SeztionParser/SeztionParser.csproj b/src/SeztionParser/SeztionParser.csproj index 7500cc2..32028e5 100644 --- a/src/SeztionParser/SeztionParser.csproj +++ b/src/SeztionParser/SeztionParser.csproj @@ -2,6 +2,7 @@ netstandard2.0;netstandard2.1 + latest David Román Amariles Seztion Parser David Román Amariles @@ -15,7 +16,7 @@ seztion-parser is a class library used to read data from each section of a file (the file extension can be any, e.g. .INI). seztionparser-icon-nuget.png README.md - 1.0.1 + 1.0.2