Struct ngyn_shared::server::context::NgynContext

source ·
pub struct NgynContext { /* private fields */ }
Expand description

Represents the context of a request in Ngyn

Implementations§

source§

impl NgynContext

source

pub fn request(&self) -> &Request<Vec<u8>>

Retrieves the request associated with the context.

§Returns

A reference to the request associated with the context.

§Examples
use ngyn_shared::core::context::NgynContext;
use hyper::Request;

let request = Request::new(Vec::new());
let context = NgynContext::from_request(request);

let request_ref = context.request();
source

pub fn params(&self) -> Option<&Vec<(String, String)>>

Retrieves the params associated with the context.

§Returns

An optional reference to the params associated with the context.

§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);
context.set("name", "John".into());

let params_ref = context.params();
source§

impl NgynContext

source

pub fn state<T: 'static>(&self) -> Option<&T>

Retrieves the state of the context as a reference to the specified type.

§Type Parameters
  • T - The type of the state to retrieve.
§Returns

An optional reference to the state of the specified type. Returns None if the state is not found or if it cannot be downcasted to the specified type.

§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);
context.set_state(Box::new(MyAppState::new()));

let state_ref = context.state::<MyAppState>();
source§

impl NgynContext

source

pub fn get<V: for<'a> Deserialize<'a>>(&self, key: &str) -> Option<V>

Retrieves the value associated with the given key from the context.

§Arguments
  • key - The key to retrieve the value for.
§Returns

A reference to the value associated with the key. If the key is not found, returns a reference to an empty context value.

§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);
context.set("name", "John".into());

let value: String = context.get("name").unwrap();
assert_eq!(value, "John".to_string());
source

pub fn set<V: Serialize>(&mut self, key: &str, value: V)

Sets the value associated with the given key in the context.

§Arguments
  • key - The key to set the value for.
  • value - The value to associate with the key.
§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);
context.set("name", "John".into());

let value: String = context.get("name").unwrap();
assert_eq!(value, "John".to_string());
source

pub fn remove(&mut self, key: &str)

Removes the value associated with the given key from the context.

§Arguments
  • key - The key to remove the value for.
§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);
context.set("name", "John".into());

context.remove("name");
let value = context.get::<String>("name");
assert_eq!(value, None);
source

pub fn clear(&mut self)

Clears all values from the context.

§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);
context.set("name", "John".into());
context.set("age", 30.into());

context.clear();
assert_eq!(context.len(), 0);
source

pub fn len(&self) -> usize

Returns the number of values in the context.

§Returns

The number of values in the context.

§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);
context.set("name", "John".into());
context.set("age", 30.into());

assert_eq!(context.len(), 2);
source

pub fn is_empty(&self) -> bool

Checks if the context is empty.

§Returns

true if the context is empty, false otherwise.

§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);

assert!(context.is_empty());

context.set("name", "John".into());
assert!(!context.is_empty());
source

pub fn has(&self, key: &str) -> bool

Checks if the context contains a value for the given key.

§Arguments
  • key - The key to check for.
§Returns

true if the context contains a value for the key, false otherwise.

§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);
context.set("name", "John".into());

assert!(context.has("name"));
assert!(!context.has("age"));
source§

impl NgynContext

source

pub fn is_valid_route(&self) -> bool

Checks if the context has a valid route. A valid route is when the route information and the params are set. This is great for differentiating known routes from unknown routes.

§Returns

true if the context has a valid route, false otherwise.

Trait Implementations§

source§

impl<'a> Transformer<'a> for &'a NgynContext

source§

fn transform(cx: &'a mut NgynContext, _res: &'a mut NgynResponse) -> Self

Transforms the given NgynContext and NgynResponse and returns an instance of Self. Read more
source§

impl<'a> Transformer<'a> for &'a mut NgynContext

source§

fn transform(cx: &'a mut NgynContext, _res: &'a mut NgynResponse) -> Self

Transforms the given NgynContext and NgynResponse and returns an instance of Self. Read more

Auto Trait Implementations§

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<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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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<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