pub struct JSONFileHandler { /* private fields */ }
Expand description
A handler for retrieving values from a specified JSON file.
This struct is responsible for handling requests by reading content from the file
specified in the underlying FileHandler
, and then searching for a specific key
within the parsed JSON structure. If the key is not found in the JSON structure,
it delegates the request to the next handler (if provided).
use cor_args::{JSONFileHandler, Handler};
// Create a new JSONFileHandler specifying a path to a file.
let handler = JSONFileHandler::new("file.json");
// Add a fallback handler
//let handler = handler.next(some_other_handler.into());
// Handle a configuration request matching a `"some_key"` within `file.json`
let value = handler.handle_request("some_key");
Implementations§
Trait Implementations§
Source§impl Handler for JSONFileHandler
impl Handler for JSONFileHandler
Source§fn handle_request(&self, key: &str) -> Option<String>
fn handle_request(&self, key: &str) -> Option<String>
Retrieves a value for the specified key from the JSON file.
This implementation attempts to read content from the file specified in the underlying FileHandler
,
parses the content as JSON, and then searches for the specified key within the parsed JSON structure.
If the key is not found in the JSON structure, and if a next handler is provided, it delegates the request
to the next handler. If there’s no next handler, or if the key is not found in both the JSON structure
and the next handler, it returns None
.
§Arguments
key
- The key for which the value needs to be retrieved from the JSON file.
§Returns
An Option
containing the value associated with the key, or None
if the key is not found.