Browse Source

Added the ability to construct Scopes manually

main
Alex Feldman-Crough 4 weeks ago
parent
commit
e9f53e75f2
  1. 37
      src/config/oauth2.rs

37
src/config/oauth2.rs

@ -1,3 +1,4 @@
use std::borrow::Borrow;
use serde::Deserialize;
use serde::de::{Deserializer, Visitor, SeqAccess, Error as _};
use serde::ser::{Serialize, Serializer, SerializeSeq};
@ -89,6 +90,35 @@ pub struct Scopes {
}
impl Scopes {
pub fn from_iter<I>(iter: I) -> Result<Self, ScopeError>
where
I: IntoIterator,
I::Item: Borrow<str>,
{
let mut string = String::with_capacity(1024);
let mut length = 0;
let mut iter = iter.into_iter();
if let Some(scope) = iter.next() {
let scope: &str = scope.borrow();
if scope.contains(' ') {
return Err(ScopeError(scope.into()));
}
string += scope;
length += 1;
}
for scope in iter {
let scope: &str = scope.borrow();
if scope.contains(' ') {
return Err(ScopeError(scope.into()));
}
string.push(' ');
string += scope;
length += 1;
}
string.shrink_to_fit();
Ok(Scopes { string, length })
}
pub fn as_str(&self) -> &str {
&self.string
}
@ -138,7 +168,6 @@ impl<'de> Deserialize<'de> for Scopes {
Ok(())
}
}
let mut string = String::with_capacity(1024);
let mut length = 0;
if let Some(s) = seq.next_element::<&'de str>()? {
@ -173,7 +202,11 @@ impl Serialize for Scopes {
}
}
mod def {
#[derive(Debug, thiserror::Error)]
#[error("Scopes must not have spaces, but {0:?} does")]
pub struct ScopeError(String);
pub mod def {
use url::Url;
const GOOGLE_OAUTH2_ENDPOINT: &str =

Loading…
Cancel
Save