1use crate::TopicId;
4use alloc::{format, string::String};
5use asimov_id::{Handle, HandleError};
6use core::{fmt::Display, str::FromStr};
7use rdf_hash::TermHash;
8use rdf_model::HeapTerm;
9
10#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
11pub enum Topic {
12 Handle(Handle),
14}
15
16impl Topic {
17 pub fn id(&self) -> TopicId {
18 let term = HeapTerm::iri(self.to_uri());
19 let term_hash = TermHash::from(term);
20 TopicId::from(*term_hash.as_bytes())
21 }
22
23 pub fn to_uri(&self) -> String {
24 match self {
25 Self::Handle(handle) => handle.to_uri(),
26 }
27 }
28}
29
30impl Display for Topic {
31 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
32 write!(f, "{}", self.to_uri())
33 }
34}
35
36impl FromStr for Topic {
37 type Err = HandleError;
38
39 fn from_str(input: &str) -> Result<Self, Self::Err> {
40 Ok(Topic::Handle(Handle::from_str(input)?))
41 }
42}
43
44impl<T> From<&T> for Topic
45where
46 T: Clone + Into<Self>,
47{
48 fn from(t: &T) -> Self {
49 t.clone().into()
50 }
51}
52
53impl TryFrom<String> for Topic {
54 type Error = HandleError;
55
56 fn try_from(input: String) -> Result<Self, Self::Error> {
57 Self::from_str(&input)
58 }
59}
60
61impl From<&Topic> for String {
62 fn from(input: &Topic) -> Self {
63 input.to_uri()
64 }
65}
66
67impl From<Handle> for Topic {
68 fn from(input: Handle) -> Self {
69 Topic::Handle(input)
70 }
71}