use super::CompiledNode;
use super::compile_ctx::NodeId;
#[cfg(feature = "error-handling")]
use datavalue::OwnedDataValue;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) enum PathSegment {
Field(Box<str>),
FieldOrIndex(Box<str>, usize),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum ReduceHint {
None,
Current,
Accumulator,
CurrentPath,
AccumulatorPath,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum MetadataHint {
None,
Index,
Key,
}
impl MetadataHint {
#[inline]
pub(crate) fn from_path(path: &str) -> Self {
match path {
"index" => MetadataHint::Index,
"key" => MetadataHint::Key,
_ => MetadataHint::None,
}
}
}
#[derive(Debug, Clone)]
pub(crate) struct CustomOperatorData {
pub id: NodeId,
pub name: String,
pub args: Box<[CompiledNode]>,
}
#[derive(Debug, Clone)]
pub(crate) struct CseData {
pub slot: u16,
pub inner: CompiledNode,
}
#[cfg(feature = "templating")]
#[derive(Debug, Clone)]
pub(crate) struct StructuredObjectData {
pub id: NodeId,
pub fields: Box<[(String, CompiledNode)]>,
pub has_escaped_keys: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub(crate) enum ScopeBinding {
#[default]
Unresolved,
Root,
Current,
Ancestor,
}
impl ScopeBinding {
pub(crate) fn resolve(static_depth: u32, scope_level: u32) -> Self {
Self::from_target(crate::arena::frame_target(
static_depth as usize,
scope_level as usize,
))
}
#[inline]
pub(crate) fn from_target(target: crate::arena::FrameTarget) -> Self {
use crate::arena::FrameTarget;
match target {
FrameTarget::Root => ScopeBinding::Root,
FrameTarget::Top => ScopeBinding::Current,
FrameTarget::Ancestor(_) => ScopeBinding::Ancestor,
}
}
}
pub(crate) fn metadata_reads_ancestor(static_depth: u32, scope_level: u32) -> bool {
use crate::arena::{frame_at_climb, metadata_climb};
metadata_climb(scope_level as usize)
.map(|c| ScopeBinding::from_target(frame_at_climb(static_depth as usize, c)))
== Some(ScopeBinding::Ancestor)
}
#[cfg(feature = "ext-control")]
#[derive(Debug, Clone)]
pub(crate) struct CompiledExistsData {
pub id: NodeId,
pub scope_level: u32,
pub segments: Box<[PathSegment]>,
pub binding: ScopeBinding,
}
#[derive(Debug, Clone)]
pub(crate) enum Resolved<S, D> {
Now(S),
Later(D),
}
pub(crate) type StaticMissingPath = (Box<str>, Box<[PathSegment]>);
pub(crate) type CompiledMissingArg = Resolved<StaticMissingPath, CompiledNode>;
pub(crate) type CompiledMissingMin = Resolved<usize, CompiledNode>;
pub(crate) type CompiledMissingPaths = Resolved<Box<[StaticMissingPath]>, CompiledNode>;
#[derive(Debug, Clone)]
pub(crate) struct CompiledMissingData {
pub id: NodeId,
pub args: Box<[CompiledMissingArg]>,
}
#[derive(Debug, Clone)]
pub(crate) struct CompiledMissingSomeData {
pub id: NodeId,
pub min_present: CompiledMissingMin,
pub paths: CompiledMissingPaths,
}
#[cfg(feature = "error-handling")]
#[derive(Debug, Clone)]
pub(crate) struct CompiledThrowData {
pub id: NodeId,
pub error: OwnedDataValue,
}