From 32f4b40e078c0089a14483f0c8f8dfa107883419 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Thu, 4 Aug 2022 07:40:56 -0700 Subject: [PATCH] Fix unused singletons in lib_guide.rs (#1539) * Make it one file again by instantiating a fresh Oso each time. Co-authored-by: Graham Kaemmer Co-authored-by: Stephen Olsen Co-authored-by: Sam Scott --- languages/rust/oso/examples/lib_guide.rs | 30 ++++++++++++++++-------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/languages/rust/oso/examples/lib_guide.rs b/languages/rust/oso/examples/lib_guide.rs index f0ee62b173..a0776d61cb 100644 --- a/languages/rust/oso/examples/lib_guide.rs +++ b/languages/rust/oso/examples/lib_guide.rs @@ -13,13 +13,15 @@ fn types() -> anyhow::Result<()> { is_admin: bool, } oso.register_class(User1::get_polar_class())?; - oso.load_str(r#"allow(actor: User1, action, resource) if actor.is_admin;"#)?; + oso.load_str(r#"allow(actor: User1, _action, _resource) if actor.is_admin;"#)?; let user1 = User1 { name: "alice".to_string(), is_admin: true, }; assert!(oso.is_allowed(user1, "foo", "bar")?); + let mut oso = Oso::new(); + #[derive(Clone, PolarClass)] struct User2 { #[polar(attribute)] @@ -44,9 +46,15 @@ fn types() -> anyhow::Result<()> { .add_method("is_called_alice", User2::is_called_alice) .build(), )?; - oso.load_str(r#"allow(user: User2, _, _) if user.is_admin;"#)?; - oso.load_str(r#"?= allow(new User2("bob", true), "foo", "bar");"#)?; - oso.load_str(r#"?= new User2("alice", true).is_called_alice();"#)?; + oso.load_str( + r#" + allow(user: User2, _, _) if user.is_admin; + ?= allow(new User2("bob", true), "foo", "bar"); + ?= new User2("alice", true).is_called_alice(); + "#, + )?; + + let mut oso = Oso::new(); #[derive(Clone, PolarClass)] struct User3 { @@ -56,7 +64,7 @@ fn types() -> anyhow::Result<()> { is_admin: bool, } oso.register_class(User3::get_polar_class())?; - oso.load_str(r#"allow(actor, action, resource) if actor matches User3{name: "alice"};"#)?; + oso.load_str(r#"allow(actor, _action, _resource) if actor matches User3{name: "alice"};"#)?; let user3 = User3 { name: "alice".to_string(), is_admin: true, @@ -78,7 +86,9 @@ fn strings() -> anyhow::Result<()> { oso.register_class(User::get_polar_class())?; - oso.load_str(r#"allow(actor, action, resource) if actor.username.ends_with("example.com");"#)?; + oso.load_str( + r#"allow(actor, _action, _resource) if actor.username.ends_with("example.com");"#, + )?; let user = User { username: "alice@example.com".to_owned(), @@ -99,7 +109,7 @@ fn vecs() -> anyhow::Result<()> { oso.register_class(User::get_polar_class()).unwrap(); - oso.load_str(r#"allow(actor, action, resource) if "HR" in actor.groups;"#)?; + oso.load_str(r#"allow(actor, _action, _resource) if "HR" in actor.groups;"#)?; let user = User { groups: vec!["HR".to_string(), "payroll".to_string()], @@ -118,7 +128,7 @@ fn maps() -> anyhow::Result<()> { } oso.register_class(User::get_polar_class())?; - oso.load_str(r#"allow(actor, action, resource) if actor.roles.project1 = "admin";"#)?; + oso.load_str(r#"allow(actor, _action, _resource) if actor.roles.project1 = "admin";"#)?; let user = User { roles: maplit::hashmap! { "project1".to_string() => "admin".to_string() }, @@ -144,7 +154,7 @@ fn enums() -> anyhow::Result<()> { .add_method("is_admin", |u: &UserType| matches!(u, UserType::Admin)) .build(), )?; - oso.load_str(r#"allow(actor, action, resource) if actor.is_admin();"#)?; + oso.load_str(r#"allow(actor, _action, _resource) if actor.is_admin();"#)?; let user = UserType::Admin; assert!(oso.is_allowed(user, "foo", "bar")?); @@ -168,7 +178,7 @@ fn iters() -> anyhow::Result<()> { ) .unwrap(); - oso.load_str(r#"allow(actor, action, resource) if "payroll" in actor.get_group();"#)?; + oso.load_str(r#"allow(actor, _action, _resource) if "payroll" in actor.get_group();"#)?; let user = User { groups: vec!["HR".to_string(), "payroll".to_string()],