File: src\roslyn\src\Dependencies\Contracts\Contract.InterpolatedStringHandlers.cs
Web Access
Project: src\src\roslyn\src\Workspaces\SharedUtilitiesAndExtensions\Compiler\Extensions\Microsoft.CodeAnalysis.Extensions.Package.csproj (Microsoft.CodeAnalysis.Extensions.Package)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

#if !MICROSOFT_CODEANALYSIS_CONTRACTS_NO_CONTRACT

using System.Runtime.CompilerServices;
using System.Text;

#pragma warning disable IDE0060 // Remove unused parameter - https://github.com/dotnet/roslyn/issues/58168

namespace Microsoft.CodeAnalysis;

internal static partial class Contract
{
    [InterpolatedStringHandler]
    public readonly struct ThrowIfTrueInterpolatedStringHandler
    {
        private readonly StringBuilder _stringBuilder;

        public ThrowIfTrueInterpolatedStringHandler(int literalLength, int formattedCount, bool condition, out bool success)
        {
            _stringBuilder = condition ? new StringBuilder(capacity: literalLength) : null!;
            success = condition;
        }

        public void AppendLiteral(string value) => _stringBuilder.Append(value);

        public void AppendFormatted<T>(T value) => _stringBuilder.Append(value?.ToString());

        public string GetFormattedText() => _stringBuilder.ToString();
    }

    [InterpolatedStringHandler]
    public readonly struct ThrowIfFalseInterpolatedStringHandler
    {
        private readonly StringBuilder _stringBuilder;

        public ThrowIfFalseInterpolatedStringHandler(int literalLength, int formattedCount, bool condition, out bool success)
        {
            _stringBuilder = condition ? null! : new StringBuilder(capacity: literalLength);
            success = !condition;
        }

        public void AppendLiteral(string value) => _stringBuilder.Append(value);

        public void AppendFormatted<T>(T value) => _stringBuilder.Append(value?.ToString());

        public string GetFormattedText() => _stringBuilder.ToString();
    }

    [InterpolatedStringHandler]
    public readonly struct ThrowIfNullInterpolatedStringHandler<T>
    {
        private readonly StringBuilder _stringBuilder;

        public ThrowIfNullInterpolatedStringHandler(int literalLength, int formattedCount, T? value, out bool success)
        {
            _stringBuilder = value is null ? new StringBuilder(capacity: literalLength) : null!;
            success = value is null;
        }

        public void AppendLiteral(string value) => _stringBuilder.Append(value);

        public void AppendFormatted<T2>(T2 value) => _stringBuilder.Append(value?.ToString());

        public string GetFormattedText() => _stringBuilder.ToString();
    }
}

#endif