Use PhantomData intead of two Options useless for Authorization

And remove some warnings about unused parameters
This commit is contained in:
Baptiste Gelez 2018-10-24 12:21:42 +01:00
parent 9784d754b2
commit 28fbf35779
2 changed files with 7 additions and 10 deletions

View file

@ -31,11 +31,11 @@ pub struct NewApp {
impl Provider<Connection> for App {
type Data = AppEndpoint;
fn get(conn: &Connection, id: i32) -> Result<AppEndpoint, Error> {
fn get(_conn: &Connection, _id: i32) -> Result<AppEndpoint, Error> {
unimplemented!()
}
fn list(conn: &Connection, query: AppEndpoint) -> Vec<AppEndpoint> {
fn list(_conn: &Connection, _query: AppEndpoint) -> Vec<AppEndpoint> {
unimplemented!()
}
@ -61,11 +61,11 @@ impl Provider<Connection> for App {
})
}
fn update(conn: &Connection, id: i32, new_data: AppEndpoint) -> Result<AppEndpoint, Error> {
fn update(_conn: &Connection, _id: i32, _new_data: AppEndpoint) -> Result<AppEndpoint, Error> {
unimplemented!()
}
fn delete(conn: &Connection, id: i32) {
fn delete(_conn: &Connection, _id: i32) {
unimplemented!()
}
}

View file

@ -3,6 +3,7 @@ use rocket::{
http::Status,
request::{self, FromRequest, Request}
};
use std::marker::PhantomData;
use plume_models::{self, api_tokens::ApiToken};
// Actions
@ -32,11 +33,7 @@ impl Scope for plume_models::posts::Post {
}
}
// We have to use A and S in the struct definition
// otherwise rustc complains they are useless
//
// A nicer solution is probably possible.
pub struct Authorization<A, S> (Option<A>, Option<S>);
pub struct Authorization<A, S> (PhantomData<(A, S)>);
impl<'a, 'r, A, S> FromRequest<'a, 'r> for Authorization<A, S>
where A: Action,
@ -48,7 +45,7 @@ where A: Action,
request.guard::<ApiToken>()
.map_failure(|_| (Status::Unauthorized, ()))
.and_then(|token| if token.can(A::to_str(), S::to_str()) {
Outcome::Success(Authorization(None, None))
Outcome::Success(Authorization(PhantomData))
} else {
Outcome::Failure((Status::Unauthorized, ()))
})