Skip to content

Commit a2e4a65

Browse files
committed
improved documentation and method naming
the documentation of IRI and LangTag has been made clearer, to ellicit the responsibilities of producers vs. consumers. Also, the methods unchecked_map were have been renamed to map_unchecked, which is aligned with new_unchecked.
1 parent 587bf87 commit a2e4a65

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

statement/src/_iri.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
use std::borrow::Cow;
22

3-
/// Wrapper around a [`Cow<str>`] guaranteeing that the underlying text satisfies [RFC3987].
3+
/// Wrapper around a [`Cow<str>`] signaling that it complies with [RFC3987],
4+
/// i.e. it is a valid IRI.
5+
///
6+
/// ## Contract
7+
/// * Consumers of [`Iri`]s can safely assume that the underlying text is a valid IRI.
8+
/// * Producers of [`Iri`]s are responsible for ensuring that constraint.
49
///
510
/// [RFC3987]: https://datatracker.ietf.org/doc/rfc3987/
611
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
712
pub struct Iri<'a>(Cow<'a, str>);
813

914
impl<'a> Iri<'a> {
1015
/// Return a new [`Iri`], assuming the argument is a valid IRI.
16+
///
17+
/// ## Precondition
18+
/// It is the responsibility of the caller to ensure that `txt` is a valid IRI
1119
pub fn new_unchecked(txt: impl Into<Cow<'a, str>>) -> Self {
1220
Iri(txt.into())
1321
}
@@ -17,8 +25,12 @@ impl<'a> Iri<'a> {
1725
self.0
1826
}
1927

20-
/// Apply a function to the inner txt, assuming the result of the function is still a valid IRI.
21-
pub fn unchecked_map(self, mut f: impl FnMut(Cow<'a, str>) -> Cow<'a, str>) -> Self {
28+
/// Apply a function to the inner text, assuming the result is still a valid IRI.
29+
///
30+
/// ## Precondition
31+
/// It is the responsibility of the caller to ensure that `f`
32+
/// produces a valid IRI when its argument is a valid IRI.
33+
pub fn map_unchecked(self, mut f: impl FnMut(Cow<'a, str>) -> Cow<'a, str>) -> Self {
2234
Self(f(self.0))
2335
}
2436

statement/src/_literal/_language_tag.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
use std::borrow::Cow;
22

3-
/// Wrapper around a [`Cow<str>`] guaranteeing that the underlying text satisfies [BCP47].
3+
/// Wrapper around a [`Cow<str>`] signaling that it complies with [BCP47],
4+
/// i.e. it is a valid language tag.
45
///
5-
/// NB: This type checks that the structure of the tag complies with the grammar,
6-
/// but does *not* check that each component is a valid code
7-
/// (i.e. ISO 639 for 2-3 characters language tag, or ISO 15924 for the script)
6+
/// ## Contract
7+
/// * Consumers of [`LangTag`]s can safely assume that the underlying text is a valid IRI.
8+
/// * Producers of [`LangTag`]s are responsible for ensuring that constraint.
9+
///
10+
/// This contract only require that the underlying text complies with the grammar defined by [BCP47].
11+
/// It does not require that each component is a valid code
12+
/// (i.e. ISO 639 for 2-3 characters language tag, or ISO 15924 for the script).
813
///
914
/// [BCP47]: https://datatracker.ietf.org/doc/bcp47/
1015
#[derive(Clone, Debug, Eq, Ord)]
1116
pub struct LangTag<'a>(Cow<'a, str>);
1217

1318
impl<'a> LangTag<'a> {
1419
/// Return a new [`LangTag`], assuming the argument is a valid language tag.
20+
///
21+
/// ## Precondition
22+
/// It is the responsibility of the caller to ensure that `txt` is a valid language tag.
1523
pub fn new_unchecked(txt: impl Into<Cow<'a, str>>) -> Self {
1624
LangTag(txt.into())
1725
}
@@ -21,8 +29,12 @@ impl<'a> LangTag<'a> {
2129
self.0
2230
}
2331

24-
/// Apply a function to the inner txt, assuming the result of the function is still a valid language tag.
25-
pub fn unchecked_map(self, mut f: impl FnMut(Cow<'a, str>) -> Cow<'a, str>) -> Self {
32+
/// Apply a function to the inner text, assuming the result is still a valid language tag.
33+
///
34+
/// ## Precondition
35+
/// It is the responsibility of the caller to ensure that `f`
36+
/// produces a valid language tag when its argument is a valid language tag.
37+
pub fn map_unchecked(self, mut f: impl FnMut(Cow<'a, str>) -> Cow<'a, str>) -> Self {
2638
Self(f(self.0))
2739
}
2840

0 commit comments

Comments
 (0)