Skip to content

Commit 37a1829

Browse files
committed
Add {Uri, UriRef, Iri, IriRef}::strip_fragment
1 parent dd16656 commit 37a1829

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/ri.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,24 @@ macro_rules! ri_maybe_ref {
568568
self.as_ref().has_fragment()
569569
}
570570

571+
#[doc = concat!("Returns a slice of this ", $name)]
572+
/// with the fragment component removed.
573+
///
574+
/// # Examples
575+
///
576+
/// ```
577+
#[doc = concat!("use fluent_uri::", $ty, ";")]
578+
///
579+
#[doc = concat!("let ", $var, " = ", $ty, "::parse(\"http://example.com/#fragment\")?;")]
580+
#[doc = concat!("assert_eq!(", $var, ".strip_fragment(), \"http://example.com/\");")]
581+
/// # Ok::<_, fluent_uri::error::ParseError>(())
582+
/// ```
583+
#[must_use]
584+
pub fn strip_fragment(&self) -> $Ty<&str> {
585+
// Altering only the fragment does not change the metadata.
586+
RiRef::new(self.as_ref().strip_fragment(), self.meta)
587+
}
588+
571589
#[doc = concat!("Creates a new ", $name)]
572590
/// by replacing the fragment component of `self` with the given one.
573591
///
@@ -585,10 +603,7 @@ macro_rules! ri_maybe_ref {
585603
/// );
586604
///
587605
#[doc = concat!("let ", $var, " = ", $ty, "::parse(\"http://example.com/#fragment\")?;")]
588-
/// assert_eq!(
589-
#[doc = concat!(" ", $var, ".with_fragment(None),")]
590-
/// "http://example.com/"
591-
/// );
606+
#[doc = concat!("assert_eq!(", $var, ".with_fragment(None), \"http://example.com/\");")]
592607
/// # Ok::<_, fluent_uri::error::ParseError>(())
593608
/// ```
594609
#[must_use]
@@ -618,6 +633,7 @@ macro_rules! ri_maybe_ref {
618633
/// # Ok::<_, fluent_uri::error::ParseError>(())
619634
/// ```
620635
pub fn set_fragment(&mut self, opt: Option<&EStr<$FragmentE>>) {
636+
// Altering only the fragment does not change the metadata.
621637
Ref::set_fragment(&mut self.val, &self.meta, opt.map(EStr::as_str))
622638
}
623639
}
@@ -877,8 +893,12 @@ impl<'v, 'm> Ref<'v, 'm> {
877893
}
878894
}
879895

896+
pub fn strip_fragment(self) -> &'v str {
897+
&self.val[..self.meta.query_or_path_end()]
898+
}
899+
880900
pub fn with_fragment(self, opt: Option<&str>) -> String {
881-
let stripped = &self.val[..self.meta.query_or_path_end()];
901+
let stripped = self.strip_fragment();
882902
if let Some(s) = opt {
883903
[stripped, "#", s].concat()
884904
} else {

0 commit comments

Comments
 (0)