Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] Added file-scoped namespace declarations #4

Merged
merged 2 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*.cs]
csharp_style_namespace_declarations=file_scoped:suggestion
37 changes: 18 additions & 19 deletions src/SeztionParser/Constants/ExceptionMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@
using System.Collections.Generic;
using System.Text;

namespace SeztionParser.Constants
namespace SeztionParser.Constants;

/// <summary>
/// Represents error messages that can be used by the library.
/// </summary>
public class ExceptionMessages
{
/// <summary>
/// Represents error messages that can be used by the library.
/// </summary>
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";
}
59 changes: 29 additions & 30 deletions src/SeztionParser/Exceptions/ParserException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,40 @@
using System.Collections.Generic;
using System.Text;

namespace SeztionParser.Exceptions
namespace SeztionParser.Exceptions;

/// <summary>
/// The exception that is thrown when the parser finds an error during the parsing, such as a duplicate section.
/// </summary>
public class ParserException : Exception
{
private readonly object actualValue;

/// <summary>
/// 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.
/// </summary>
public class ParserException : Exception
{
private readonly object actualValue;

/// <summary>
/// Allows access to the actual value causing the exception.
/// </summary>
public object ActualValue => actualValue;
public object ActualValue => actualValue;

/// <summary>
/// Initializes a new instance of the <see cref="ParserException" /> class with a specified error message.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public ParserException(string message) : base(message)
{

}
/// <summary>
/// Initializes a new instance of the <see cref="ParserException" /> class with a specified error message.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public ParserException(string message) : base(message)
{

/// <summary>
/// Initializes a new instance of the <see cref="ParserException" /> class with a specified error message and the actual value that caused the exception.
/// </summary>
/// <param name="message">The message that describes the error.</param>
/// <param name="actualValue">The actual value that caused the exception.</param>
public ParserException(string message, object actualValue) : base(message)
{
this.actualValue = actualValue;
}
}

/// <inheritdoc />
public override string Message
=> actualValue != null ? base.Message + " (Actual Value: " + actualValue + ")" : base.Message;
/// <summary>
/// Initializes a new instance of the <see cref="ParserException" /> class with a specified error message and the actual value that caused the exception.
/// </summary>
/// <param name="message">The message that describes the error.</param>
/// <param name="actualValue">The actual value that caused the exception.</param>
public ParserException(string message, object actualValue) : base(message)
{
this.actualValue = actualValue;
}

/// <inheritdoc />
public override string Message
=> actualValue != null ? base.Message + " (Actual Value: " + actualValue + ")" : base.Message;
}
21 changes: 10 additions & 11 deletions src/SeztionParser/Exceptions/SectionNotFoundException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
using System.Collections.Generic;
using System.Text;

namespace SeztionParser.Exceptions
namespace SeztionParser.Exceptions;

/// <summary>
/// The exception that is thrown when the specified section does not exist in the collection.
/// </summary>
public class SectionNotFoundException : ArgumentException
{
/// <summary>
/// The exception that is thrown when the specified section does not exist in the collection.
/// Initializes a new instance of the <see cref="SectionNotFoundException" /> class with a specified error message and the name of the parameter that causes this exception.
/// </summary>
public class SectionNotFoundException : ArgumentException
/// <param name="message">The message that describes the error.</param>
/// <param name="paramName">The name of the parameter that caused the exception.</param>
public SectionNotFoundException(string message, string paramName) : base(message, paramName)
{
/// <summary>
/// Initializes a new instance of the <see cref="SectionNotFoundException" /> class with a specified error message and the name of the parameter that causes this exception.
/// </summary>
/// <param name="message">The message that describes the error.</param>
/// <param name="paramName">The name of the parameter that caused the exception.</param>
public SectionNotFoundException(string message, string paramName) : base(message, paramName)
{

}
}
}
27 changes: 13 additions & 14 deletions src/SeztionParser/Facades/SectionsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
using System.IO;
using System.Text;

namespace SeztionParser.Facades
namespace SeztionParser.Facades;

/// <summary>
/// Defines the operations that can be applied to a sections file.
/// </summary>
public static class SectionsFile
{
/// <inheritdoc cref="File.ReadAllText(string)" path="/exception"></inheritdoc>
/// <inheritdoc cref="ISectionsParser.Parse" path="/exception"></inheritdoc>
/// <summary>
/// Defines the operations that can be applied to a sections file.
/// Load the sections file.
/// </summary>
public static class SectionsFile
{
/// <inheritdoc cref="File.ReadAllText(string)" path="/exception"></inheritdoc>
/// <inheritdoc cref="ISectionsParser.Parse" path="/exception"></inheritdoc>
/// <summary>
/// Load the sections file.
/// </summary>
/// <param name="path">The path of the file to load.</param>
/// <returns>An instance with the data of each section.</returns>
public static ISectionsData Load(string path)
=> new SectionsParser().Parse(File.ReadAllText(path));
}
/// <param name="path">The path of the file to load.</param>
/// <returns>An instance with the data of each section.</returns>
public static ISectionsData Load(string path)
=> new SectionsParser().Parse(File.ReadAllText(path));
}
147 changes: 73 additions & 74 deletions src/SeztionParser/Helpers/SectionDataConversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,87 +6,86 @@
using System.Globalization;
using SeztionParser.Exceptions;

namespace SeztionParser.Helpers
namespace SeztionParser.Helpers;

/// <summary>
/// Defines operations that serve to convert a section data to another data type.
/// </summary>
public static class SectionDataConversion
{
/// <summary>
/// Defines operations that serve to convert a section data to another data type.
/// Convert section data to <c>decimal</c>.
/// </summary>
public static class SectionDataConversion
/// <param name="sections">The data of sections.</param>
/// <param name="sectionName">The name of the section to convert.</param>
/// <param name="provider">An object that provides culture-specific formatting information.</param>
/// <exception cref="FormatException">If the data in the section are not <c>decimals</c>.</exception>
/// <exception cref="SectionNotFoundException">If the <c>section</c> does not exist.</exception>
/// <returns>The section data in <c>decimal</c> format.</returns>
/// <remarks><c>provider</c> is <c>null</c>, the default culture is <see cref="CultureInfo.InvariantCulture" />.</remarks>
public static IEnumerable<decimal> ToDecimal(this ISectionsData sections, string sectionName, IFormatProvider provider = null)
{
/// <summary>
/// Convert section data to <c>decimal</c>.
/// </summary>
/// <param name="sections">The data of sections.</param>
/// <param name="sectionName">The name of the section to convert.</param>
/// <param name="provider">An object that provides culture-specific formatting information.</param>
/// <exception cref="FormatException">If the data in the section are not <c>decimals</c>.</exception>
/// <exception cref="SectionNotFoundException">If the <c>section</c> does not exist.</exception>
/// <returns>The section data in <c>decimal</c> format.</returns>
/// <remarks><c>provider</c> is <c>null</c>, the default culture is <see cref="CultureInfo.InvariantCulture" />.</remarks>
public static IEnumerable<decimal> 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);
}

/// <summary>
/// Convert section data to <c>doubles</c>.
/// </summary>
/// <param name="sections">The data of sections.</param>
/// <param name="sectionName">The name of the section to convert.</param>
/// <param name="provider">An object that provides culture-specific formatting information.</param>
/// <exception cref="FormatException">If the data in the section are not <c>doubles</c>.</exception>
/// <exception cref="SectionNotFoundException">If the <c>section</c> does not exist.</exception>
/// <returns>The section data in <c>double</c> format.</returns>
/// <remarks><c>provider</c> is <c>null</c>, the default culture is <see cref="CultureInfo.InvariantCulture" />.</remarks>
public static IEnumerable<double> ToDouble(this ISectionsData sections, string sectionName, IFormatProvider provider = null)
{
foreach (var data in sections[sectionName])
yield return double.Parse(data, provider ?? CultureInfo.InvariantCulture);
}
/// <summary>
/// Convert section data to <c>doubles</c>.
/// </summary>
/// <param name="sections">The data of sections.</param>
/// <param name="sectionName">The name of the section to convert.</param>
/// <param name="provider">An object that provides culture-specific formatting information.</param>
/// <exception cref="FormatException">If the data in the section are not <c>doubles</c>.</exception>
/// <exception cref="SectionNotFoundException">If the <c>section</c> does not exist.</exception>
/// <returns>The section data in <c>double</c> format.</returns>
/// <remarks><c>provider</c> is <c>null</c>, the default culture is <see cref="CultureInfo.InvariantCulture" />.</remarks>
public static IEnumerable<double> ToDouble(this ISectionsData sections, string sectionName, IFormatProvider provider = null)
{
foreach (var data in sections[sectionName])
yield return double.Parse(data, provider ?? CultureInfo.InvariantCulture);
}

/// <summary>
/// Convert section data to <c>floats</c>.
/// </summary>
/// <param name="sections">The data of sections.</param>
/// <param name="sectionName">The name of the section to convert.</param>
/// <param name="provider">An object that provides culture-specific formatting information.</param>
/// <exception cref="FormatException">If the data in the section are not <c>floats</c>.</exception>
/// <exception cref="SectionNotFoundException">If the <c>section</c> does not exist.</exception>
/// <returns>The section data in <c>float</c> format.</returns>
/// <remarks><c>provider</c> is <c>null</c>, the default culture is <see cref="CultureInfo.InvariantCulture" />.</remarks>
public static IEnumerable<float> ToFloat(this ISectionsData sections, string sectionName, IFormatProvider provider = null)
{
foreach (var data in sections[sectionName])
yield return float.Parse(data, provider ?? CultureInfo.InvariantCulture);
}
/// <summary>
/// Convert section data to <c>floats</c>.
/// </summary>
/// <param name="sections">The data of sections.</param>
/// <param name="sectionName">The name of the section to convert.</param>
/// <param name="provider">An object that provides culture-specific formatting information.</param>
/// <exception cref="FormatException">If the data in the section are not <c>floats</c>.</exception>
/// <exception cref="SectionNotFoundException">If the <c>section</c> does not exist.</exception>
/// <returns>The section data in <c>float</c> format.</returns>
/// <remarks><c>provider</c> is <c>null</c>, the default culture is <see cref="CultureInfo.InvariantCulture" />.</remarks>
public static IEnumerable<float> ToFloat(this ISectionsData sections, string sectionName, IFormatProvider provider = null)
{
foreach (var data in sections[sectionName])
yield return float.Parse(data, provider ?? CultureInfo.InvariantCulture);
}

/// <summary>
/// Convert section data to <c>integers</c>.
/// </summary>
/// <param name="sections">The data of sections.</param>
/// <param name="sectionName">The name of the section to convert.</param>
/// <exception cref="FormatException">If the data in the section are not <c>integers</c>.</exception>
/// <exception cref="SectionNotFoundException">If the <c>section</c> does not exist.</exception>
/// <returns>The section data in <c>integer</c> format.</returns>
public static IEnumerable<int> ToInt(this ISectionsData sections, string sectionName)
{
foreach (var data in sections[sectionName])
yield return int.Parse(data);
}
/// <summary>
/// Convert section data to <c>integers</c>.
/// </summary>
/// <param name="sections">The data of sections.</param>
/// <param name="sectionName">The name of the section to convert.</param>
/// <exception cref="FormatException">If the data in the section are not <c>integers</c>.</exception>
/// <exception cref="SectionNotFoundException">If the <c>section</c> does not exist.</exception>
/// <returns>The section data in <c>integer</c> format.</returns>
public static IEnumerable<int> ToInt(this ISectionsData sections, string sectionName)
{
foreach (var data in sections[sectionName])
yield return int.Parse(data);
}

/// <summary>
/// Convert section data to <c>longs</c>.
/// </summary>
/// <param name="sections">The data of sections.</param>
/// <param name="sectionName">The name of the section to convert.</param>
/// <exception cref="FormatException">If the data in the section are not <c>longs</c>.</exception>
/// <exception cref="SectionNotFoundException">If the <c>section</c> does not exist.</exception>
/// <returns>The section data in <c>long</c> format.</returns>
public static IEnumerable<long> ToLong(this ISectionsData sections, string sectionName)
{
foreach (var data in sections[sectionName])
yield return long.Parse(data);
}
/// <summary>
/// Convert section data to <c>longs</c>.
/// </summary>
/// <param name="sections">The data of sections.</param>
/// <param name="sectionName">The name of the section to convert.</param>
/// <exception cref="FormatException">If the data in the section are not <c>longs</c>.</exception>
/// <exception cref="SectionNotFoundException">If the <c>section</c> does not exist.</exception>
/// <returns>The section data in <c>long</c> format.</returns>
public static IEnumerable<long> ToLong(this ISectionsData sections, string sectionName)
{
foreach (var data in sections[sectionName])
yield return long.Parse(data);
}
}
Loading