Skip to content

Commit 3ac8ad6

Browse files
authored
Merge branch 'main' into unknowns
2 parents a63ed5b + 59adc6d commit 3ac8ad6

Some content is hidden

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

46 files changed

+1496
-1048
lines changed

Cargo.lock

Lines changed: 62 additions & 83 deletions
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
@@ -43,6 +43,7 @@ rust-2018-idioms = "deny"
4343
# means we'll see it in local runs.
4444
[workspace.lints.clippy]
4545
nursery = { level = "warn", priority = -1 }
46+
needless_pass_by_value = "warn"
4647

4748
# These lints may be worth enforcing, but cause a lot of noise at the moment.
4849
use_self = "allow"

cedar-policy-cli/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ protobufs = ["dep:prost", "dep:prost-build", "cedar-policy/protobufs", "cedar-po
3737
[dev-dependencies]
3838
assert_cmd = "2.0"
3939
tempfile = "3"
40-
glob = "0.3.1"
41-
predicates = "3.1.0"
40+
glob = "0.3.2"
41+
predicates = "3.1.3"
4242
rstest = "0.23.0"
4343

4444
# We override the name of the binary for src/main.rs, which otherwise would be

cedar-policy-cli/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ fn translate_policy_inner(args: &TranslatePolicyArgs) -> Result<String> {
947947
let translate = match args.direction {
948948
PolicyTranslationDirection::CedarToJson => translate_policy_to_json,
949949
};
950-
read_from_file_or_stdin(args.input_file.clone(), "policy").and_then(translate)
950+
read_from_file_or_stdin(args.input_file.as_ref(), "policy").and_then(translate)
951951
}
952952

953953
pub fn translate_policy(args: &TranslatePolicyArgs) -> CedarExitCode {
@@ -984,7 +984,7 @@ fn translate_schema_inner(args: &TranslateSchemaArgs) -> Result<String> {
984984
SchemaTranslationDirection::JsonToCedar => translate_schema_to_cedar,
985985
SchemaTranslationDirection::CedarToJson => translate_schema_to_json,
986986
};
987-
read_from_file_or_stdin(args.input_file.clone(), "schema").and_then(translate)
987+
read_from_file_or_stdin(args.input_file.as_ref(), "schema").and_then(translate)
988988
}
989989

990990
pub fn translate_schema(args: &TranslateSchemaArgs) -> CedarExitCode {
@@ -1377,7 +1377,7 @@ fn load_entities(entities_filename: impl AsRef<Path>, schema: Option<&Schema>) -
13771377
/// This will rename template-linked policies to the id of their template, which may
13781378
/// cause id conflicts, so only call this function before instancing
13791379
/// templates into the policy set.
1380-
fn rename_from_id_annotation(ps: PolicySet) -> Result<PolicySet> {
1380+
fn rename_from_id_annotation(ps: &PolicySet) -> Result<PolicySet> {
13811381
let mut new_ps = PolicySet::new();
13821382
let t_iter = ps.templates().map(|t| match t.annotation("id") {
13831383
None => Ok(t.clone()),
@@ -1443,7 +1443,7 @@ fn read_cedar_policy_set(
14431443
Report::new(err).with_source_code(NamedSource::new(name, ps_str))
14441444
})
14451445
.wrap_err_with(|| format!("failed to parse {context}"))?;
1446-
rename_from_id_annotation(ps)
1446+
rename_from_id_annotation(&ps)
14471447
}
14481448

14491449
/// Read a policy set, static policy or policy template, in Cedar JSON (EST) syntax, from the file given

cedar-policy-cli/tests/sample.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn run_link_test(
106106
links_file: impl Into<String>,
107107
template_id: impl Into<String>,
108108
linked_id: impl Into<String>,
109-
env: HashMap<SlotId, String>,
109+
env: impl IntoIterator<Item = (SlotId, String)>,
110110
expected: CedarExitCode,
111111
) {
112112
let cmd = LinkArgs {
@@ -117,7 +117,9 @@ fn run_link_test(
117117
},
118118
template_id: template_id.into(),
119119
new_id: linked_id.into(),
120-
arguments: Arguments { data: env },
120+
arguments: Arguments {
121+
data: HashMap::from_iter(env),
122+
},
121123
};
122124
let output = link(&cmd);
123125
assert_eq!(output, expected);
@@ -792,9 +794,7 @@ fn test_link_samples() {
792794
&linked_file_name,
793795
"AccessVacation",
794796
"AliceAccess",
795-
[(SlotId::principal(), "User::\"alice\"".to_string())]
796-
.into_iter()
797-
.collect(),
797+
[(SlotId::principal(), "User::\"alice\"".to_string())],
798798
CedarExitCode::Failure,
799799
);
800800

@@ -803,9 +803,7 @@ fn test_link_samples() {
803803
&linked_file_name,
804804
"AccessVacation",
805805
"AliceAccess",
806-
[(SlotId::principal(), "invalid".to_string())]
807-
.into_iter()
808-
.collect(),
806+
[(SlotId::principal(), "invalid".to_string())],
809807
CedarExitCode::Failure,
810808
);
811809

@@ -814,9 +812,7 @@ fn test_link_samples() {
814812
&linked_file_name,
815813
"AccessVacation",
816814
"AliceAccess",
817-
[(SlotId::principal(), "User::\"alice\"".to_string())]
818-
.into_iter()
819-
.collect(),
815+
[(SlotId::principal(), "User::\"alice\"".to_string())],
820816
CedarExitCode::Success,
821817
);
822818

@@ -845,9 +841,7 @@ fn test_link_samples() {
845841
&linked_file_name,
846842
"AccessVacation",
847843
"BobAccess",
848-
[(SlotId::principal(), "User::\"bob\"".to_string())]
849-
.into_iter()
850-
.collect(),
844+
[(SlotId::principal(), "User::\"bob\"".to_string())],
851845
CedarExitCode::Success,
852846
);
853847

cedar-policy-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ repository.workspace = true
1414

1515
[dependencies]
1616
serde = { version = "1.0", features = ["derive", "rc"] }
17-
serde_with = { version = "3.0", features = ["json"] }
17+
serde_with = { version = "3.12", features = ["json"] }
1818
serde_json = "1.0"
1919
lalrpop-util = { version = "0.22.0", features = ["lexer"] }
2020
lazy_static = "1.4"

cedar-policy-core/src/ast/extension.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,22 @@ pub enum CallStyle {
124124

125125
// Note: we could use currying to make this a little nicer
126126

127-
/// Trait object that implements the extension function call.
128-
pub type ExtensionFunctionObject =
129-
Box<dyn Fn(&[Value]) -> evaluator::Result<ExtensionOutputValue> + Sync + Send + 'static>;
127+
macro_rules! extension_function_object {
128+
( $( $tys:ty ), * ) => {
129+
Box<dyn Fn($($tys,)*) -> evaluator::Result<ExtensionOutputValue> + Sync + Send + 'static>
130+
}
131+
}
132+
133+
/// Trait object that implements the extension function call accepting any number of arguments.
134+
pub type ExtensionFunctionObject = extension_function_object!(&[Value]);
135+
/// Trait object that implements the extension function call accepting exactly 0 arguments
136+
pub type NullaryExtensionFunctionObject = extension_function_object!();
137+
/// Trait object that implements the extension function call accepting exactly 1 arguments
138+
pub type UnaryExtensionFunctionObject = extension_function_object!(&Value);
139+
/// Trait object that implements the extension function call accepting exactly 2 arguments
140+
pub type BinaryExtensionFunctionObject = extension_function_object!(&Value, &Value);
141+
/// Trait object that implements the extension function call accepting exactly 3 arguments
142+
pub type TernaryExtensionFunctionObject = extension_function_object!(&Value, &Value, &Value);
130143

131144
/// Extension function. These can be called by the given `name` in Ceder
132145
/// expressions.
@@ -172,7 +185,7 @@ impl ExtensionFunction {
172185
pub fn nullary(
173186
name: Name,
174187
style: CallStyle,
175-
func: Box<dyn Fn() -> evaluator::Result<ExtensionOutputValue> + Sync + Send + 'static>,
188+
func: NullaryExtensionFunctionObject,
176189
return_type: SchemaType,
177190
) -> Self {
178191
Self::new(
@@ -200,14 +213,14 @@ impl ExtensionFunction {
200213
pub fn partial_eval_unknown(
201214
name: Name,
202215
style: CallStyle,
203-
func: Box<dyn Fn(Value) -> evaluator::Result<ExtensionOutputValue> + Sync + Send + 'static>,
216+
func: UnaryExtensionFunctionObject,
204217
arg_type: SchemaType,
205218
) -> Self {
206219
Self::new(
207220
name.clone(),
208221
style,
209222
Box::new(move |args: &[Value]| match args.first() {
210-
Some(arg) => func(arg.clone()),
223+
Some(arg) => func(arg),
211224
None => Err(evaluator::EvaluationError::wrong_num_arguments(
212225
name.clone(),
213226
1,
@@ -221,18 +234,19 @@ impl ExtensionFunction {
221234
}
222235

223236
/// Create a new `ExtensionFunction` taking one argument
237+
#[allow(clippy::type_complexity)]
224238
pub fn unary(
225239
name: Name,
226240
style: CallStyle,
227-
func: Box<dyn Fn(Value) -> evaluator::Result<ExtensionOutputValue> + Sync + Send + 'static>,
241+
func: UnaryExtensionFunctionObject,
228242
return_type: SchemaType,
229243
arg_type: SchemaType,
230244
) -> Self {
231245
Self::new(
232246
name.clone(),
233247
style,
234248
Box::new(move |args: &[Value]| match &args {
235-
&[arg] => func(arg.clone()),
249+
&[arg] => func(arg),
236250
_ => Err(evaluator::EvaluationError::wrong_num_arguments(
237251
name.clone(),
238252
1,
@@ -246,20 +260,19 @@ impl ExtensionFunction {
246260
}
247261

248262
/// Create a new `ExtensionFunction` taking two arguments
263+
#[allow(clippy::type_complexity)]
249264
pub fn binary(
250265
name: Name,
251266
style: CallStyle,
252-
func: Box<
253-
dyn Fn(Value, Value) -> evaluator::Result<ExtensionOutputValue> + Sync + Send + 'static,
254-
>,
267+
func: BinaryExtensionFunctionObject,
255268
return_type: SchemaType,
256269
arg_types: (SchemaType, SchemaType),
257270
) -> Self {
258271
Self::new(
259272
name.clone(),
260273
style,
261274
Box::new(move |args: &[Value]| match &args {
262-
&[first, second] => func(first.clone(), second.clone()),
275+
&[first, second] => func(first, second),
263276
_ => Err(evaluator::EvaluationError::wrong_num_arguments(
264277
name.clone(),
265278
2,
@@ -273,23 +286,19 @@ impl ExtensionFunction {
273286
}
274287

275288
/// Create a new `ExtensionFunction` taking three arguments
289+
#[allow(clippy::type_complexity)]
276290
pub fn ternary(
277291
name: Name,
278292
style: CallStyle,
279-
func: Box<
280-
dyn Fn(Value, Value, Value) -> evaluator::Result<ExtensionOutputValue>
281-
+ Sync
282-
+ Send
283-
+ 'static,
284-
>,
293+
func: TernaryExtensionFunctionObject,
285294
return_type: SchemaType,
286295
arg_types: (SchemaType, SchemaType, SchemaType),
287296
) -> Self {
288297
Self::new(
289298
name.clone(),
290299
style,
291300
Box::new(move |args: &[Value]| match &args {
292-
&[first, second, third] => func(first.clone(), second.clone(), third.clone()),
301+
&[first, second, third] => func(first, second, third),
293302
_ => Err(evaluator::EvaluationError::wrong_num_arguments(
294303
name.clone(),
295304
3,

cedar-policy-core/src/ast/policy_set.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ impl PolicySet {
433433
match self.templates.remove(policy_id) {
434434
Some(t) => {
435435
self.template_to_links_map.remove(policy_id);
436-
Ok((*t).clone())
436+
Ok(Arc::unwrap_or_clone(t))
437437
}
438438
None => panic!("Found in template_to_links_map but not in templates"),
439439
}
@@ -630,11 +630,10 @@ mod test {
630630
.expect("Failed to parse");
631631
pset.add_template(template).expect("Add failed");
632632

633-
let env: HashMap<SlotId, EntityUID> = [(
633+
let env: HashMap<SlotId, EntityUID> = std::iter::once((
634634
SlotId::principal(),
635635
r#"Test::"test""#.parse().expect("Failed to parse"),
636-
)]
637-
.into_iter()
636+
))
638637
.collect();
639638

640639
let r = pset.link(PolicyID::from_string("t"), PolicyID::from_string("id"), env);
@@ -669,11 +668,10 @@ mod test {
669668
)
670669
.expect("Failed to parse"),
671670
);
672-
let env1: HashMap<SlotId, EntityUID> = [(
671+
let env1: HashMap<SlotId, EntityUID> = std::iter::once((
673672
SlotId::principal(),
674673
r#"Test::"test1""#.parse().expect("Failed to parse"),
675-
)]
676-
.into_iter()
674+
))
677675
.collect();
678676

679677
let p1 = Template::link(Arc::clone(&template), PolicyID::from_string("link"), env1)
@@ -686,11 +684,10 @@ mod test {
686684
"Adding link should implicitly add the template"
687685
);
688686

689-
let env2: HashMap<SlotId, EntityUID> = [(
687+
let env2: HashMap<SlotId, EntityUID> = std::iter::once((
690688
SlotId::principal(),
691689
r#"Test::"test2""#.parse().expect("Failed to parse"),
692-
)]
693-
.into_iter()
690+
))
694691
.collect();
695692

696693
let p2 = Template::link(
@@ -717,11 +714,10 @@ mod test {
717714
)
718715
.expect("Failed to parse"),
719716
);
720-
let env3: HashMap<SlotId, EntityUID> = [(
717+
let env3: HashMap<SlotId, EntityUID> = std::iter::once((
721718
SlotId::resource(),
722719
r#"Test::"test3""#.parse().expect("Failed to parse"),
723-
)]
724-
.into_iter()
720+
))
725721
.collect();
726722

727723
let p4 = Template::link(
@@ -781,9 +777,7 @@ mod test {
781777
set.link(
782778
PolicyID::from_string("template"),
783779
PolicyID::from_string("id"),
784-
[(SlotId::principal(), EntityUID::with_eid("eid"))]
785-
.into_iter()
786-
.collect(),
780+
std::iter::once((SlotId::principal(), EntityUID::with_eid("eid"))).collect(),
787781
)
788782
.expect("Linking failed!");
789783
assert_eq!(set.static_policies().count(), 1);

0 commit comments

Comments
 (0)