use std::collections::HashMap;
#[derive(Debug, Clone, Default)]
pub struct BaseAuthorizationContext {
channel_id: Option<String>,
rpc_code: Option<String>,
ext_info: Option<HashMap<String, String>>,
}
impl BaseAuthorizationContext {
pub fn new() -> Self {
Self::default()
}
pub fn channel_id(&self) -> Option<&str> {
self.channel_id.as_deref()
}
pub fn set_channel_id(&mut self, channel_id: String) {
self.channel_id = Some(channel_id);
}
pub fn rpc_code(&self) -> Option<&str> {
self.rpc_code.as_deref()
}
pub fn set_rpc_code(&mut self, rpc_code: String) {
self.rpc_code = Some(rpc_code);
}
pub fn get_ext_info(&self, key: &str) -> Option<&str> {
self.ext_info.as_ref()?.get(key).map(|s| s.as_str())
}
pub fn set_ext_info(&mut self, key: String, value: String) {
if key.is_empty() {
return;
}
self.ext_info.get_or_insert_with(HashMap::new).insert(key, value);
}
pub fn has_ext_info(&self, key: &str) -> bool {
self.ext_info.as_ref().is_some_and(|map| map.contains_key(key))
}
pub fn ext_info(&self) -> Option<&HashMap<String, String>> {
self.ext_info.as_ref()
}
pub fn set_ext_info_map(&mut self, ext_info: HashMap<String, String>) {
self.ext_info = Some(ext_info);
}
pub fn take_ext_info(&mut self) -> Option<HashMap<String, String>> {
self.ext_info.take()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_context() {
let context = BaseAuthorizationContext::new();
assert!(context.channel_id().is_none());
assert!(context.rpc_code().is_none());
assert!(context.ext_info().is_none());
}
#[test]
fn test_channel_id() {
let mut context = BaseAuthorizationContext::new();
context.set_channel_id("channel-123".to_string());
assert_eq!(context.channel_id(), Some("channel-123"));
}
#[test]
fn test_rpc_code() {
let mut context = BaseAuthorizationContext::new();
context.set_rpc_code("10".to_string());
assert_eq!(context.rpc_code(), Some("10"));
}
#[test]
fn test_ext_info_operations() {
let mut context = BaseAuthorizationContext::new();
assert!(!context.has_ext_info("key1"));
assert!(context.get_ext_info("key1").is_none());
context.set_ext_info("key1".to_string(), "value1".to_string());
context.set_ext_info("key2".to_string(), "value2".to_string());
assert!(context.has_ext_info("key1"));
assert!(context.has_ext_info("key2"));
assert!(!context.has_ext_info("key3"));
assert_eq!(context.get_ext_info("key1"), Some("value1"));
assert_eq!(context.get_ext_info("key2"), Some("value2"));
assert_eq!(context.get_ext_info("key3"), None);
let map = context.ext_info().unwrap();
assert_eq!(map.len(), 2);
assert_eq!(map.get("key1").unwrap(), "value1");
}
#[test]
fn test_set_ext_info_map() {
let mut context = BaseAuthorizationContext::new();
let mut map = HashMap::new();
map.insert("key1".to_string(), "value1".to_string());
map.insert("key2".to_string(), "value2".to_string());
context.set_ext_info_map(map);
assert_eq!(context.get_ext_info("key1"), Some("value1"));
assert_eq!(context.get_ext_info("key2"), Some("value2"));
assert_eq!(context.ext_info().unwrap().len(), 2);
}
#[test]
fn test_take_ext_info() {
let mut context = BaseAuthorizationContext::new();
context.set_ext_info("key1".to_string(), "value1".to_string());
let taken = context.take_ext_info();
assert!(taken.is_some());
assert_eq!(taken.unwrap().get("key1").unwrap(), "value1");
assert!(context.ext_info().is_none());
}
#[test]
fn test_empty_key_ignored() {
let mut context = BaseAuthorizationContext::new();
context.set_ext_info("".to_string(), "value".to_string());
assert!(context.ext_info().is_none());
}
}