Skip to content

Commit 041fa03

Browse files
committed
Extract guard-lang so guard can be used as a library
1 parent 11b2880 commit 041fa03

54 files changed

Lines changed: 1043 additions & 937 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 31 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
resolver = "2"
33
members = [
44
"guard",
5+
"guard-lang",
56
"guard-lambda",
67
"guard-ffi",
78
"guard-examples/library"

guard-lang/Cargo.toml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[package]
2+
name = "guard-lang"
3+
version = "0.1.0"
4+
edition = "2018"
5+
description = "Parser, AST, and evaluation types for the AWS CloudFormation Guard DSL"
6+
license = "Apache-2.0"
7+
8+
[dependencies]
9+
nom = "7.0.0"
10+
nom_locate = "4.0.0"
11+
indexmap = { version = "1.6.0", features = ["serde-1"] }
12+
serde = { version = "1.0", features = ["derive", "rc"] }
13+
serde_json = { version = "1.0.85", features = ["preserve_order"] }
14+
serde_yaml = "0.9.10"
15+
fancy-regex = "0.13.0"
16+
thiserror = "1.0.38"
17+
lazy_static = "1.4.0"
18+
colored = "2.2.0"
19+
unsafe-libyaml = "0.2.10"
20+
itertools = "0.4.7"
21+
chrono = "0.4.38"
22+
cruet = "0.14.0"
23+
urlencoding = "2.1.0"
24+
wasm-bindgen = "0.2.92"
25+
quick-xml = "0.30.0"
26+
27+
[dev-dependencies]
28+
pretty_assertions = "1.4.0"
29+
rstest = "0.24.0"
30+
indoc = "1.0.8"
31+
grep-searcher = "0.1.8"
32+
grep-matcher = "0.1.5"
33+
grep-regex = "0.1.9"

guard-lang/assets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../guard/assets
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use crate::rules::eval_context::EventRecord;
2-
use crate::rules::exprs::SliceDisplay;
3-
use crate::rules::path_value::PathAwareValue;
4-
use crate::rules::values::{CmpOperator, RangeType, LOWER_INCLUSIVE, UPPER_INCLUSIVE};
5-
use crate::rules::{BlockCheck, ClauseCheck, QueryResult, RecordType, Status};
1+
use crate::eval_context::EventRecord;
2+
use crate::exprs::SliceDisplay;
3+
use crate::path_value::PathAwareValue;
4+
use crate::values::{CmpOperator, RangeType, LOWER_INCLUSIVE, UPPER_INCLUSIVE};
5+
use crate::{BlockCheck, ClauseCheck, QueryResult, RecordType, Status};
66
use std::fmt::{Display, Formatter};
77
use std::rc::Rc;
88

9-
pub(crate) fn display_comparison((cmp, not): (CmpOperator, bool)) -> String {
9+
pub fn display_comparison((cmp, not): (CmpOperator, bool)) -> String {
1010
format!("{} {}", if not { "not" } else { "" }, cmp)
1111
}
1212

@@ -30,7 +30,7 @@ fn write_range<T: Display + PartialOrd>(
3030
Ok(())
3131
}
3232

33-
pub(crate) struct ValueOnlyDisplay(pub(crate) Rc<PathAwareValue>);
33+
pub struct ValueOnlyDisplay(pub Rc<PathAwareValue>);
3434

3535
impl std::fmt::Debug for ValueOnlyDisplay {
3636
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::string::FromUtf8Error;
44
use thiserror::Error;
55
use wasm_bindgen::JsValue;
66

7-
use crate::rules::parser::{ParserError, Span};
7+
use crate::parser::{ParserError, Span};
88

99
#[derive(Debug, Error)]
1010
#[allow(clippy::enum_variant_names)]
Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use super::exprs::*;
22
use super::*;
3-
use crate::rules::eval::operators::Comparator;
4-
use crate::rules::eval_context::{block_scope, resolve_function, ValueScope};
5-
use crate::rules::path_value::compare_eq;
3+
use crate::eval::operators::Comparator;
4+
use crate::eval_context::{block_scope, resolve_function, ValueScope};
5+
use crate::path_value::compare_eq;
66
use std::collections::HashMap;
77

88
mod operators;
@@ -1010,31 +1010,31 @@ pub(super) fn real_binary_operation<'value, 'loc: 'value>(
10101010
QueryResult::Literal(l) | QueryResult::Resolved(l) => {
10111011
let r = match cmp {
10121012
(CmpOperator::Eq, is_not) => each_lhs_compare(
1013-
not_compare(crate::rules::path_value::compare_eq, is_not),
1013+
not_compare(crate::path_value::compare_eq, is_not),
10141014
Rc::clone(l),
10151015
rhs,
10161016
)?,
10171017

10181018
(CmpOperator::Ge, is_not) => each_lhs_compare(
1019-
not_compare(crate::rules::path_value::compare_ge, is_not),
1019+
not_compare(crate::path_value::compare_ge, is_not),
10201020
Rc::clone(l),
10211021
rhs,
10221022
)?,
10231023

10241024
(CmpOperator::Gt, is_not) => each_lhs_compare(
1025-
not_compare(crate::rules::path_value::compare_gt, is_not),
1025+
not_compare(crate::path_value::compare_gt, is_not),
10261026
Rc::clone(l),
10271027
rhs,
10281028
)?,
10291029

10301030
(CmpOperator::Lt, is_not) => each_lhs_compare(
1031-
not_compare(crate::rules::path_value::compare_lt, is_not),
1031+
not_compare(crate::path_value::compare_lt, is_not),
10321032
Rc::clone(l),
10331033
rhs,
10341034
)?,
10351035

10361036
(CmpOperator::Le, is_not) => each_lhs_compare(
1037-
not_compare(crate::rules::path_value::compare_le, is_not),
1037+
not_compare(crate::path_value::compare_le, is_not),
10381038
Rc::clone(l),
10391039
rhs,
10401040
)?,
@@ -1075,7 +1075,7 @@ pub(super) fn real_binary_operation<'value, 'loc: 'value>(
10751075
}
10761076

10771077
#[allow(clippy::never_loop)]
1078-
pub(in crate::rules) fn eval_guard_access_clause<'value, 'loc: 'value>(
1078+
pub fn eval_guard_access_clause<'value, 'loc: 'value>(
10791079
gac: &'value GuardAccessClause<'loc>,
10801080
resolver: &mut dyn EvalContext<'value, 'loc>,
10811081
) -> Result<Status> {
@@ -1224,7 +1224,7 @@ pub(in crate::rules) fn eval_guard_access_clause<'value, 'loc: 'value>(
12241224
}
12251225
}
12261226

1227-
pub(in crate::rules) fn eval_guard_named_clause<'value, 'loc: 'value>(
1227+
pub fn eval_guard_named_clause<'value, 'loc: 'value>(
12281228
gnc: &'value GuardNamedRuleClause<'loc>,
12291229
resolver: &mut dyn EvalContext<'value, 'loc>,
12301230
) -> Result<Status> {
@@ -1288,7 +1288,7 @@ pub(in crate::rules) fn eval_guard_named_clause<'value, 'loc: 'value>(
12881288
}
12891289
}
12901290

