json_ld_syntax/context/term_definition/
index.rs1use 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