pub struct TypeRegistry { /* private fields */ }Expand description
Maps DSL type names to Rust types for adam-lang cell declarations.
§Example
use adam_lang::TypeRegistry;
let mut reg = TypeRegistry::new();
assert!(reg.get("i32").is_some());
assert!(reg.get("unknown").is_none());Implementations§
Source§impl TypeRegistry
impl TypeRegistry
Sourcepub fn cel_type_resolver(&self) -> RegistryTypeResolver
pub fn cel_type_resolver(&self) -> RegistryTypeResolver
Builds a CEL-facing resolver snapshot from this registry’s registered leaf types.
The returned resolver owns only the name-to-descriptor metadata CEL needs for typed array
annotations, keeping sheet construction and other Adam-specific function pointers out of
cel-parser.
- Complexity: O(n) in the number of registered leaf types.
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a registry pre-populated with all built-in CEL/Rust primitive types.
Registered types: i8, i16, i32, i64, i128, isize,
u8, u16, u32, u64, u128, usize, f32, f64, bool, String.
Sourcepub fn register<T: Any + PartialEq + Default + Clone + Debug + 'static>(
&mut self,
name: &str,
)
pub fn register<T: Any + PartialEq + Default + Clone + Debug + 'static>( &mut self, name: &str, )
Registers T under name with default initialization support.
- Postcondition:
self.get(name)returnsSome(entry)withentry.default_fn.is_some().
§Examples
use adam_lang::TypeRegistry;
let mut reg = TypeRegistry::new();
reg.register::<u64>("my_u64");
assert!(reg.get("my_u64").is_some());Sourcepub fn register_no_default<T: Any + PartialEq + Clone + Debug + 'static>(
&mut self,
name: &str,
)
pub fn register_no_default<T: Any + PartialEq + Clone + Debug + 'static>( &mut self, name: &str, )
Registers T under name without default initialization support.
A cell declared as cell x: T; (no initializer) is a parse error for this type.
- Postcondition:
self.get(name)returnsSome(entry)withentry.default_fn.is_none().
§Examples
use adam_lang::TypeRegistry;
#[derive(PartialEq, Clone, Debug)]
struct MyType(i32);
let mut reg = TypeRegistry::new();
reg.register_no_default::<MyType>("MyType");
let entry = reg.get("MyType").unwrap();
assert!(entry.default_fn.is_none());Sourcepub fn get(&self, name: &str) -> Option<&TypeEntry>
pub fn get(&self, name: &str) -> Option<&TypeEntry>
Looks up a type entry by its DSL name.
Returns None if name has not been registered.
§Examples
use adam_lang::TypeRegistry;
let reg = TypeRegistry::new();
assert!(reg.get("f64").is_some());
assert!(reg.get("nonexistent").is_none());Sourcepub fn entry_by_type_id(&self, type_id: TypeId) -> Option<&TypeEntry>
pub fn entry_by_type_id(&self, type_id: TypeId) -> Option<&TypeEntry>
Looks up a type entry by its TypeId.
Returns None if no type with this TypeId has been registered.
§Examples
use adam_lang::TypeRegistry;
use std::any::TypeId;
let reg = TypeRegistry::new();
assert!(reg.entry_by_type_id(TypeId::of::<f64>()).is_some());
assert!(reg.entry_by_type_id(TypeId::of::<Vec<u8>>()).is_none());Sourcepub fn resolve(&self, expr: &TypeExpr) -> Result<TypeShape, (String, Span)>
pub fn resolve(&self, expr: &TypeExpr) -> Result<TypeShape, (String, Span)>
Resolves a parsed type_expr against this registry, recursively.
§Errors
Returns an unknown leaf name, malformed callable signature, or unrepresentable layout.
Registered generic leaves use their complete source name, e.g. RangeInclusive(i32).
- Complexity: O(n) in type nodes and registered-name bytes.
§Examples
let registry = adam_lang::TypeRegistry::new();
let ty = cel_parser::CELParser::new(cel_parser::OpLookup::new())
.parse_type_expr_str("[(i32, [f64])]").unwrap();
assert_eq!(registry.display_name(®istry.resolve(&ty).unwrap()), "[(i32, [f64])]");Sourcepub fn registered_name(&self, type_id: TypeId) -> Option<&str>
pub fn registered_name(&self, type_id: TypeId) -> Option<&str>
Returns the DSL name type_id was registered under, or None if it is unregistered.
This is the name every diagnostic should use for a live value’s type: a TypeEntry’s own
type_name is the Rust type path (alloc::string::String), not the registered name.
§Examples
use adam_lang::TypeRegistry;
let reg = TypeRegistry::new();
assert_eq!(reg.registered_name(std::any::TypeId::of::<String>()), Some("String"));
assert_eq!(reg.registered_name(std::any::TypeId::of::<Vec<u8>>()), None);Sourcepub fn display_name(&self, shape: &TypeShape) -> String
pub fn display_name(&self, shape: &TypeShape) -> String
Formats shape recursively, e.g. "(i32, (f64, String))", for error messages.
Trait Implementations§
Source§impl Clone for TypeRegistry
impl Clone for TypeRegistry
Source§fn clone(&self) -> TypeRegistry
fn clone(&self) -> TypeRegistry
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl TypeResolver for TypeRegistry
impl TypeResolver for TypeRegistry
Source§fn resolve_named_type(
&self,
name: &str,
args: &[ResolvedType],
) -> Option<ResolvedLeafType>
fn resolve_named_type( &self, name: &str, args: &[ResolvedType], ) -> Option<ResolvedLeafType>
Resolves a leaf without allocating a complete registry snapshot.
- Complexity: O(n) in generic argument nodes and name bytes.