1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-30 18:34:36 +01:00

session code cleanup

This commit is contained in:
Nikolay Kim 2018-01-22 10:50:24 -08:00
parent 7910c7b511
commit 5b9b8c6dd9

View File

@ -27,11 +27,7 @@ pub struct RedisSession {
impl SessionImpl for RedisSession { impl SessionImpl for RedisSession {
fn get(&self, key: &str) -> Option<&str> { fn get(&self, key: &str) -> Option<&str> {
if let Some(s) = self.state.get(key) { self.state.get(key).map(|s| s.as_str())
Some(s)
} else {
None
}
} }
fn set(&mut self, key: &str, value: String) { fn set(&mut self, key: &str, value: String) {
@ -88,6 +84,7 @@ impl RedisSessionBackend {
self self
} }
/// Set custom cookie name for session id
pub fn cookie_name(mut self, name: &str) -> Self { pub fn cookie_name(mut self, name: &str) -> Self {
Rc::get_mut(&mut self.0).unwrap().name = name.to_owned(); Rc::get_mut(&mut self.0).unwrap().name = name.to_owned();
self self
@ -108,15 +105,13 @@ impl<S> SessionBackend<S> for RedisSessionBackend {
changed: false, changed: false,
inner: inner, inner: inner,
state: state, state: state,
value: Some(value), value: Some(value) }
}
} else { } else {
RedisSession { RedisSession {
changed: false, changed: false,
inner: inner, inner: inner,
state: HashMap::new(), state: HashMap::new(),
value: None, value: None }
}
} }
})) }))
} }
@ -130,8 +125,10 @@ struct Inner {
} }
impl Inner { impl Inner {
#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
fn load<S>(&self, req: &mut HttpRequest<S>) fn load<S>(&self, req: &mut HttpRequest<S>)
-> Box<Future<Item=Option<(HashMap<String, String>, String)>, Error=Error>> { -> Box<Future<Item=Option<(HashMap<String, String>, String)>, Error=Error>>
{
if let Ok(cookies) = req.cookies() { if let Ok(cookies) = req.cookies() {
for cookie in cookies { for cookie in cookies {
if cookie.name() == self.name { if cookie.name() == self.name {
@ -142,8 +139,7 @@ impl Inner {
return Box::new( return Box::new(
self.addr.call_fut(Command(resp_array!["GET", cookie.value()])) self.addr.call_fut(Command(resp_array!["GET", cookie.value()]))
.map_err(Error::from) .map_err(Error::from)
.and_then(move |res| { .and_then(move |res| match res {
match res {
Ok(val) => { Ok(val) => {
match val { match val {
RespValue::Error(err) => RespValue::Error(err) =>
@ -163,7 +159,6 @@ impl Inner {
Ok(None) Ok(None)
}, },
Err(err) => Err(error::ErrorInternalServerError(err).into()) Err(err) => Err(error::ErrorInternalServerError(err).into())
}
})) }))
} else { } else {
return Box::new(FutOk(None)) return Box::new(FutOk(None))
@ -198,13 +193,11 @@ impl Inner {
Box::new( Box::new(
match serde_json::to_string(state) { match serde_json::to_string(state) {
Err(e) => Either::A(FutErr(e.into())), Err(e) => Either::A(FutErr(e.into())),
Ok(body) => { Ok(body) => Either::B(
Either::B(
self.addr.call_fut( self.addr.call_fut(
Command(resp_array!["SET", value, body,"EX", &self.ttl])) Command(resp_array!["SET", value, body,"EX", &self.ttl]))
.map_err(Error::from) .map_err(Error::from)
.and_then(move |res| { .and_then(move |res| match res {
match res {
Ok(_) => { Ok(_) => {
if let Some(jar) = jar { if let Some(jar) = jar {
for cookie in jar.delta() { for cookie in jar.delta() {
@ -216,9 +209,7 @@ impl Inner {
Ok(resp) Ok(resp)
}, },
Err(err) => Err(error::ErrorInternalServerError(err).into()) Err(err) => Err(error::ErrorInternalServerError(err).into())
}
})) }))
}
}) })
} }
} }