1291-
pub(in crate::rules) fn eval_general_block_clause<'value, 'loc: 'value, T, E>(
1291+
pub fn eval_general_block_clause<'value, 'loc: 'value, T, E>(
12921292
block: &'value Block<'loc, T>,
12931293
resolver: &mut dyn EvalContext<'value, 'loc>,
12941294
eval_fn: E,
@@ -1300,7 +1300,7 @@ where
13001300
eval_conjunction_clauses(&block.conjunctions, &mut block_scope, eval_fn)
13011301
}
13021302

1303-
pub(in crate::rules) fn eval_guard_block_clause<'value, 'loc: 'value>(
1303+
pub fn eval_guard_block_clause<'value, 'loc: 'value>(
13041304
block_clause: &'value BlockGuardClause<'loc>,
13051305
resolver: &mut dyn EvalContext<'value, 'loc>,
13061306
) -> Result<Status> {
@@ -1571,7 +1571,7 @@ impl<'eval, 'value, 'loc: 'value> RecordTracer<'value>
15711571
}
15721572
}
15731573

1574-
pub(in crate::rules) fn eval_parameterized_rule_call<'value, 'loc: 'value>(
1574+
pub fn eval_parameterized_rule_call<'value, 'loc: 'value>(
15751575
call_rule: &'value ParameterizedNamedRuleClause<'loc>,
15761576
resolver: &mut dyn EvalContext<'value, 'loc>,
15771577
) -> Result<Status> {
@@ -1617,7 +1617,7 @@ pub(in crate::rules) fn eval_parameterized_rule_call<'value, 'loc: 'value>(
16171617
eval_rule(&param_rule.rule, &mut eval)
16181618
}
16191619

