Struct Session

Source
pub struct Session<S: SessionStore = RedisStore> { /* private fields */ }
Expand description

A parsed on-demand session store.

The default store is the RedisStore

Implementations§

Source§

impl<S> Session<S>
where S: SessionStore,

Source

pub fn new(inner: Arc<Inner<S>>) -> Self

Creates a new Session instance.

Source

pub async fn get<T>(&self, field: &str) -> Result<Option<T>, Error>

Retrieves the value of a field from the session store.

§Example
use ruts::{Session};
use fred::clients::Client;
use serde::Deserialize;
use ruts::store::redis::RedisStore;

#[derive(Clone, Deserialize)]
struct User {
    id: i64,
    name: String,
}

#[derive(Clone, Deserialize)]
enum Theme {
    Light,
    Dark,
}

#[derive(Clone, Deserialize)]
struct AppSession {
    user: User,
    theme: Option<Theme>,
}

async fn some_handler_could_be_axum(session: Session<RedisStore<Client>>) {
    session.get::<AppSession>("app").await.unwrap();
}
Source

pub async fn get_all<T>(&self) -> Result<Option<T>, Error>

Retrieves values for all fields from the session store.

Source

pub async fn insert<T>( &self, field: &str, value: &T, field_expire: Option<i64>, ) -> Result<bool, Error>
where T: Send + Sync + Serialize,

Inserts a value into the session store.

Returns true if the value was successfully inserted.

§Example
use ruts::{Session};
use fred::clients::Client;
use serde::Serialize;
use ruts::store::redis::RedisStore;

#[derive(Serialize)]
struct User {
    id: i64,
    name: String,
}

#[derive(Serialize)]
enum Theme {
    Light,
    Dark,
}

#[derive(Serialize)]
struct AppSession {
    user: User,
    theme: Option<Theme>,
}

async fn some_handler_could_be_axum(session: Session<RedisStore<Client>>) {
    let app = AppSession {
            user: User {
            id: 34895634,
            name: String::from("John Doe"),
        },
        theme: Some(Theme::Dark),
    };

    session.insert("app", &app, Some(5)).await.unwrap();
}
Source

pub async fn update<T>( &self, field: &str, value: &T, field_expire: Option<i64>, ) -> Result<bool, Error>
where T: Send + Sync + Serialize,

Updates a value in the session store.

If the key doesn’t exist, it will be inserted.

Returns true if the value was successfully updated or inserted.

§Example
use ruts::{Session};
use fred::clients::Client;
use serde::Serialize;
use ruts::store::redis::RedisStore;

#[derive(Serialize)]
struct User {
    id: i64,
    name: String,
}

#[derive(Serialize)]
enum Theme {
    Light,
    Dark,
}

#[derive(Serialize)]
struct AppSession {
    user: User,
    theme: Option<Theme>,
}

async fn some_handler_could_be_axum(session: Session<RedisStore<Client>>) {
    let app = AppSession {
        user: User {
            id: 21342365,
            name: String::from("Jane Doe"),
        },
        theme: Some(Theme::Light),
    };

    let updated = session.update("app", &app, Some(5)).await.unwrap();
}
Source

pub async fn remove(&self, field: &str) -> Result<i8, Error>

Removes a field along with its value from the session store.

Returns true if the field was successfully removed.

§Example
use ruts::{Session};
use fred::clients::Client;
use ruts::store::redis::RedisStore;

async fn some_handler_could_be_axum(session: Session<RedisStore<Client>>) {
    let removed = session.remove("app").await.unwrap();
}
Source

pub async fn delete(&self) -> Result<bool, Error>

Deletes the entire session from the store.

Returns true if the session was successfully deleted.

§Example
use ruts::{Session};
use fred::clients::Client;
use ruts::store::redis::RedisStore;

async fn some_handler_could_be_axum(session: Session<RedisStore<Client>>) {
    let deleted = session.delete().await.unwrap();
}
Source

pub async fn expire(&self, seconds: i64) -> Result<bool, Error>

Updates the cookie’s max-age and session expiry time in the store.

A value of -1 or 0 immediately expires the session and deletes it.

Returns true if the expiry was successfully updated.

§Example
use ruts::{Session};
use fred::clients::Client;
use ruts::store::redis::RedisStore;

async fn some_handler_could_be_axum(session: Session<RedisStore<Client>>) {
    session.expire(30).await.unwrap();
}
Source

pub fn set_expiration(&self, seconds: i64)

Updates the cookie max-age.

Any subsequent call to insert, update or regenerate within this request cycle will use this value.

NOTE: This does not change the max-age value set in the CookieOptions.

Source

pub async fn regenerate(&self) -> Result<Option<Id>, Error>

Regenerates the session with a new ID.

Returns the new session ID if successful.

§Example
use ruts::{Session};
use fred::clients::Client;
use ruts::store::redis::RedisStore;

async fn some_handler_could_be_axum(session: Session<RedisStore<Client>>) {
    let id = session.regenerate().await.unwrap();
}
Source

pub fn id(&self) -> Option<Id>

Returns the session ID, if it exists.

Trait Implementations§

Source§

impl<S: Debug + SessionStore> Debug for Session<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S, T> FromRequestParts<S> for Session<T>
where S: Sync + Send, T: SessionStore,

axum extractor for Session.

Source§

type Rejection = (StatusCode, &'static str)

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

async fn from_request_parts( parts: &mut Parts, _state: &S, ) -> Result<Self, Self::Rejection>

Perform the extraction.

Auto Trait Implementations§

§

impl<S> Freeze for Session<S>

§

impl<S = RedisStore> !RefUnwindSafe for Session<S>

§

impl<S> Send for Session<S>

§

impl<S> Sync for Session<S>

§

impl<S> Unpin for Session<S>

§

impl<S = RedisStore> !UnwindSafe for Session<S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S, T> FromRequest<S, ViaParts> for T
where S: Send + Sync, T: FromRequestParts<S>,

Source§

type Rejection = <T as FromRequestParts<S>>::Rejection

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

async fn from_request( req: Request<Body>, state: &S, ) -> Result<T, <T as FromRequest<S, ViaParts>>::Rejection>

Perform the extraction.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more