///|
/// A namespace declaration in scope: a prefix, `""` for the default namespace, and its URI, as
/// System.Xaml `NamespaceDeclaration`.
#derive(Eq, Debug)
pub(all) struct NamespaceDeclaration {
prefix : String
namespace_uri : String
}
///|
/// An object or other value that a name resolver found, with whether its initialization has completed.
#derive(Eq, Debug)
pub(all) struct ResolvedName {
value : @nodes.XamlValue
fully_initialized : Bool
}
///|
/// The name services of a load, as System.Xaml `IXamlNameResolver` gives them (design D15): lookup
/// with the initialization state, and fix-up tokens for names that are not available yet. It offers
/// nothing else: no enumeration of names and no completion events.
pub struct NameResolver {
priv lookup : (String) -> ResolvedName? raise
priv token : (ReadOnlyArray[String], Bool) -> @nodes.XamlObject? raise
}
///|
/// A name resolver over `resolve`, which finds a registered value by name, and `fixup_token`, which
/// gives a token for the names and whether the token may be assigned directly, or `None` once token
/// creation is disabled. Both can raise, because the operations of a name scope can.
pub fn NameResolver::new(
resolve~ : (String) -> ResolvedName? raise,
fixup_token~ : (ReadOnlyArray[String], Bool) -> @nodes.XamlObject? raise,
) -> NameResolver {
{ lookup: resolve, token: fixup_token, }
}
///|
/// The value registered as `name` in the first scope of the request's scope chain that has the name. Until the
/// document ends, only a fully initialized one is given. After that `fully_initialized` tells.
pub fn NameResolver::resolve(
self : NameResolver,
name : String,
) -> ResolvedName? raise {
(self.lookup)(name)
}
///|
/// A fix-up token that waits for every name in `names`, as `GetFixupToken(names, canAssignDirectly)`.
/// A markup extension or converter returns it as its value. A token that may be assigned directly
/// takes exactly one name. `None` means token creation is disabled, as at the end of a load.
pub fn NameResolver::fixup_token(
self : NameResolver,
names : ArrayView[String],
can_assign_directly~ : Bool = false,
) -> @nodes.XamlObject? raise {
(self.token)(ReadOnlyArray::from_array(names), can_assign_directly)
}
///|
/// The services that markup extensions, type converters and value serializers receive (design D14).
///
/// Each field is the counterpart of one System.Xaml service. `schema_context` is always present.
/// A load context has the load services: the target fields (`IProvideValueTarget`, absent when
/// there is no destination), `root_object` (`IRootObjectProvider`), `resolve_type`
/// (`IXamlTypeResolver`), `resolve_prefix` and `prefixes` (`IXamlNamespaceResolver`),
/// `name_resolver` (`IXamlNameResolver`), `parents`, the objects of the enclosing elements and extensions
/// from the nearest outward, and `base_uri`, the root `xml:base` (`IUriContext`). A save context has
/// `lookup_prefix` (`INamespacePrefixLookup`) and `name_provider` (`IXamlNameProvider`). The services
/// of the other direction are absent.
///
/// A context that is kept after the call holds the objects in its fields, and after the load its name resolver
/// finds no name.
pub struct ServiceContext {
schema_context : SchemaContext
/// The object whose member receives the value, or the collection or dictionary of an item.
target_object : @nodes.XamlObject?
/// The member that receives the value.
target_member : XamlMember?
root_object : @nodes.XamlObject?
/// Resolve `prefix:Name`, type arguments included, to the identity of a registered type.
resolve_type : ((String) -> @nodes.XamlTypeName?)?
/// The namespace URI that a prefix is bound to in scope.
resolve_prefix : ((String) -> String?)?
/// The namespace declarations in scope.
prefixes : (() -> ReadOnlyArray[NamespaceDeclaration])?
name_resolver : NameResolver?
/// The objects of the enclosing elements and extensions, the nearest first. Those that do not exist yet and
/// collections that a getter gives are not in it.
parents : @list.List[@nodes.XamlObject]
base_uri : XamlUri?
/// The prefix of a namespace in the saved document. A namespace not yet seen is given a prefix,
/// which the document declares on its root.
lookup_prefix : ((String) -> String)?
/// The name of a referenced object in the saved document, generated when it has none.
name_provider : ((@nodes.XamlObject) -> String)?
}
///|
/// A context with the schema context only, as converters receive at `build()` and where no other
/// service exists.
pub fn ServiceContext::new(schema_context : SchemaContext) -> ServiceContext {
{
schema_context,
target_object: None,
target_member: None,
root_object: None,
resolve_type: None,
resolve_prefix: None,
prefixes: None,
name_resolver: None,
parents: @list.List([]),
base_uri: None,
lookup_prefix: None,
name_provider: None,
}
}
///|
/// A context for loading. Without `resolve_type`, a type name is parsed with `resolve_prefix` and
/// resolves to the identity of the registered type that it names, type arguments included.
pub fn ServiceContext::for_load(
schema_context : SchemaContext,
resolve_prefix~ : (String) -> String?,
prefixes~ : () -> ReadOnlyArray[NamespaceDeclaration],
resolve_type? : (String) -> @nodes.XamlTypeName?,
target_object? : @nodes.XamlObject,
target_member? : XamlMember,
root_object? : @nodes.XamlObject,
name_resolver? : NameResolver,
parents~ : @list.List[@nodes.XamlObject] = @list.List([]),
base_uri? : XamlUri,
) -> ServiceContext {
let resolve_type = match resolve_type {
Some(resolve) => resolve
None =>
text => {
let name = @nodes.XamlTypeName::parse(text, resolve_prefix) catch {
_ => return None
}
schema_context.get_type(name).map(t => t.name)
}
}
{
..ServiceContext::new(schema_context),
target_object,
target_member,
root_object,
resolve_type: Some(resolve_type),
resolve_prefix: Some(resolve_prefix),
prefixes: Some(prefixes),
name_resolver,
parents,
base_uri,
}
}
///|
/// A context for saving, with the prefix lookup and the name provider of the save.
pub fn ServiceContext::for_save(
schema_context : SchemaContext,
lookup_prefix~ : (String) -> String,
name_provider~ : (@nodes.XamlObject) -> String,
) -> ServiceContext {
{
..ServiceContext::new(schema_context),
lookup_prefix: Some(lookup_prefix),
name_provider: Some(name_provider),
}
}