/// <summary>
    /// Writes <paramref name="result"/> to <paramref name="response"/>.
    /// </summary>
    public static Task WriteResult(IDocumentWriter writer, HttpResponse response, AttachmentExecutionResult result, CancellationToken cancellation = default)
    {
        var executionResult = result.ExecutionResult;
        var attachments     = (OutgoingAttachments)result.Attachments;

        if (response.StatusCode == (int)HttpStatusCode.OK && executionResult.Errors?.Count > 0)
        {
            response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(WriteStream(writer, executionResult, response, cancellation));
        }

        if (attachments.HasPendingAttachments)
        {
            return(WriteMultipart(writer, response, executionResult, attachments, cancellation));
        }
        return(WriteStream(writer, executionResult, response, cancellation));
    }
        /// <summary>
        /// Writes <paramref name="result"/> to <paramref name="response"/>.
        /// </summary>
        public static Task WriteResult(HttpResponse response, AttachmentExecutionResult result, CancellationToken cancellation = default)
        {
            Guard.AgainstNull(nameof(response), response);
            Guard.AgainstNull(nameof(result), result);
            var executionResult = result.ExecutionResult;
            var attachments     = (OutgoingAttachments)result.Attachments;

            if (response.StatusCode == (int)HttpStatusCode.OK && executionResult.Errors?.Count > 0)
            {
                response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(WriteStream(executionResult, response, cancellation));
            }

            if (attachments.HasPendingAttachments)
            {
                return(WriteMultipart(response, executionResult, attachments, cancellation));
            }
            return(WriteStream(executionResult, response, cancellation));
        }