stylex_structures/
stylex_env.rs1use std::rc::Rc;
2
3use swc_core::ecma::ast::Expr;
4
5#[derive(Clone, Debug)]
10pub enum EnvEntry {
11 Expr(Expr),
12 Function(JSFunction),
13}
14
15impl EnvEntry {
16 pub fn as_expr(&self) -> Option<&Expr> {
17 match self {
18 EnvEntry::Expr(e) => Some(e),
19 _ => None,
20 }
21 }
22
23 pub fn as_function(&self) -> Option<&JSFunction> {
24 match self {
25 EnvEntry::Function(f) => Some(f),
26 _ => None,
27 }
28 }
29
30 pub fn is_function(&self) -> bool {
31 matches!(self, EnvEntry::Function(_))
32 }
33}
34
35#[derive(Clone)]
38pub struct JSFunction {
39 inner: Rc<dyn Fn(Vec<Expr>) -> Expr>,
40}
41
42impl std::fmt::Debug for JSFunction {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 write!(f, "JSFunction(<closure>)")
45 }
46}
47
48impl JSFunction {
49 pub fn new(inner: impl Fn(Vec<Expr>) -> Expr + 'static) -> Self {
50 Self {
51 inner: Rc::new(inner),
52 }
53 }
54
55 pub fn call(&self, args: Vec<Expr>) -> Expr {
56 (self.inner)(args)
57 }
58}