diff --git a/sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs b/sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs index 6a77f926e270..f1e85527d870 100644 --- a/sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs +++ b/sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs @@ -18,6 +18,24 @@ public enum ResponseStatusOption } namespace Azure.Core { + [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] + public readonly partial struct ContentType : System.IEquatable, System.IEquatable + { + private readonly object _dummy; + private readonly int _dummyPrimitive; + public ContentType(string contentType) { throw null; } + public static Azure.Core.ContentType ApplicationJson { get { throw null; } } + public static Azure.Core.ContentType ApplicationOctetStream { get { throw null; } } + public static Azure.Core.ContentType TextPlain { get { throw null; } } + public bool Equals(Azure.Core.ContentType other) { throw null; } + public override bool Equals(object? obj) { throw null; } + public bool Equals(string other) { throw null; } + public override int GetHashCode() { throw null; } + public static bool operator ==(Azure.Core.ContentType left, Azure.Core.ContentType right) { throw null; } + public static implicit operator Azure.Core.ContentType (string contentType) { throw null; } + public static bool operator !=(Azure.Core.ContentType left, Azure.Core.ContentType right) { throw null; } + public override string ToString() { throw null; } + } [System.Diagnostics.DebuggerDisplayAttribute("Content: {_body}")] public partial class DynamicContent : Azure.Core.RequestContent { diff --git a/sdk/core/Azure.Core.Experimental/src/ContentType.cs b/sdk/core/Azure.Core.Experimental/src/ContentType.cs new file mode 100644 index 000000000000..4ed52f3803ac --- /dev/null +++ b/sdk/core/Azure.Core.Experimental/src/ContentType.cs @@ -0,0 +1,87 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; + +namespace Azure.Core +{ + /// + /// Represents content type. + /// + public readonly struct ContentType : IEquatable, IEquatable + { + private readonly string _contentType; + + /// + /// application/json + /// + public static ContentType ApplicationJson { get; } = new ContentType("application/json"); + + /// + /// application/json + /// + public static ContentType ApplicationOctetStream { get; } = new ContentType("application/octet-stream"); + + /// + /// application/json + /// + public static ContentType TextPlain { get; } = new ContentType("text/plain"); + + /// + /// Creates an instance of . + /// + /// The content type string. + public ContentType(string contentType) + { + Argument.AssertNotNull(contentType, nameof(contentType)); + + _contentType = contentType; + } + + /// + /// Creates an instance of . + /// + /// The content type string. + public static implicit operator ContentType(string contentType) => new ContentType(contentType); + + /// + public bool Equals(ContentType other) + { + return string.Equals(_contentType, other._contentType, StringComparison.Ordinal); + } + + /// + public bool Equals(string other) + => string.Equals(_contentType, _contentType, StringComparison.Ordinal); + + /// + public override bool Equals(object? obj) + => (obj is ContentType other && Equals(other)) || + (obj is string str && str.Equals(_contentType, StringComparison.Ordinal)); + + /// + public override int GetHashCode() + => _contentType?.GetHashCode() ?? 0; + + /// + /// Compares equality of two instances. + /// + /// The method to compare. + /// The method to compare against. + /// true if values are equal for and , otherwise false. + public static bool operator ==(ContentType left, ContentType right) + => left.Equals(right); + + /// + /// Compares inequality of two instances. + /// + /// The method to compare. + /// The method to compare against. + /// true if values are equal for and , otherwise false. + public static bool operator !=(ContentType left, ContentType right) + => !left.Equals(right); + + /// + public override string ToString() => _contentType == null?"":_contentType; + } +} diff --git a/sdk/core/Azure.Core.Experimental/tests/ContentTypeTests.cs b/sdk/core/Azure.Core.Experimental/tests/ContentTypeTests.cs new file mode 100644 index 000000000000..3a62aa4084be --- /dev/null +++ b/sdk/core/Azure.Core.Experimental/tests/ContentTypeTests.cs @@ -0,0 +1,43 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using NUnit.Framework; + +namespace Azure.Core.Tests +{ + public class ContentTypeTests + { + [Test] + public void Basics() + { + ContentType contentType = default; + Assert.AreEqual("", contentType.ToString()); + Assert.IsTrue(contentType.Equals(null)); + Assert.IsTrue(contentType.Equals(new ContentType())); + + string aj = "application/json"; + contentType = ContentType.ApplicationJson; + Assert.AreEqual(aj, contentType.ToString()); + Assert.IsTrue(contentType.Equals(aj)); + Assert.IsTrue(contentType.Equals(new ContentType(aj))); + Assert.IsTrue(contentType.Equals((object)aj)); + Assert.IsTrue(contentType.Equals((object)new ContentType(aj))); + + string aos = "application/octet-stream"; + contentType = ContentType.ApplicationOctetStream; + Assert.AreEqual(aos, contentType.ToString()); + Assert.IsTrue(contentType.Equals(aos)); + Assert.IsTrue(contentType.Equals(new ContentType(aos))); + Assert.IsTrue(contentType.Equals((object)aos)); + Assert.IsTrue(contentType.Equals((object)new ContentType(aos))); + + string pt = "text/plain"; + contentType = ContentType.TextPlain; + Assert.AreEqual(pt, contentType.ToString()); + Assert.IsTrue(contentType.Equals(pt)); + Assert.IsTrue(contentType.Equals(new ContentType(pt))); + Assert.IsTrue(contentType.Equals((object)pt)); + Assert.IsTrue(contentType.Equals((object)new ContentType(pt))); + } + } +}