Path: redb-route/redb.Route/Core/MessageAttachments.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Core/MessageAttachments.cs
----
using System.Collections;
using redb.Route.Abstractions;
namespace redb.Route.Core;
/// The attachment list of a .
internal sealed class MessageAttachments : IMessageAttachments
{
///
/// What a message that does not implement attachments answers with: it carries none, and adding one fails instead of
/// being dropped.
///
internal static readonly IMessageAttachments Unsupported = new UnsupportedAttachments();
private readonly List _items;
public MessageAttachments() => _items = [];
private MessageAttachments(List items) => _items = items;
public int Count => _items.Count;
public Attachment this[int index] => _items[index];
public Attachment? Find(string name)
{
ArgumentNullException.ThrowIfNull(name);
foreach (var item in _items)
if (string.Equals(item.Name, name, StringComparison.Ordinal))
return item;
return null;
}
public IReadOnlyList FindAll(string name)
{
ArgumentNullException.ThrowIfNull(name);
return _items.Where(i => string.Equals(i.Name, name, StringComparison.Ordinal)).ToArray();
}
public void Add(Attachment attachment)
{
ArgumentNullException.ThrowIfNull(attachment);
_items.Add(attachment);
}
public void Set(Attachment attachment)
{
ArgumentNullException.ThrowIfNull(attachment);
var first = _items.FindIndex(i => string.Equals(i.Name, attachment.Name, StringComparison.Ordinal));
if (first < 0)
{
_items.Add(attachment);
return;
}
_items[first] = attachment;
for (var i = _items.Count - 1; i > first; i--)
if (string.Equals(_items[i].Name, attachment.Name, StringComparison.Ordinal))
_items.RemoveAt(i);
}
public int Remove(string name)
{
ArgumentNullException.ThrowIfNull(name);
return _items.RemoveAll(i => string.Equals(i.Name, name, StringComparison.Ordinal));
}
public void Clear() => _items.Clear();
/// Same attachments in a new list (the content sources are shared), as shares the body.
public MessageAttachments Copy() => new([.. _items]);
/// Same attachments with isolated content, as deep-copies the body.
public MessageAttachments DeepCopy() => new(_items.Select(i => i.WithContent(i.Content.DeepCopy())).ToList());
/// Appends the attachments of to , sharing their content.
internal static void CopyInto(IMessage source, IMessage target)
{
if (source is not Message { AttachmentsOrNull: { } items })
return;
var into = target.Attachments;
foreach (var item in items)
into.Add(item);
}
///
/// The pipeline's Out-to-In rule: when carries attachments they replace those of
/// ; a step that set no attachments on its Out leaves the In ones in place.
///
internal static void ReplaceWhenPresent(IMessage source, IMessage target)
{
if (source is not Message { AttachmentsOrNull: { } items })
return;
var into = target.Attachments;
into.Clear();
foreach (var item in items)
into.Add(item);
}
/// Total length of the attachment content of ; 0 without attachments.
internal static long TotalLength(IMessage message) =>
message is Message { AttachmentsOrNull: { } items } ? items.TotalLength() : 0;
/// Total length of the content of all attachments.
public long TotalLength() => _items.Sum(i => i.Content.Length);
public IEnumerator GetEnumerator() => _items.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
private sealed class UnsupportedAttachments : IMessageAttachments
{
public int Count => 0;
public Attachment this[int index] => throw new ArgumentOutOfRangeException(nameof(index));
public Attachment? Find(string name) => null;
public IReadOnlyList FindAll(string name) => [];
public void Add(Attachment attachment) => throw NotSupported();
public void Set(Attachment attachment) => throw NotSupported();
public int Remove(string name) => 0;
public void Clear() { }
public IEnumerator GetEnumerator() => Enumerable.Empty().GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
private static NotSupportedException NotSupported() =>
new("This message type does not carry attachments; use redb.Route.Core.Message.");
}
}