use super::ReferenceError;
use core::fmt;
use sha2::Digest;
pub trait IdentitySink: fmt::Debug + Send + Sync {
fn identify(&self, request: &IdentityRequest) -> Result<String, ReferenceError>;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IdentityRequest {
resource_type: String,
parent_type: String,
parent_id: Option<String>,
composition: Option<String>,
mapping: String,
occurrence: Vec<u32>,
unique: Vec<String>,
}
impl IdentityRequest {
#[must_use]
pub fn new(
resource_type: impl Into<String>,
parent_type: impl Into<String>,
mapping: impl Into<String>,
) -> Self {
Self {
resource_type: resource_type.into(),
parent_type: parent_type.into(),
parent_id: None,
composition: None,
mapping: mapping.into(),
occurrence: Vec::new(),
unique: Vec::new(),
}
}
#[must_use]
pub fn with_parent_id(mut self, id: impl Into<String>) -> Self {
self.parent_id = Some(id.into());
self
}
#[must_use]
pub fn with_composition(mut self, uid: impl Into<String>) -> Self {
self.composition = Some(uid.into());
self
}
#[must_use]
pub fn with_occurrence(mut self, occurrence: Vec<u32>) -> Self {
self.occurrence = occurrence;
self
}
#[must_use]
pub fn with_unique(mut self, unique: Vec<String>) -> Self {
self.unique = unique;
self
}
#[must_use]
pub fn resource_type(&self) -> &str {
&self.resource_type
}
#[must_use]
pub fn parent_type(&self) -> &str {
&self.parent_type
}
#[must_use]
pub fn parent_id(&self) -> Option<&str> {
self.parent_id.as_deref()
}
#[must_use]
pub fn composition(&self) -> Option<&str> {
self.composition.as_deref()
}
#[must_use]
pub fn mapping(&self) -> &str {
&self.mapping
}
#[must_use]
pub fn occurrence(&self) -> &[u32] {
&self.occurrence
}
#[must_use]
pub fn unique(&self) -> &[String] {
&self.unique
}
}
pub trait ResourceIdentity: fmt::Debug + Send + Sync {
fn identify(&self, request: &ResourceRequest) -> Result<String, ReferenceError>;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResourceRequest {
resource_type: String,
template: String,
composition: Option<String>,
written: Option<String>,
occurrence: Vec<u32>,
entry_path: Option<String>,
}
impl ResourceRequest {
#[must_use]
pub fn new(resource_type: impl Into<String>, template: impl Into<String>) -> Self {
Self {
resource_type: resource_type.into(),
template: template.into(),
composition: None,
written: None,
occurrence: Vec::new(),
entry_path: None,
}
}
#[must_use]
pub fn with_occurrence(mut self, occurrence: Vec<u32>) -> Self {
self.occurrence = occurrence;
self
}
#[must_use]
pub fn with_entry_path(mut self, path: impl Into<String>) -> Self {
self.entry_path = Some(path.into());
self
}
#[must_use]
pub fn with_composition(mut self, uid: impl Into<String>) -> Self {
self.composition = Some(uid.into());
self
}
#[must_use]
pub fn with_written(mut self, id: impl Into<String>) -> Self {
self.written = Some(id.into());
self
}
#[must_use]
pub fn resource_type(&self) -> &str {
&self.resource_type
}
#[must_use]
pub fn template(&self) -> &str {
&self.template
}
#[must_use]
pub fn composition(&self) -> Option<&str> {
self.composition.as_deref()
}
#[must_use]
pub fn written(&self) -> Option<&str> {
self.written.as_deref()
}
#[must_use]
pub fn entry_path(&self) -> Option<&str> {
self.entry_path.as_deref()
}
#[must_use]
pub fn occurrence(&self) -> &[u32] {
&self.occurrence
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct DerivedIdentity;
impl IdentitySink for DerivedIdentity {
fn identify(&self, request: &IdentityRequest) -> Result<String, ReferenceError> {
let mut hasher = sha2::Sha256::new();
let mut frame = |field: &[u8]| {
hasher.update(u64::try_from(field.len()).unwrap_or(u64::MAX).to_be_bytes());
hasher.update(field);
};
frame(request.resource_type.as_bytes());
frame(request.parent_type.as_bytes());
frame(request.parent_id.as_deref().unwrap_or_default().as_bytes());
frame(
request
.composition
.as_deref()
.unwrap_or_default()
.as_bytes(),
);
frame(request.mapping.as_bytes());
for position in &request.occurrence {
frame(&position.to_be_bytes());
}
frame(b"unique");
for value in &request.unique {
frame(value.as_bytes());
}
let mut id = String::with_capacity(64);
for byte in hasher.finalize().as_slice() {
for nibble in [byte >> 4, byte & 0x0f] {
id.extend(char::from_digit(u32::from(nibble), 16));
}
}
Ok(id)
}
}