From 123f491d3400db1d7e37c8179e1ae5392e9128e1 Mon Sep 17 00:00:00 2001 From: Aumetra Weisman Date: Thu, 19 Dec 2024 13:55:27 +0100 Subject: [PATCH] move mod --- lib/komainu/src/code_grant.rs | 6 +++--- lib/komainu/src/lib.rs | 2 +- lib/komainu/src/primitive/mod.rs | 3 --- lib/komainu/src/{primitive => }/scope.rs | 20 ++++++++++---------- lib/komainu/tests/scope.rs | 18 +++++++++--------- 5 files changed, 23 insertions(+), 26 deletions(-) delete mode 100644 lib/komainu/src/primitive/mod.rs rename lib/komainu/src/{primitive => }/scope.rs (85%) diff --git a/lib/komainu/src/code_grant.rs b/lib/komainu/src/code_grant.rs index 9ab8beb4..a5f144da 100644 --- a/lib/komainu/src/code_grant.rs +++ b/lib/komainu/src/code_grant.rs @@ -1,5 +1,5 @@ use crate::{ - error::Error, flow::pkce, params::ParamStorage, primitive::Scopes, AuthInstruction, Client, + error::Error, flow::pkce, params::ParamStorage, scope::Scope, AuthInstruction, Client, ClientExtractor, }; use std::{borrow::Cow, future::Future, ops::Deref, str::FromStr}; @@ -83,8 +83,8 @@ where return Err(GrantError::AccessDenied); } - let request_scopes = scope.split_whitespace().collect::(); - let client_scopes = client.scopes.iter().map(Deref::deref).collect::(); + let request_scopes = scope.parse().unwrap(); + let client_scopes = client.scopes.iter().map(Deref::deref).collect::(); // Check whether the client can actually perform the grant if !client_scopes.can_perform(&request_scopes) { diff --git a/lib/komainu/src/lib.rs b/lib/komainu/src/lib.rs index 34392c2e..e89bbd0e 100644 --- a/lib/komainu/src/lib.rs +++ b/lib/komainu/src/lib.rs @@ -10,7 +10,7 @@ pub mod error; pub mod extract; pub mod flow; pub mod params; -pub mod primitive; +pub mod scope; pub struct Authorization<'a> { pub code: Cow<'a, str>, diff --git a/lib/komainu/src/primitive/mod.rs b/lib/komainu/src/primitive/mod.rs deleted file mode 100644 index ba2c0930..00000000 --- a/lib/komainu/src/primitive/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod scope; - -pub use self::scope::Scopes; diff --git a/lib/komainu/src/primitive/scope.rs b/lib/komainu/src/scope.rs similarity index 85% rename from lib/komainu/src/primitive/scope.rs rename to lib/komainu/src/scope.rs index cf58090b..27074734 100644 --- a/lib/komainu/src/primitive/scope.rs +++ b/lib/komainu/src/scope.rs @@ -8,11 +8,11 @@ use std::{ #[derive(Default, Deserialize)] #[serde(transparent)] -pub struct Scopes { +pub struct Scope { inner: HashSet, } -impl FromStr for Scopes { +impl FromStr for Scope { type Err = Infallible; fn from_str(s: &str) -> Result { @@ -20,7 +20,7 @@ impl FromStr for Scopes { } } -impl Scopes { +impl Scope { #[inline] #[must_use] pub fn new() -> Self { @@ -58,24 +58,24 @@ impl Scopes { } } -impl FromIterator for Scopes +impl FromIterator for Scope where Item: Into, { #[inline] fn from_iter>(iter: T) -> Self { - let mut collection = Self::new(); - for item in iter { - collection.insert(item.into()); - } - collection + iter.into_iter().fold(Scope::new(), |mut acc, item| { + acc.insert(item.into()); + acc + }) } } -impl IntoIterator for Scopes { +impl IntoIterator for Scope { type Item = CompactString; type IntoIter = hash_set::IntoIter; + #[inline] fn into_iter(self) -> Self::IntoIter { self.inner.into_iter() } diff --git a/lib/komainu/tests/scope.rs b/lib/komainu/tests/scope.rs index da0ea6f1..5be38231 100644 --- a/lib/komainu/tests/scope.rs +++ b/lib/komainu/tests/scope.rs @@ -1,4 +1,4 @@ -use komainu::primitive::Scopes; +use komainu::scope::Scope; use rstest::rstest; #[rstest] @@ -6,8 +6,8 @@ use rstest::rstest; #[case("read write", "read write")] #[case("read write follow", "read write follow push")] fn can_perform(#[case] request: &str, #[case] client: &str) { - let request: Scopes = request.parse().unwrap(); - let client: Scopes = client.parse().unwrap(); + let request: Scope = request.parse().unwrap(); + let client: Scope = client.parse().unwrap(); assert!(client.can_perform(&request)); } @@ -17,8 +17,8 @@ fn can_perform(#[case] request: &str, #[case] client: &str) { #[case("read follow", "write")] #[case("write push", "read")] fn cant_perform(#[case] request: &str, #[case] client: &str) { - let request: Scopes = request.parse().unwrap(); - let client: Scopes = client.parse().unwrap(); + let request: Scope = request.parse().unwrap(); + let client: Scope = client.parse().unwrap(); assert!(!client.can_perform(&request)); } @@ -29,8 +29,8 @@ fn cant_perform(#[case] request: &str, #[case] client: &str) { #[case("follow", "read follow")] #[case("write follow", "follow write")] fn can_access(#[case] endpoint: &str, #[case] client: &str) { - let endpoint: Scopes = endpoint.parse().unwrap(); - let client: Scopes = client.parse().unwrap(); + let endpoint: Scope = endpoint.parse().unwrap(); + let client: Scope = client.parse().unwrap(); assert!(endpoint.can_be_accessed_by(&client)); } @@ -40,8 +40,8 @@ fn can_access(#[case] endpoint: &str, #[case] client: &str) { #[case("follow", "read write")] #[case("write follow", "read follow")] fn cant_access(#[case] endpoint: &str, #[case] client: &str) { - let endpoint: Scopes = endpoint.parse().unwrap(); - let client: Scopes = client.parse().unwrap(); + let endpoint: Scope = endpoint.parse().unwrap(); + let client: Scope = client.parse().unwrap(); assert!(!endpoint.can_be_accessed_by(&client)); }