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
impl NgynContext
sourcepub fn request(&self) -> &Request<Vec<u8>>
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();sourcepub fn params(&self) -> Option<&Vec<(String, String)>>
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
impl NgynContext
sourcepub fn state<T: 'static>(&self) -> Option<&T>
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
impl NgynContext
sourcepub fn get<V: for<'a> Deserialize<'a>>(&self, key: &str) -> Option<V>
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());sourcepub fn set<V: Serialize>(&mut self, key: &str, value: V)
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());sourcepub fn remove(&mut self, key: &str)
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);sourcepub fn has(&self, key: &str) -> bool
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
impl NgynContext
sourcepub fn is_valid_route(&self) -> bool
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.