json_ld_syntax/context/term_definition/
index.rs

1use crate::CompactIri;
2use iref::Iri;
3use std::fmt;
4use std::hash::Hash;
5
6#[derive(Clone, PartialOrd, Ord, Debug)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[cfg_attr(feature = "serde", serde(transparent))]
9pub struct Index(String);
10
11impl Index {
12	pub fn as_iri(&self) -> Option<&Iri> {
13		Iri::new(&self.0).ok()
14	}
15
16	pub fn as_compact_iri(&self) -> Option<&CompactIri> {
17		CompactIri::new(&self.0).ok()
18	}
19
20	pub fn as_str(&self) -> &str {
21		&self.0
22	}
23
24	pub fn into_string(self) -> String {
25		self.0
26	}
27}
28
29impl PartialEq for Index {
30	fn eq(&self, other: &Self) -> bool {
31		self.0 == other.0
32	}
33}
34
35impl fmt::Display for Index {
36	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37		self.0.fmt(f)
38	}
39}
40
41impl Eq for Index {}
42
43impl Hash for Index {
44	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
45		self.as_str().hash(state)
46	}
47}
48
49impl From<String> for Index {
50	fn from(s: String) -> Self {
51		Self(s)
52	}
53}
54
55// #[derive(Clone, Copy)]
56// pub struct IndexRef<'a>(&'a str);
57
58// impl<'a> IndexRef<'a> {
59// 	pub fn to_owned(self) -> Index {
60// 		Index(self.0.to_owned())
61// 	}
62
63// 	pub fn as_str(&self) -> &'a str {
64// 		self.0
65// 	}
66// }
67
68// impl<'a> From<&'a Index> for IndexRef<'a> {
69// 	fn from(i: &'a Index) -> Self {
70// 		Self(&i.0)
71// 	}
72// }
73
74// impl<'a> From<IndexRef<'a>> for super::EntryKeyRef<'a> {
75// 	fn from(i: IndexRef<'a>) -> Self {
76// 		i.0.into()
77// 	}
78// }