ucan-wg / delegation

1 stars 1 forks source link

finalize syntax & set of supported operators #9

Closed Gozala closed 4 months ago

Gozala commented 8 months ago

Forking discussion around which operators do we want to support and how the overall syntax looks like here also

Gozala commented 8 months ago

Here is the lastest Rust schema from @expede

pub enum Term {
    Literal(Json), // FIXME only JSON? Because we need a way to inspect CIDs + Bytes
    Variable(Variable), // ?x

    Args, // Special `$` variable
    Select(Vec<Index>), // e.g. `.foo..bar.[]`

    Not(Box<Term>),
    And(Vec<Term>),
    Or(Vec<Term>),

    Equal(Value, Value),
    GreaterThan(Value, Value),
    GreaterOrEqual(Value, Value),
    LessThan(Value, Value),
    LessOrEqual(Value, Value),

    Match(Variable, String),

    // Future
    Every(Variable, Variable), // forall x in domain
}

pub struct Variable(String);

pub enum Value {
  Literal(Json),
  Variable(Variable)
}

pub enum Index {
    RecDesend,    // ..
    FlattenAll,   // .[]
    Index(usize), // .[2]
    Key(String),  // .key
}

pub enum Json {
    Null,
    Bool(bool),
    Float(f64),
    Integer(i128), // Only up to 2^53 (IEEE 754), not libipld's i128? 🤔
    String(String),
    Array(Vec<Json>),
    Object(BTreeMap<String, Json>),
}
expede commented 8 months ago

Updated from other discussions today:

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Term {
    // Leaves
    Args, // $
    Literal(Ipld),
    Variable(Variable),

    Selector(Selector),

    // Connectives
    Not(Box<Term>),
    And(Vec<Term>),
    Or(Vec<Term>),

    // Comparison
    Equal(Value, Value),
    GreaterThan(Value, Value),
    GreaterOrEqual(Value, Value),
    LessThan(Value, Value),
    LessOrEqual(Value, Value),

    // String Matcher
    Glob(Value, String),

    // Existential Quantification
    Exists(Variable, Collection), // ∃x ∈ xs
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Variable(String); // ?x

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Collection {
    Array(Vec<Ipld>),
    Map(BTreeMap<String, Ipld>),
    Variable(Variable),
    Selector(Selector),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Selector(Vec<Index>); // .foo.bar[].baz

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Index {
    RecDesend,    // ..
    FlattenAll,   // .[]
    Index(usize), // .[2]
    Key(String),  // .key
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Value {
    Literal(Ipld),
    Variable(Variable),
    ImplicitBind(Selector),
}

I'll probably write a version with sugar (e.g. implicit bind) and a lower one that it desugars to, but this is the high level interface for now