Skip to content

Commit

Permalink
add IFormattable
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasteles committed May 24, 2023
1 parent a428728 commit f320a4f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/Cnpj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace BrazilModels;
[TypeConverter(typeof(StringTypeConverter<Cnpj>))]
[Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(StringSchemaFilter))]
[DebuggerDisplay("{DebuggerDisplay(),nq}")]
public readonly record struct Cnpj : IComparable<Cnpj>
public readonly record struct Cnpj : IComparable<Cnpj>, IFormattable
{
/// <summary>
/// CNPJ Size
Expand Down Expand Up @@ -82,6 +82,10 @@ public Cnpj(in ReadOnlySpan<char> value) : this(value, true) { }
/// <returns>CNPJ as string</returns>
public string ToString(bool withMask) => Format(Value, withMask);

/// <inheritdoc />
string IFormattable.ToString(string? format, IFormatProvider? formatProvider) =>
Value.ToString(formatProvider);

static Exception CnpjException(in ReadOnlySpan<char> value) =>
new FormatException($"Invalid CNPJ: {value}");

Expand Down
6 changes: 5 additions & 1 deletion src/Cpf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace BrazilModels;
[TypeConverter(typeof(StringTypeConverter<Cpf>))]
[Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(StringSchemaFilter))]
[DebuggerDisplay("{DebuggerDisplay(),nq}")]
public readonly record struct Cpf : IComparable<Cpf>
public readonly record struct Cpf : IComparable<Cpf>, IFormattable
{
/// <summary>
/// CPF Size
Expand Down Expand Up @@ -72,6 +72,10 @@ public Cpf(in ReadOnlySpan<char> value) : this(value, true) { }
/// <returns>CPF as string</returns>
public string ToString(bool withMask) => Format(Value, withMask);

/// <inheritdoc />
string IFormattable.ToString(string? format, IFormatProvider? formatProvider) =>
Value.ToString(formatProvider);

/// <summary>
/// Convert CPF to string representation without mask
/// </summary>
Expand Down
6 changes: 5 additions & 1 deletion src/CpfCnpj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public enum DocumentType
[TypeConverter(typeof(StringTypeConverter<CpfCnpj>))]
[Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(StringSchemaFilter))]
[DebuggerDisplay("{DebuggerDisplay(),nq}")]
public readonly record struct CpfCnpj : IComparable<CpfCnpj>
public readonly record struct CpfCnpj : IComparable<CpfCnpj>, IFormattable
{
/// <summary>
/// Defines if the Document is an CPF or CNPJ
Expand Down Expand Up @@ -98,6 +98,10 @@ public CpfCnpj(in ReadOnlySpan<char> value)
/// <returns>CPF/CNPJ as string</returns>
public string ToString(bool withMask) => Format(Value, Type, withMask);

/// <inheritdoc />
string IFormattable.ToString(string? format, IFormatProvider? formatProvider) =>
Value.ToString(formatProvider);

static Exception CpfCnpjException(in ReadOnlySpan<char> value) =>
new FormatException($"Invalid CpfCnpj: {value}");

Expand Down
2 changes: 1 addition & 1 deletion tests/BrazilModels.Tests/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
global using BrazilModels.Tests.Utils;
global using FluentAssertions;
global using NUnit.Framework;
global using PropertyTestAttribute = FsCheck.NUnit.PropertyAttribute;

5 changes: 5 additions & 0 deletions tests/BrazilModels.Tests/Utils/Generators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace BrazilModels.Tests.Utils;

public sealed class PropertyTestAttribute : FsCheck.NUnit.PropertyAttribute
{
public PropertyTestAttribute() => this.QuietOnSuccess = true;
}

public abstract record DocumentValue(string Value)
{
public static implicit operator string(DocumentValue document) => document.Value;
Expand Down

0 comments on commit f320a4f

Please sign in to comment.