1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use iri_rs::{IriRef, IriRefBuf};
/// Error raised when a string is not a compact IRI.
pub struct InvalidCompactIri<T>(pub T);
#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
/// Compact IRI: a prefix and a suffix separated by a colon.
// `repr(transparent)` guarantees the layout assumed by the `&str` transmute
// in `new_unchecked`.
#[repr(transparent)]
pub struct CompactIri(str);
impl CompactIri {
/// Borrows the given string as a compact IRI, checking that it is one.
///
/// # Errors
///
/// Returns an error when the input is not valid for this type.
pub fn new(s: &str) -> Result<&Self, InvalidCompactIri<&str>> {
match s.split_once(':') {
Some((prefix, suffix)) if prefix != "_" && !suffix.starts_with("//") => match IriRef::parse(s) {
Ok(_) => Ok(unsafe { Self::new_unchecked(s) }),
Err(_) => Err(InvalidCompactIri(s)),
},
_ => Err(InvalidCompactIri(s)),
}
}
/// Creates a new compact IRI without parsing it.
///
/// # Safety
///
/// `s` must be a valid compact IRI: it must contain a `:`, the segment
/// before `:` must not equal `_`, the segment after `:` must not start with
/// `//`, and `s` as a whole must parse as an `IriRef`.
#[must_use]
pub unsafe fn new_unchecked(s: &str) -> &Self {
// SAFETY: `Self` is `repr(transparent)` over `str`, so the pointee
// layout is identical; the caller guarantees `s` is a valid compact IRI.
unsafe { &*(std::ptr::from_ref::<str>(s) as *const Self) }
}
/// Returns this value as a string slice.
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
/// Clones this `CompactIri` into an owned one.
#[must_use]
pub fn to_owned(&self) -> CompactIriBuf {
CompactIriBuf(self.0.to_owned())
}
/// Returns the part before the colon, which names a term to expand.
#[must_use]
pub fn prefix(&self) -> &str {
// SAFETY: a `CompactIri` always contains a `:` (enforced by `new` /
// `new_unchecked`).
let i = unsafe { self.find(':').unwrap_unchecked() };
&self[0..i]
}
/// Returns the part after the colon, appended to the expanded prefix.
#[must_use]
pub fn suffix(&self) -> &str {
// SAFETY: see `prefix`.
let i = unsafe { self.find(':').unwrap_unchecked() };
&self[i + 1..]
}
/// Returns this compact IRI as an IRI reference.
///
/// Every compact IRI is also a syntactically valid IRI reference; that is
/// checked when the value is created.
#[must_use]
pub fn as_iri_ref(&self) -> IriRef<&str> {
// SAFETY: validated as an `IriRef` at construction.
unsafe { IriRef::parse(self.as_str()).unwrap_unchecked() }
}
}
impl std::ops::Deref for CompactIri {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}
impl std::borrow::Borrow<str> for CompactIri {
fn borrow(&self) -> &str {
&self.0
}
}
impl AsRef<str> for CompactIri {
fn as_ref(&self) -> &str {
&self.0
}
}
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
/// Owned compact IRI.
pub struct CompactIriBuf(String);
impl CompactIriBuf {
/// Takes ownership of the given string, checking that it is a compact IRI.
///
/// # Errors
///
/// Returns an error when the input is not valid for this type.
pub fn new(s: String) -> Result<Self, InvalidCompactIri<String>> {
match CompactIri::new(&s) {
Ok(_) => Ok(unsafe { Self::new_unchecked(s) }),
Err(_) => Err(InvalidCompactIri(s)),
}
}
/// Creates a new compact IRI without parsing it.
///
/// # Safety
///
/// `s` must satisfy the contract of [`CompactIri::new_unchecked`].
#[must_use]
pub unsafe fn new_unchecked(s: String) -> Self {
Self(s)
}
/// Borrows this value as a [`CompactIri`].
#[must_use]
pub fn as_compact_iri(&self) -> &CompactIri {
unsafe { CompactIri::new_unchecked(&self.0) }
}
/// Converts this compact IRI into an owned IRI reference, without
/// reallocating.
#[must_use]
pub fn into_iri_ref(self) -> IriRefBuf {
// SAFETY: validated as an `IriRef` at construction.
unsafe { IriRefBuf::new(self.0).unwrap_unchecked() }
}
/// Unwraps the underlying string.
#[must_use]
pub fn into_string(self) -> String {
self.0
}
}
impl std::borrow::Borrow<CompactIri> for CompactIriBuf {
fn borrow(&self) -> &CompactIri {
self.as_compact_iri()
}
}
impl std::ops::Deref for CompactIriBuf {
type Target = CompactIri;
fn deref(&self) -> &CompactIri {
self.as_compact_iri()
}
}