1620-
pub(in crate::rules) fn eval_guard_clause<'value, 'loc: 'value>(
1620+
pub fn eval_guard_clause<'value, 'loc: 'value>(
16211621
gc: &'value GuardClause<'loc>,
16221622
resolver: &mut dyn EvalContext<'value, 'loc>,
16231623
) -> Result<Status> {
@@ -1635,7 +1635,7 @@ pub(in crate::rules) fn eval_guard_clause<'value, 'loc: 'value>(
16351635
}
16361636
}
16371637

1638-
pub(in crate::rules) fn eval_when_clause<'value, 'loc: 'value>(
1638+
pub fn eval_when_clause<'value, 'loc: 'value>(
16391639
when_clause: &'value WhenGuardClause<'loc>,
16401640
resolver: &mut dyn EvalContext<'value, 'loc>,
16411641
) -> Result<Status> {
@@ -1646,7 +1646,7 @@ pub(in crate::rules) fn eval_when_clause<'value, 'loc: 'value>(
16461646
}
16471647
}
16481648

1649-
pub(in crate::rules) fn eval_type_block_clause<'value, 'loc: 'value>(
1649+
pub fn eval_type_block_clause<'value, 'loc: 'value>(
16501650
type_block: &'value TypeBlock<'loc>,
16511651
resolver: &mut dyn EvalContext<'value, 'loc>,
16521652
) -> Result<Status> {
@@ -1821,7 +1821,7 @@ pub(in crate::rules) fn eval_type_block_clause<'value, 'loc: 'value>(
18211821
Ok(status)
18221822
}
18231823

1824-
pub(in crate::rules) fn eval_rule_clause<'value, 'loc: 'value>(
1824+
pub fn eval_rule_clause<'value, 'loc: 'value>(
18251825
rule_clause: &'value RuleClause<'loc>,
18261826
resolver: &mut dyn EvalContext<'value, 'loc>,
18271827
) -> Result<Status> {
@@ -1834,7 +1834,7 @@ pub(in crate::rules) fn eval_rule_clause<'value, 'loc: 'value>(
18341834
}
18351835
}
18361836

1837-
pub(in crate::rules) fn eval_rule<'value, 'loc: 'value>(
1837+
pub fn eval_rule<'value, 'loc: 'value>(
18381838
rule: &'value Rule<'loc>,
18391839
resolver: &mut dyn EvalContext<'value, 'loc>,
18401840
) -> Result<Status> {
@@ -1912,7 +1912,7 @@ impl<'loc> std::fmt::Display for RulesFile<'loc> {
19121912
}
19131913
}
19141914

1915-
pub(crate) fn eval_rules_file<'value, 'loc: 'value>(
1915+
pub fn eval_rules_file<'value, 'loc: 'value>(
19161916
rule: &'value RulesFile<'loc>,
19171917
resolver: &mut dyn EvalContext<'value, 'loc>,
19181918
data_file_name: Option<&'value str>,
@@ -1968,7 +1968,7 @@ pub(crate) fn eval_rules_file<'value, 'loc: 'value>(
19681968
}
19691969

19701970
#[allow(clippy::never_loop)]
1971-
pub(in crate::rules) fn eval_conjunction_clauses<'value, 'loc: 'value, T, E>(
1971+
pub fn eval_conjunction_clauses<'value, 'loc: 'value, T, E>(
19721972
conjunctions: &'value Conjunctions<T>,
19731973
resolver: &mut dyn EvalContext<'value, 'loc>,
19741974
eval_fn: E,
@@ -1979,7 +1979,15 @@ where
19791979
Ok(loop {
19801980
let mut num_passes = 0;
19811981
let mut num_fails = 0;
1982-
let context = format!("{}#disjunction", std::any::type_name::<T>());
1982+
let full_name = std::any::type_name::<T>();
1983+
let short_name = full_name
1984+
.rsplit("::")
1985+
.next()
1986+
.unwrap_or(full_name)
1987+
.split('<')
1988+
.next()
1989+
.unwrap_or(full_name);
1990+
let context = format!("{}#disjunction", short_name);
19831991
'conjunction: for conjunction in conjunctions {
19841992
let mut num_of_disjunction_fails = 0;
19851993
let multiple_ors_present = conjunction.len() > 1;

0 commit comments

Comments
 (0)