Path: redb-route/redb.Route.Xml/Contracts.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.Xml/Contracts.cs
----
using System.Xml.Linq;
using redb.Route.Abstractions;
namespace redb.Route.Xml;
///
/// What kind of element a contribution declares. The kind is metadata for the schema and the
/// catalog and for the editor; dispatch itself is uniform — every contribution gets full
/// control of its element through and the traversal
/// helpers on .
///
public enum XmlElementKind
{
/// A leaf step (<to>, <setHeader>).
Step,
/// A scope whose children are steps (<filter>, <split>).
Scope,
/// A node whose children are branches, not steps (<choice>).
Branching,
/// A node whose children configure it rather than being steps (rich <log>).
ConfigChild,
/// An element that lives beside <route> (<rest>, future).
TopLevel,
///
/// An element that lives inside <context> beside <components>/
/// <bean>/<onInit> (the redb bridge above all). Never a step.
///
ContextLevel,
}
/// The value kind of one XML attribute — what the schema and the editor say about it.
public enum AttributeType
{
/// A literal string (a name, a key, a literal value).
String,
/// A route-language expression or ${…} template — a string to the schema, annotated for the editor.
Expression,
/// An endpoint URI (may carry {{…}} placeholders).
Uri,
/// A #name reference into the context registry.
Reference,
/// A CLR type name (Full.Name, Assembly).
TypeName,
/// true/false.
Bool,
/// A whole number.
Int,
/// A whole number, 64-bit.
Long,
/// A floating-point number, invariant culture.
Double,
/// A (hh:mm:ss / hh:mm:ss.fff).
Duration,
/// One of a fixed set of names — .
Enum,
///
/// A comma-separated list of CLR type names (exceptions=): the comma SEPARATES entries,
/// so an entry is a full name without an assembly suffix. Distinct from
/// because the pack gate resolves each entry, and an assembly-qualified single name also has
/// a comma. Appended LAST on purpose: package contributions compiled against an earlier
/// version store the numeric values of the members above, so an insertion would shift them.
///
TypeNameList,
}
/// One attribute of a format element — the schema/catalog/editor description.
public sealed record AttributeSpec(
string Name,
AttributeType Type,
bool Required = false,
IReadOnlyList? EnumValues = null)
{
///
/// The attribute is read as a CONDITION (a predicate), not as a value: <filter expr>,
/// <when expr>, <loop while>. The pack gate reads this to warn about a
/// condition written as a template, which is true whatever it renders. An init property, never a
/// positional parameter: a released package contribution is compiled against this record.
///
public bool Condition { get; init; }
///
/// The attribute is a KEY that may be a constant: a value without ${...} that does not start with a root of
/// the exchange (header., property., body, exception.), a namespace (bean:,
/// ref:, env:), a function call or a literal is the key as written (<cache key="orders-all">);
/// any other value is an expression. The pack gate checks it by that rule. An init property, never a positional
/// parameter.
///
public bool ConstantKey { get; init; }
///
/// The attribute names a FILE the runtime reads from the package's resources/ — a
/// template, a transformation spec. The pack gate checks the file is there, exactly as it
/// checks the format's own file=; a package element whose attribute is named
/// differently (<payload template>, <transformJson spec>) says so
/// here. An init property, never a positional parameter: a released contribution compiled
/// against this record keeps working, it only goes unchecked until it opts in.
///
public bool Resource { get; init; }
///
/// For a : the CLR type (Full.Name, Assembly) the
/// registry object it names must be assignable to — <idempotentConsumer repository>
/// names an IIdempotentRepository. The pack gate with the built assemblies at hand
/// holds the declared object against it, so a bean of the wrong type is a build error, not a
/// start-up refusal. An init property: a released contribution keeps working unchecked.
///
public string? References { get; init; }
///
/// The attribute's value is a registry NAME this element registers an object under, of the
/// CLR type given here (<redb><idempotentRepository name> registers a redb
/// idempotent repository). The pack gate counts it as a declaration, exactly like a
/// <bean name>, and knows the object's type for checks.
///
public string? Registers { get; init; }
}
///
/// The public description of one format element: the single source the parser hints,
/// the XSD generator and the catalog all read, so they cannot drift apart. Branch and
/// configuration children (<when>, <param>, …) are nested specs —
/// they are parsed inside their parent's contribution and have no registry entry of their own.
///
public sealed record ElementSpec(
string Name,
XmlElementKind Kind,
IReadOnlyList Attributes,
IReadOnlyList Children,
bool AllowsSteps = false,
bool AllowsTextContent = false,
bool TakesEndpoint = false)
{
///
/// The step ends the route for the exchange (stop, rollbackAll,
/// throwException): a step written after it in the same list never runs, and the pack
/// gate says so. An init-only property, not a constructor parameter, so a package
/// contribution compiled against an earlier version keeps binding.
///
public bool Terminal { get; init; }
///
/// The element takes attributes beyond : the options of the type it names (a serializer's
/// delimiter=";" on <marshal type="…"> or <dataFormat>), which only that type knows. The
/// schema lets them through and the loader holds each against the type's writable properties. Init-only, as
/// .
///
public bool OpenAttributes { get; init; }
/// A leaf with attributes only.
public static ElementSpec Leaf(string name, params AttributeSpec[] attributes)
=> new(name, XmlElementKind.Step, attributes, []);
/// A scope: attributes plus the step group as content.
public static ElementSpec Scope(string name, params AttributeSpec[] attributes)
=> new(name, XmlElementKind.Scope, attributes, [], AllowsSteps: true);
/// A configuration/branch child, declared inline by its parent.
public static ElementSpec Child(string name, bool allowsSteps, params AttributeSpec[] attributes)
=> new(name, XmlElementKind.ConfigChild, attributes, [], AllowsSteps: allowsSteps);
}
///
/// One XML element of the route format: its name, kind, and the parse action that turns the
/// element into calls on the existing fluent DSL. The core registers its own elements through
/// this same contract; a package with DSL of its own (`Cache`, `Templates`, …) registers its
/// elements via . A contribution only calls DSL —
/// it never carries semantics of its own.
///
public interface IXmlElementContribution
{
/// The element's local name (to, filter). Case-sensitive, camelCase.
string Name { get; }
/// The element's kind — metadata for the schema, catalog and editor.
XmlElementKind Kind { get; }
///
/// The element's public description for the schema, the catalog and the editor. The
/// default is an opaque spec (name and kind, nothing else) so existing contributions keep
/// working; a contribution that wants autocompletion and validation declares the full shape.
///
ElementSpec Spec => new(Name, Kind, [], [], AllowsSteps: Kind is XmlElementKind.Scope);
///
/// Applies the element to the route under construction and returns the current position for
/// the next sibling. A scope opens itself, parses its children through
/// , and returns the closed scope's parent. On a
/// recoverable problem the contribution records it via
/// and returns unchanged — the traversal collects every error of
/// the document in one pass.
///
IRouteDefinition Apply(XElement element, IRouteDefinition current, XmlParseContext context);
///
/// Prints the element as the fluent C# it parses into — the third face of the
/// contribution beside and , so the parser, the
/// schema and the generator cannot drift apart. The default refuses LOUDLY: a package
/// element without a printer is an explicit generator error, never a silent skip.
///
void Print(XElement element, XmlCodeWriter code) => throw new NotSupportedException(
$"element <{Name}> is not supported by the code generator " +
$"(contribution {GetType().FullName} declares no printer).");
///
/// The namespaces the printed C# needs (using lines the generator adds to the file
/// shell when this contribution is registered). Empty by default — a printer whose verbs
/// live outside the core namespaces declares its own.
///
IReadOnlyList GeneratedUsings => [];
}
///
/// A contribution whose element lives at the container level, beside <route>
/// ( — the REST DSL above all): it applies to the route
/// BUILDER, not to a route under construction. Parsed with the container handlers, before the
/// route elements, inside the builder's Configure.
///
public interface IXmlTopLevelContribution : IXmlElementContribution
{
/// Applies the container-level element to the builder.
void ApplyTopLevel(XElement element, redb.Route.Core.RouteBuilder builder, XmlParseContext context);
}
///
/// A contribution whose element lives at the CONTEXT level, inside <context>
/// ( — the redb bridge above all): it applies to the
/// route CONTEXT during context.xml loading, before any route document. Parsed by
/// XmlContextLoader; in route position the element is an error.
///
public interface IXmlContextContribution : IXmlElementContribution
{
/// Applies the context-level element to the context being configured.
void ApplyContext(XElement element, redb.Route.Core.RouteContext context, XmlParseContext parseContext);
}
/// Options of the loader. Extensions are the registration channel for packages.
public sealed class XmlRouteLoaderOptions
{
/// Element contributions supplied by packages, merged into the core registry.
public IReadOnlyList Extensions { get; init; } = [];
///
/// Validates every document against the registry's generated XSD during load (on
/// by default). The parser's own findings win on a shared line; the schema adds what the
/// parser deliberately leaves to it — an unqualified attribute typo above all. Turn off for
/// documents that carry a newer schema's ##other extensions on purpose.
///
public bool ValidateAgainstSchema { get; init; } = true;
}
///
/// Every problem of one document, collected in one pass — file, line and column per entry, so
/// fixing ten typos does not take ten runs.
///
public sealed class XmlRouteException : Exception
{
/// The individual problems, each carrying its position.
public IReadOnlyList Errors { get; }
internal XmlRouteException(string sourceName, IReadOnlyList errors)
: base($"{sourceName}: {errors.Count} error(s) in the route document:{Environment.NewLine}" +
string.Join(Environment.NewLine, errors))
=> Errors = errors;
}