rudof_rdf/rdf_core/term/blank_node.rs
1use std::fmt::{Debug, Display};
2
3/// Represents an owned blank node in RDF.
4///
5/// Blank nodes are anonymous resources in RDF that don't have global identifiers.
6/// They are only meaningful within the scope of a single RDF graph and are used
7/// to represent resources that don't need to be referenced outside of that context.
8pub trait BlankNode: Debug + Display + PartialEq {
9 /// Constructs a new blank node with the given identifier.
10 ///
11 /// # Arguments
12 ///
13 /// * `id` - The identifier for this blank node. Accepts any type convertible to `String`, such as `&str` or `String`.
14 fn new(id: impl Into<String>) -> Self;
15 /// Returns the identifier of this blank node as a string slice.
16 fn id(&self) -> &str;
17}
18
19/// A trait for borrowed references to blank node labels.
20///
21/// This trait is useful for working with blank node identifiers without
22/// taking ownership, allowing zero-copy operations when processing RDF data.
23pub trait BlankNodeRef<'a> {
24 /// Returns a reference to the blank node's label.
25 fn label(&self) -> &'a str;
26}
27
28/// A lightweight borrowed representation of a blank node.
29#[derive(Debug, PartialEq)]
30pub struct ConcreteBlankNode<'a> {
31 s: &'a str,
32}
33
34impl<'a> ConcreteBlankNode<'a> {
35 /// Constructs a `ConcreteBlankNode` from a string slice
36 /// This is a lightweight constructor that simply wraps the provided
37 /// string reference without allocating new memory.
38 ///
39 /// # Arguments
40 ///
41 /// * `s` - A string slice containing the blank node label
42 pub fn from(s: &'a str) -> ConcreteBlankNode<'a> {
43 ConcreteBlankNode { s }
44 }
45}
46
47impl<'a> BlankNodeRef<'a> for ConcreteBlankNode<'a> {
48 /// Returns the blank node's label as a string slice.
49 ///
50 /// The returned reference has the same lifetime `'a` as the
51 /// `ConcreteBlankNode` instance
52 fn label(&self) -> &'a str {
53 self.s
54 }
55}