pub struct NgynContext<'a> { /* private fields */ }Expand description
Represents the context of a request in Ngyn
Implementations§
Source§impl<'a> NgynContext<'a>
impl<'a> NgynContext<'a>
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();pub fn response(&mut self) -> &mut NgynResponse
response_mut() insteadSourcepub fn response_mut(&mut self) -> &mut NgynResponse
pub fn response_mut(&mut self) -> &mut NgynResponse
Retrieves the response associated with the context.
§Returns
A mutable reference to the response associated with the context.
§Examples
use ngyn_shared::core::context::NgynContext;
use http::Request;
let request = Request::new(Vec::new());
let context = NgynContext::from_request(request);
let response_ref = context.response_mut();Sourcepub fn params(&self) -> Option<&Params<'a, 'a>>
pub fn params(&self) -> Option<&Params<'a, 'a>>
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".to_string());
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);
let state_ref = context.state::<TestAppState>();Sourcepub fn state_mut<T: 'static>(&mut self) -> Option<&mut T>
pub fn state_mut<T: 'static>(&mut self) -> Option<&mut T>
Retrieves the state of the context as a mutable 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);
let state_ref = context.state::<TestAppState>();Source§impl<'b> NgynContext<'b>
impl<'b> NgynContext<'b>
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 (case-insensitive) 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".to_string());
let value: String = context.get("name").unwrap();
assert_eq!(value, "John".to_string());Sourcepub fn set<V: Serialize>(&mut self, key: &'b str, value: V)
pub fn set<V: Serialize>(&mut self, key: &'b str, value: V)
Sets the value associated with the given key in the context.
§Arguments
key- The key (case-insensitive) 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".to_string());
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 (case-insensitive) to remove the value for.
§Examples
use ngyn_shared::core::context::NgynContext;
let mut context = NgynContext::from_request(request);
context.set("name", "John".to_string());
context.remove("name");
let value = context.get::<String>("name");
assert_eq!(value, None);Sourcepub fn len(&self) -> usize
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".to_string());
context.set("age", 30.into());
assert_eq!(context.len(), 2);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 (case-insensitive) 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".to_string());
assert!(context.has("name"));
assert!(!context.has("age"));