Skip to main content

pdk_pel/
lib.rs

1// Copyright (c) 2026, Salesforce, Inc.,
2// All rights reserved.
3// For full license text, see the LICENSE.txt file
4
5//! Policy Expression Language (PEL)
6//!
7//! The Policy Expression Language (PEL) is an internal abstraction layer to evaluate DataWeave
8//! expressions.
9//!
10//! ## Overview
11//!
12//! PEL provides:
13//! - Expression parsing and evaluation
14//! - Runtime environment for expression execution
15//! - Context management for variable references
16//! - Type handling and coercion
17//!
18//! ## Features
19//!
20//! - `experimental_coerced_type`: USE AT OWN RISK: Enables experimental type coercion features. This feature enables reading objects as their original representation as strings.
21//!
22
23pub mod expression;
24
25pub mod parser;
26
27/// Runtime environment for executing PEL expressions.
28pub mod runtime;
29
30/// Represents a location in the source Dataweave expression, typically used for error reporting.
31///
32/// The `start` and `end` fields represent the character offsets in the source string.
33#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
34pub struct Location {
35    /// The starting character offset of the location (inclusive).
36    pub start: usize,
37    /// The ending character offset of the location (exclusive).
38    pub end: usize,
39}
40
41impl Location {
42    /// Creates a new `Location` spanning from `start` to `end`.
43    pub fn new(start: usize, end: usize) -> Self {
44        Self { start, end }
45    }
46}
47
48/// A unique identifier for a context in the PEL runtime.
49///
50/// Contexts are used to manage scopes and variable lookups during expression evaluation.
51/// Each context has a unique ID that can be used to reference it.
52#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
53pub struct ContextId(&'static str);
54
55impl ContextId {
56    /// Creates a new `ContextId` with the given identifier.
57    ///
58    /// The identifier should be unique within the scope of the application.
59    pub const fn new(raw: &'static str) -> Self {
60        Self(raw)
61    }
62
63    /// Creates a new `Reference` pointing to the first slot in this context.
64    pub const fn first_reference(&self) -> Reference {
65        Reference {
66            context_id: *self,
67            offset: 0,
68        }
69    }
70}
71
72/// A reference to a specific slot within a context.
73///
74/// References are used to track variable lookups and other context-dependent operations.
75#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
76pub struct Reference {
77    /// The context this reference belongs to.
78    context_id: ContextId,
79    /// The offset within the context.
80    offset: usize,
81}
82
83impl Reference {
84    /// Creates a new reference to the next slot in the same context.
85    pub const fn next(&self) -> Self {
86        Self {
87            context_id: self.context_id,
88            offset: self.offset + 1,
89        }
90    }
91
92    /// Returns the offset of this reference within its context.
93    pub const fn offset(&self) -> usize {
94        self.offset
95    }
96}