11use 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 ) ]
1116pub struct LangTag < ' a > ( Cow < ' a , str > ) ;
1217
1318impl < ' 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