Skip to content

Commit

Permalink
merge predicates in one file
Browse files Browse the repository at this point in the history
  • Loading branch information
grepsuzette committed Apr 10, 2023
1 parent 70692c4 commit 6ce733f
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions examples/gno.land/r/demo/mail/predicates.gno
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
package mail

import (
"std"
"strings"
"time"
)

type Predicate interface {
Satisfy(mail *Mail) bool
}

type (
SenderIs struct{ sender std.Address }
RecipientIs struct{ recipient std.Address }
AndPred struct{ a []Predicate }
Contains struct{ s string }
ContainsEither struct{ a []string }
TopicContains struct{ s string }
Not struct{ pred Predicate }
TimeBefore struct{ t time.Time } // <=
TimeAfter struct{ t time.Time } // >=
TimeBetween struct {
min time.Time
max time.Time
}
)

// `And(Contains("virus"), TimeBefore(covid))`
func And(preds ...Predicate) Predicate { return AndPred{preds} }

func (o AndPred) Satisfy(mail *Mail) bool { return mail.Satisfies(o.a) }
func (x SenderIs) Satisfy(mail *Mail) bool { return mail.sender == x.sender }
func (x RecipientIs) Satisfy(mail *Mail) bool { return mail.recipient == x.recipient }

// searches are always case-insensitive, and within Topic+Body
// You may combine if neeeded: Not{Contains} or And{Not{Contains}, Not{Contains}}
func (o Contains) Satisfy(mail *Mail) bool { return mail.Contains(o.s) }
func (o Not) Satisfy(mail *Mail) bool { return !mail.Satisfies([]Predicate{o.pred}) }

func (o TopicContains) Satisfy(mail *Mail) bool {
topic := strings.ToLower(mail.topic)
return strings.Contains(topic, strings.ToLower(o.s))
}

func (o ContainsEither) Satisfy(mail *Mail) bool {
for _, substring := range o.a {
if mail.Contains(substring) {
return true
}
}
return false
}

func (o TimeBefore) Satisfy(mail *Mail) bool { return !mail.time.After(o.t) }
func (o TimeAfter) Satisfy(mail *Mail) bool { return !mail.time.Before(o.t) }
func (o TimeBetween) Satisfy(mail *Mail) bool {
return !mail.time.Before(o.min) && !mail.time.After(o.max)
}

0 comments on commit 6ce733f

Please sign in to comment.