Skip to content

Commit

Permalink
move mod
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Dec 19, 2024
1 parent a948dbe commit 123f491
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 26 deletions.
6 changes: 3 additions & 3 deletions lib/komainu/src/code_grant.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -83,8 +83,8 @@ where
return Err(GrantError::AccessDenied);
}

let request_scopes = scope.split_whitespace().collect::<Scopes>();
let client_scopes = client.scopes.iter().map(Deref::deref).collect::<Scopes>();
let request_scopes = scope.parse().unwrap();
let client_scopes = client.scopes.iter().map(Deref::deref).collect::<Scope>();

// Check whether the client can actually perform the grant
if !client_scopes.can_perform(&request_scopes) {
Expand Down
2 changes: 1 addition & 1 deletion lib/komainu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
3 changes: 0 additions & 3 deletions lib/komainu/src/primitive/mod.rs

This file was deleted.

20 changes: 10 additions & 10 deletions lib/komainu/src/primitive/scope.rs → lib/komainu/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ use std::{

#[derive(Default, Deserialize)]
#[serde(transparent)]
pub struct Scopes {
pub struct Scope {
inner: HashSet<CompactString>,
}

impl FromStr for Scopes {
impl FromStr for Scope {
type Err = Infallible;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(s.split_whitespace().collect())
}
}

impl Scopes {
impl Scope {
#[inline]
#[must_use]
pub fn new() -> Self {
Expand Down Expand Up @@ -58,24 +58,24 @@ impl Scopes {
}
}

impl<Item> FromIterator<Item> for Scopes
impl<Item> FromIterator<Item> for Scope
where
Item: Into<CompactString>,
{
#[inline]
fn from_iter<T: IntoIterator<Item = Item>>(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<Self::Item>;

#[inline]
fn into_iter(self) -> Self::IntoIter {
self.inner.into_iter()
}
Expand Down
18 changes: 9 additions & 9 deletions lib/komainu/tests/scope.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use komainu::primitive::Scopes;
use komainu::scope::Scope;
use rstest::rstest;

#[rstest]
#[case("read", "read write")]
#[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));
}
Expand All @@ -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));
}
Expand All @@ -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));
}
Expand All @@ -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));
}

0 comments on commit 123f491

Please sign in to comment.