Skip to content

Commit

Permalink
rename Atom to Term
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Dec 12, 2020
1 parent 8bb5bfc commit 11a17e3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 70 deletions.
26 changes: 13 additions & 13 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn predicate(i: &str) -> IResult<&str, builder::Predicate> {
let (i, _) = space0(i)?;
let (i, ids) = delimited(
char('('),
separated_list1(preceded(space0, char(',')), atom),
separated_list1(preceded(space0, char(',')), term),
preceded(space0, char(')')),
)(i)?;

Expand Down Expand Up @@ -383,22 +383,22 @@ fn parse_string(i: &str) -> IResult<&str, String> {
delimited(char('"'), parse_string_internal, char('"'))(i)
}

fn string(i: &str) -> IResult<&str, builder::Atom> {
parse_string(i).map(|(i, s)| (i, builder::Atom::Str(s)))
fn string(i: &str) -> IResult<&str, builder::Term> {
parse_string(i).map(|(i, s)| (i, builder::Term::Str(s)))
}

fn parse_symbol(i: &str) -> IResult<&str, &str> {
preceded(char('#'), name)(i)
}

fn symbol(i: &str) -> IResult<&str, builder::Atom> {
fn symbol(i: &str) -> IResult<&str, builder::Term> {
parse_symbol(i).map(|(i, s)| (i, builder::s(s)))
}

fn parse_integer(i: &str) -> IResult<&str, i64> {
map_res(recognize(pair(opt(char('-')), digit1)), |s: &str| s.parse())(i)
}
fn integer(i: &str) -> IResult<&str, builder::Atom> {
fn integer(i: &str) -> IResult<&str, builder::Term> {
parse_integer(i).map(|(i, n)| (i, builder::int(n)))
}

Expand All @@ -415,8 +415,8 @@ fn parse_date(i: &str) -> IResult<&str, u64> {
)(i)
}

fn date(i: &str) -> IResult<&str, builder::Atom> {
parse_date(i).map(|(i, t)| (i, builder::Atom::Date(t)))
fn date(i: &str) -> IResult<&str, builder::Term> {
parse_date(i).map(|(i, t)| (i, builder::Term::Date(t)))
}

fn parse_bytes(i: &str) -> IResult<&str, Vec<u8>> {
Expand All @@ -434,18 +434,18 @@ fn parse_bytes(i: &str) -> IResult<&str, Vec<u8>> {
)(i)
}

fn bytes(i: &str) -> IResult<&str, builder::Atom> {
parse_bytes(i).map(|(i, s)| (i, builder::Atom::Bytes(s)))
fn bytes(i: &str) -> IResult<&str, builder::Term> {
parse_bytes(i).map(|(i, s)| (i, builder::Term::Bytes(s)))
}

fn variable(i: &str) -> IResult<&str, builder::Atom> {
fn variable(i: &str) -> IResult<&str, builder::Term> {
map(
preceded(char('$'), name),
builder::variable,
)(i)
}

fn atom(i: &str) -> IResult<&str, builder::Atom> {
fn term(i: &str) -> IResult<&str, builder::Term> {
preceded(space0, alt((symbol, string, date, variable, integer, bytes)))(i)
}

Expand Down Expand Up @@ -502,7 +502,7 @@ mod tests {
fn date() {
assert_eq!(
super::date("2019-12-02T13:49:53Z"),
Ok(("", builder::Atom::Date(1575294593)))
Ok(("", builder::Term::Date(1575294593)))
);
}

Expand Down Expand Up @@ -755,7 +755,7 @@ mod tests {
"date",
&[
builder::s("ambient"),
builder::Atom::Date(1575294593)
builder::Term::Date(1575294593)
]
)
))
Expand Down
110 changes: 55 additions & 55 deletions src/token/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl<'a> BiscuitBuilder<'a> {
}

#[derive(Debug, Clone, PartialEq, Hash, Eq)]
pub enum Atom {
pub enum Term {
Symbol(String),
Variable(String),
Integer(i64),
Expand All @@ -251,61 +251,61 @@ pub enum Atom {
Bytes(Vec<u8>),
}

impl Atom {
impl Term {
pub fn convert(&self, symbols: &mut SymbolTable) -> ID {
match self {
Atom::Symbol(s) => ID::Symbol(symbols.insert(s)),
Atom::Variable(s) => ID::Variable(symbols.insert(s) as u32),
Atom::Integer(i) => ID::Integer(*i),
Atom::Str(s) => ID::Str(s.clone()),
Atom::Date(d) => ID::Date(*d),
Atom::Bytes(s) => ID::Bytes(s.clone()),
Term::Symbol(s) => ID::Symbol(symbols.insert(s)),
Term::Variable(s) => ID::Variable(symbols.insert(s) as u32),
Term::Integer(i) => ID::Integer(*i),
Term::Str(s) => ID::Str(s.clone()),
Term::Date(d) => ID::Date(*d),
Term::Bytes(s) => ID::Bytes(s.clone()),
}
}

pub fn convert_from(f: &datalog::ID, symbols: &SymbolTable) -> Self {
match f {
ID::Symbol(s) => Atom::Symbol(symbols.print_symbol(*s)),
ID::Variable(s) => Atom::Variable(symbols.print_symbol(*s as u64)),
ID::Integer(i) => Atom::Integer(*i),
ID::Str(s) => Atom::Str(s.clone()),
ID::Date(d) => Atom::Date(*d),
ID::Bytes(s) => Atom::Bytes(s.clone()),
ID::Symbol(s) => Term::Symbol(symbols.print_symbol(*s)),
ID::Variable(s) => Term::Variable(symbols.print_symbol(*s as u64)),
ID::Integer(i) => Term::Integer(*i),
ID::Str(s) => Term::Str(s.clone()),
ID::Date(d) => Term::Date(*d),
ID::Bytes(s) => Term::Bytes(s.clone()),
}
}
}

impl From<&Atom> for Atom {
fn from(i: &Atom) -> Self {
impl From<&Term> for Term {
fn from(i: &Term) -> Self {
match i {
Atom::Symbol(ref s) => Atom::Symbol(s.clone()),
Atom::Variable(ref v) => Atom::Variable(v.clone()),
Atom::Integer(ref i) => Atom::Integer(*i),
Atom::Str(ref s) => Atom::Str(s.clone()),
Atom::Date(ref d) => Atom::Date(*d),
Atom::Bytes(ref s) => Atom::Bytes(s.clone()),
Term::Symbol(ref s) => Term::Symbol(s.clone()),
Term::Variable(ref v) => Term::Variable(v.clone()),
Term::Integer(ref i) => Term::Integer(*i),
Term::Str(ref s) => Term::Str(s.clone()),
Term::Date(ref d) => Term::Date(*d),
Term::Bytes(ref s) => Term::Bytes(s.clone()),
}
}
}

impl AsRef<Atom> for Atom {
fn as_ref(&self) -> &Atom {
impl AsRef<Term> for Term {
fn as_ref(&self) -> &Term {
self
}
}

impl fmt::Display for Atom {
impl fmt::Display for Term {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Atom::Variable(i) => write!(f, "${}", i),
Atom::Integer(i) => write!(f, "{}", i),
Atom::Str(s) => write!(f, "\"{}\"", s),
Atom::Symbol(s) => write!(f, "#{}", s),
Atom::Date(d) => {
Term::Variable(i) => write!(f, "${}", i),
Term::Integer(i) => write!(f, "{}", i),
Term::Str(s) => write!(f, "\"{}\"", s),
Term::Symbol(s) => write!(f, "#{}", s),
Term::Date(d) => {
let t = UNIX_EPOCH + Duration::from_secs(*d);
write!(f, "{:?}", t)
}
Atom::Bytes(s) => write!(f, "hex:{}", hex::encode(s)),
Term::Bytes(s) => write!(f, "hex:{}", hex::encode(s)),
}

}
Expand All @@ -314,7 +314,7 @@ impl fmt::Display for Atom {
#[derive(Debug, Clone, PartialEq, Hash, Eq)]
pub struct Predicate {
pub name: String,
pub ids: Vec<Atom>,
pub ids: Vec<Term>,
}

impl Predicate {
Expand All @@ -332,11 +332,11 @@ impl Predicate {
pub fn convert_from(p: &datalog::Predicate, symbols: &SymbolTable) -> Self {
Predicate {
name: symbols.print_symbol(p.name),
ids: p.ids.iter().map(|id| Atom::convert_from(&id, symbols)).collect(),
ids: p.ids.iter().map(|id| Term::convert_from(&id, symbols)).collect(),
}
}

pub fn new(name: String, ids: &[Atom]) -> Predicate {
pub fn new(name: String, ids: &[Term]) -> Predicate {
Predicate {
name,
ids: ids.to_vec(),
Expand Down Expand Up @@ -372,7 +372,7 @@ impl fmt::Display for Predicate {
pub struct Fact(pub Predicate);

impl Fact {
pub fn new(name: String, ids: &[Atom]) -> Fact {
pub fn new(name: String, ids: &[Term]) -> Fact {
Fact(Predicate::new(name, ids))
}
}
Expand Down Expand Up @@ -655,20 +655,20 @@ impl fmt::Display for Caveat {
}

/// creates a new fact
pub fn fact<I: AsRef<Atom>>(name: &str, ids: &[I]) -> Fact {
pub fn fact<I: AsRef<Term>>(name: &str, ids: &[I]) -> Fact {
Fact(pred(name, ids))
}

/// creates a predicate
pub fn pred<I: AsRef<Atom>>(name: &str, ids: &[I]) -> Predicate {
pub fn pred<I: AsRef<Term>>(name: &str, ids: &[I]) -> Predicate {
Predicate {
name: name.to_string(),
ids: ids.iter().map(|id| id.as_ref().clone()).collect(),
}
}

/// creates a rule
pub fn rule<I: AsRef<Atom>, P: AsRef<Predicate>>(
pub fn rule<I: AsRef<Term>, P: AsRef<Predicate>>(
head_name: &str,
head_ids: &[I],
predicates: &[P],
Expand All @@ -681,7 +681,7 @@ pub fn rule<I: AsRef<Atom>, P: AsRef<Predicate>>(
}

/// creates a rule with constraints
pub fn constrained_rule<I: AsRef<Atom>, P: AsRef<Predicate>, C: AsRef<Constraint>>(
pub fn constrained_rule<I: AsRef<Term>, P: AsRef<Predicate>, C: AsRef<Constraint>>(
head_name: &str,
head_ids: &[I],
predicates: &[P],
Expand All @@ -695,48 +695,48 @@ pub fn constrained_rule<I: AsRef<Atom>, P: AsRef<Predicate>, C: AsRef<Constraint
}

/// creates an integer value
pub fn int(i: i64) -> Atom {
Atom::Integer(i)
pub fn int(i: i64) -> Term {
Term::Integer(i)
}

/// creates a string
pub fn string(s: &str) -> Atom {
Atom::Str(s.to_string())
pub fn string(s: &str) -> Term {
Term::Str(s.to_string())
}

/// creates a symbol
///
/// once the block is generated, this symbol will be added to the symbol table if needed
pub fn s(s: &str) -> Atom {
Atom::Symbol(s.to_string())
pub fn s(s: &str) -> Term {
Term::Symbol(s.to_string())
}

/// creates a symbol
///
/// once the block is generated, this symbol will be added to the symbol table if needed
pub fn symbol(s: &str) -> Atom {
Atom::Symbol(s.to_string())
pub fn symbol(s: &str) -> Term {
Term::Symbol(s.to_string())
}

/// creates a date
///
/// internally the date will be stored as seconds since UNIX_EPOCH
pub fn date(t: &SystemTime) -> Atom {
pub fn date(t: &SystemTime) -> Term {
let dur = t.duration_since(UNIX_EPOCH).unwrap();
Atom::Date(dur.as_secs())
Term::Date(dur.as_secs())
}

/// creates a variable for a rule
pub fn var(s: &str) -> Atom {
Atom::Variable(s.to_string())
pub fn var(s: &str) -> Term {
Term::Variable(s.to_string())
}

/// creates a variable for a rule
pub fn variable(s: &str) -> Atom {
Atom::Variable(s.to_string())
pub fn variable(s: &str) -> Term {
Term::Variable(s.to_string())
}

/// creates a byte array
pub fn bytes(s: &[u8]) -> Atom {
Atom::Bytes(s.to_vec())
pub fn bytes(s: &[u8]) -> Term {
Term::Bytes(s.to_vec())
}
4 changes: 2 additions & 2 deletions src/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,8 +1172,8 @@ mod tests {

let res2 = verifier.query(rule(
"revocation_id_verif",
&[builder::Atom::Variable("id".to_string())],
&[pred("revocation_id", &[builder::Atom::Variable("id".to_string())])]
&[builder::Term::Variable("id".to_string())],
&[pred("revocation_id", &[builder::Term::Variable("id".to_string())])]
));
println!("res2: {:?}", res2);
assert_eq!(
Expand Down

0 comments on commit 11a17e3

Please sign in to comment.