use crate::policy::{AttributeIdentifier, AttributeValue, FqnError};
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AttributeFqn {
namespace: String,
name: String,
value: Option<String>,
}
impl AttributeFqn {
pub fn parse(s: &str) -> Result<Self, FqnError> {
Self::parse_with_rules(s, &FqnValidationRules::default())
}
pub fn parse_with_rules(s: &str, rules: &FqnValidationRules) -> Result<Self, FqnError> {
if rules.require_https && s.starts_with("http://") {
return Err(FqnError::NotHttps { url: s.to_string() });
}
let (scheme, rest) = s
.split_once("://")
.ok_or_else(|| FqnError::MalformedUrl("Missing scheme separator ://".to_string()))?;
if scheme != "https" && scheme != "http" {
return Err(FqnError::InvalidScheme {
expected: "https",
found: scheme.to_string(),
});
}
let (namespace, path) = match rest.split_once('/') {
Some((ns, p)) => (ns, p),
None => {
return Ok(Self {
namespace: rest.to_lowercase(),
name: String::new(),
value: None,
});
}
};
if namespace.is_empty() {
return Err(FqnError::MissingComponent {
component: "namespace",
});
}
let namespace = namespace.to_lowercase();
if rules.require_attr_structure {
if !path.starts_with("attr/") {
return Err(FqnError::MissingAttrStructure { url: s.to_string() });
}
let path = &path[5..];
if path.is_empty() {
return Err(FqnError::MissingComponent { component: "name" });
}
if let Some((name, value_part)) = path.split_once("/value/") {
Ok(Self {
namespace,
name: percent_decode(name),
value: Some(percent_decode(value_part)),
})
} else {
Ok(Self {
namespace,
name: percent_decode(path),
value: None,
})
}
} else {
let parts: Vec<&str> = path.split('/').collect();
match parts.as_slice() {
[] => Ok(Self {
namespace,
name: String::new(),
value: None,
}),
[name] => Ok(Self {
namespace,
name: percent_decode(name),
value: None,
}),
[name, value] => Ok(Self {
namespace,
name: percent_decode(name),
value: Some(percent_decode(value)),
}),
_ => Ok(Self {
namespace,
name: percent_decode(parts[0]),
value: Some(percent_decode(&parts[1..].join("/"))),
}),
}
}
}
pub fn namespace(ns: &str) -> Self {
Self {
namespace: ns.to_lowercase(),
name: String::new(),
value: None,
}
}
pub fn attribute(ns: &str, name: &str) -> Self {
Self {
namespace: ns.to_lowercase(),
name: name.to_string(),
value: None,
}
}
pub fn with_value(ns: &str, name: &str, value: &str) -> Self {
Self {
namespace: ns.to_lowercase(),
name: name.to_string(),
value: Some(value.to_string()),
}
}
pub fn get_namespace(&self) -> &str {
&self.namespace
}
pub fn get_name(&self) -> &str {
&self.name
}
pub fn get_value(&self) -> Option<&str> {
self.value.as_deref()
}
pub fn to_url(&self) -> String {
if self.name.is_empty() {
format!("https://{}", self.namespace)
} else if let Some(value) = &self.value {
format!(
"https://{}/attr/{}/value/{}",
self.namespace,
percent_encode(&self.name),
percent_encode(value)
)
} else {
format!(
"https://{}/attr/{}",
self.namespace,
percent_encode(&self.name)
)
}
}
pub fn to_identifier(&self) -> AttributeIdentifier {
AttributeIdentifier::new(&self.namespace, &self.name)
}
pub fn to_attribute_value(&self) -> Option<AttributeValue> {
self.value
.as_ref()
.map(|v| AttributeValue::String(v.clone()))
}
pub fn validate_namespace(&self, registry: &NamespaceRegistry) -> Result<(), FqnError> {
if registry.is_registered(&self.namespace) {
Ok(())
} else {
Err(FqnError::NamespaceNotRegistered {
namespace: self.namespace.clone(),
})
}
}
}
impl std::fmt::Display for AttributeFqn {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_url())
}
}
impl std::str::FromStr for AttributeFqn {
type Err = FqnError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse(s)
}
}
impl<'a> TryFrom<&'a str> for AttributeFqn {
type Error = FqnError;
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
Self::parse(s)
}
}
#[derive(Debug, Clone)]
pub struct FqnValidationRules {
pub require_https: bool,
pub require_attr_structure: bool,
pub validate_namespace_registry: bool,
}
impl Default for FqnValidationRules {
fn default() -> Self {
Self {
require_https: true,
require_attr_structure: true,
validate_namespace_registry: false,
}
}
}
#[derive(Debug, Clone)]
pub struct NamespaceRegistry {
namespaces: HashMap<String, NamespaceInfo>,
}
#[derive(Debug, Clone)]
pub struct NamespaceInfo {
pub name: String,
pub description: Option<String>,
}
impl NamespaceRegistry {
pub fn new() -> Self {
Self {
namespaces: HashMap::new(),
}
}
pub fn register(&mut self, namespace: &str) -> &mut Self {
self.namespaces.insert(
namespace.to_lowercase(),
NamespaceInfo {
name: namespace.to_string(),
description: None,
},
);
self
}
pub fn register_with_description(&mut self, namespace: &str, description: &str) -> &mut Self {
self.namespaces.insert(
namespace.to_lowercase(),
NamespaceInfo {
name: namespace.to_string(),
description: Some(description.to_string()),
},
);
self
}
pub fn is_registered(&self, namespace: &str) -> bool {
self.namespaces.contains_key(&namespace.to_lowercase())
}
pub fn get(&self, namespace: &str) -> Option<&NamespaceInfo> {
self.namespaces.get(&namespace.to_lowercase())
}
}
impl Default for NamespaceRegistry {
fn default() -> Self {
Self::new()
}
}
fn percent_encode(s: &str) -> String {
s.chars()
.map(|c| match c {
'A'..='Z' | 'a'..='z' | '0'..='9' | '-' | '_' | '.' | '~' => c.to_string(),
_ => format!("%{:02X}", c as u8),
})
.collect()
}
fn percent_decode(s: &str) -> String {
let mut result = String::new();
let mut chars = s.chars();
while let Some(c) = chars.next() {
if c == '%' {
let hex: String = chars.by_ref().take(2).collect();
if hex.len() == 2
&& let Ok(byte) = u8::from_str_radix(&hex, 16)
{
result.push(byte as char);
continue;
}
result.push('%');
result.push_str(&hex);
} else {
result.push(c);
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_full_fqn() {
let fqn =
AttributeFqn::parse("https://example.com/attr/classification/value/secret").unwrap();
assert_eq!(fqn.get_namespace(), "example.com");
assert_eq!(fqn.get_name(), "classification");
assert_eq!(fqn.get_value(), Some("secret"));
}
#[test]
fn test_parse_attribute_only() {
let fqn = AttributeFqn::parse("https://example.com/attr/classification").unwrap();
assert_eq!(fqn.get_namespace(), "example.com");
assert_eq!(fqn.get_name(), "classification");
assert_eq!(fqn.get_value(), None);
}
#[test]
fn test_parse_namespace_only() {
let fqn = AttributeFqn::parse("https://example.com").unwrap();
assert_eq!(fqn.get_namespace(), "example.com");
assert_eq!(fqn.get_name(), "");
assert_eq!(fqn.get_value(), None);
}
#[test]
fn test_reject_http() {
let result = AttributeFqn::parse("http://example.com/attr/test/value/val");
assert!(matches!(result, Err(FqnError::NotHttps { .. })));
}
#[test]
fn test_missing_attr_structure() {
let result = AttributeFqn::parse("https://example.com/test/value");
assert!(matches!(result, Err(FqnError::MissingAttrStructure { .. })));
}
#[test]
fn test_case_insensitive_namespace() {
let fqn1 = AttributeFqn::parse("https://EXAMPLE.COM/attr/test").unwrap();
let fqn2 = AttributeFqn::parse("https://example.com/attr/test").unwrap();
assert_eq!(fqn1.get_namespace(), fqn2.get_namespace());
}
#[test]
fn test_percent_encoding() {
let fqn = AttributeFqn::with_value("example.com", "test name", "test value");
let url = fqn.to_url();
assert!(url.contains("test%20name"));
assert!(url.contains("test%20value"));
}
#[test]
fn test_namespace_registry() {
let mut registry = NamespaceRegistry::new();
registry.register("example.com");
registry.register("test.org");
let fqn = AttributeFqn::parse("https://example.com/attr/test").unwrap();
assert!(fqn.validate_namespace(®istry).is_ok());
let fqn2 = AttributeFqn::parse("https://unknown.com/attr/test").unwrap();
assert!(fqn2.validate_namespace(®istry).is_err());
}
#[test]
fn test_to_identifier() {
let fqn = AttributeFqn::parse("https://example.com/attr/classification").unwrap();
let id = fqn.to_identifier();
assert_eq!(id.as_string(), "example.com:classification");
}
}