Skip to content

Commit

Permalink
Renamed InternetAddress[List]TypeConverter -> InternetAddress[List]Co…
Browse files Browse the repository at this point in the history
…nverter

Also fixed coding style and XML doc comments a bit.
  • Loading branch information
jstedfast committed Aug 8, 2024
1 parent edff3b7 commit 5a4ca6e
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 162 deletions.
2 changes: 1 addition & 1 deletion MimeKit/InternetAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace MimeKit {
/// types of addresses. They typically only contain mailbox addresses, but may also
/// contain other group addresses.</para>
/// </remarks>
[TypeConverter(typeof(InternetAddressTypeConverter))]
[TypeConverter (typeof (InternetAddressConverter))]
public abstract class InternetAddress : IComparable<InternetAddress>, IEquatable<InternetAddress>
{
const string AtomSpecials = "()<>@,;:\\\".[]";
Expand Down
100 changes: 100 additions & 0 deletions MimeKit/InternetAddressConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//
// InternetAddressConverter.cs
//
// Author: Jeffrey Stedfast <jestedfa@microsoft.com>
//
// Copyright (c) 2013-2024 .NET Foundation and Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

using System;
using System.ComponentModel;
using System.Globalization;
using System.Threading;

namespace MimeKit {
/// <summary>
/// A type converter for converting between <see cref="InternetAddress"/> and string.
/// </summary>
/// <remarks>
/// Provides a way of converting between <see cref="InternetAddress"/> and <see langword="string"/>.
/// </remarks>
public class InternetAddressConverter : TypeConverter
{
static ParserOptions Options;

/// <summary>
/// Register the type converter so that it's available through <see cref="TypeDescriptor.GetConverter(object)"/>.
/// </summary>
/// <remarks>
/// Registers the type converter so that it's available through <see cref="TypeDescriptor.GetConverter(object)"/>.
/// </remarks>
/// <param name="options">The <see cref="ParserOptions"/> to use when converting from string or <see langword="null"/> to use the default options.</param>
/// <exception cref="InvalidOperationException">
/// The Register method was called more than once.
/// </exception>
public static void Register (ParserOptions options = null)
{
if (Interlocked.Exchange (ref Options, options ?? ParserOptions.Default) != null)
throw new InvalidOperationException ($"The {typeof (InternetAddressConverter)}.{nameof (Register)} method must be called only once.");

TypeDescriptor.AddAttributes (typeof (InternetAddress), new TypeConverterAttribute (typeof (InternetAddressConverter)));
}

/// <inheritdoc/>
public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof (string) || base.CanConvertFrom (context, sourceType);
}

/// <inheritdoc/>
public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof (string) || base.CanConvertTo (context, destinationType);
}

/// <inheritdoc/>
public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string text)
return InternetAddress.Parse (Options ?? ParserOptions.Default, text);

return base.ConvertFrom (context, culture, value);
}

/// <inheritdoc/>
public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof (string) && value is InternetAddress address)
return address.ToString ();

return base.ConvertTo (context, culture, value, destinationType);
}

/// <inheritdoc/>
public override bool IsValid (ITypeDescriptorContext context, object value)
{
if (value is string text)
return InternetAddress.TryParse (Options ?? ParserOptions.Default, text, out _);

return base.IsValid (context, value);
}
}
}
2 changes: 1 addition & 1 deletion MimeKit/InternetAddressList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace MimeKit {
/// types of addresses. They typically only contain mailbox addresses, but may also
/// contain other group addresses.</para>
/// </remarks>
[TypeConverter(typeof(InternetAddressListTypeConverter))]
[TypeConverter (typeof (InternetAddressListConverter))]
public class InternetAddressList : IList<InternetAddress>, IEquatable<InternetAddressList>, IComparable<InternetAddressList>
{
readonly List<InternetAddress> list = new List<InternetAddress> ();
Expand Down
100 changes: 100 additions & 0 deletions MimeKit/InternetAddressListConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//
// InternetAddressListConverter.cs
//
// Author: Jeffrey Stedfast <jestedfa@microsoft.com>
//
// Copyright (c) 2013-2024 .NET Foundation and Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

using System;
using System.ComponentModel;
using System.Globalization;
using System.Threading;

namespace MimeKit {
/// <summary>
/// A type converter for converting between <see cref="InternetAddressList"/> and string.
/// </summary>
/// <remarks>
/// Provides a way of converting between <see cref="InternetAddressList"/> and <see langword="string"/>.
/// </remarks>
public class InternetAddressListConverter : TypeConverter
{
static ParserOptions Options;

/// <summary>
/// Register the type converter so that it's available through <see cref="TypeDescriptor.GetConverter(object)"/>.
/// </summary>
/// <remarks>
/// Registers the type converter so that it's available through <see cref="TypeDescriptor.GetConverter(object)"/>.
/// </remarks>
/// <param name="options">The <see cref="ParserOptions"/> to use when converting from string or <see langword="null"/> to use the default options.</param>
/// <exception cref="InvalidOperationException">
/// The Register method was called more than once.
/// </exception>
public static void Register (ParserOptions options = null)
{
if (Interlocked.Exchange (ref Options, options ?? ParserOptions.Default) != null)
throw new InvalidOperationException ($"The {typeof (InternetAddressListConverter)}.{nameof (Register)} method must be called only once.");

TypeDescriptor.AddAttributes (typeof (InternetAddressList), new TypeConverterAttribute (typeof (InternetAddressListConverter)));
}

/// <inheritdoc/>
public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof (string) || base.CanConvertFrom (context, sourceType);
}

/// <inheritdoc/>
public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof (string) || base.CanConvertTo (context, destinationType);
}

/// <inheritdoc/>
public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string text)
return InternetAddressList.Parse (Options ?? ParserOptions.Default, text);

return base.ConvertFrom (context, culture, value);
}

/// <inheritdoc/>
public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof (string) && value is InternetAddressList list)
return list.ToString ();

return base.ConvertTo (context, culture, value, destinationType);
}

/// <inheritdoc/>
public override bool IsValid (ITypeDescriptorContext context, object value)
{
if (value is string text)
return InternetAddressList.TryParse (Options ?? ParserOptions.Default, text, out _);

return base.IsValid (context, value);
}
}
}
80 changes: 0 additions & 80 deletions MimeKit/InternetAddressListTypeConverter.cs

This file was deleted.

80 changes: 0 additions & 80 deletions MimeKit/InternetAddressTypeConverter.cs

This file was deleted.

0 comments on commit 5a4ca6e

Please sign in to comment.