use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RelationType(String);
impl RelationType {
pub fn new(name: impl Into<String>) -> Self {
Self(name.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn into_string(self) -> String {
self.0
}
}
impl fmt::Display for RelationType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<&str> for RelationType {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
impl From<String> for RelationType {
fn from(s: String) -> Self {
Self(s)
}
}
impl AsRef<str> for RelationType {
fn as_ref(&self) -> &str {
&self.0
}
}
impl std::borrow::Borrow<str> for RelationType {
fn borrow(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Relation {
pub relation_type: RelationType,
pub confidence: Option<f32>,
pub source: Option<String>,
#[serde(default)]
pub properties: std::collections::HashMap<String, serde_json::Value>,
}
impl Relation {
pub fn new(relation_type: impl Into<RelationType>) -> Self {
Self {
relation_type: relation_type.into(),
confidence: None,
source: None,
properties: std::collections::HashMap::new(),
}
}
pub fn with_confidence(mut self, confidence: f32) -> Self {
self.confidence = Some(confidence.clamp(0.0, 1.0));
self
}
pub fn with_source(mut self, source: impl Into<String>) -> Self {
self.source = Some(source.into());
self
}
}
impl fmt::Display for Relation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.relation_type)
}
}