Skip to content

Commit

Permalink
More must_use attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
yescallop committed Apr 19, 2024
1 parent 7e51665 commit 6624098
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Authority<T> {
/// assert_eq!(authority.as_str(), "user@[fe80::abcd]:6780");
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
#[must_use]
pub fn as_str(&'i self) -> &'o str {
self.uri.slice(self.start(), self.end())
}
Expand All @@ -153,6 +154,7 @@ impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Authority<T> {
/// assert_eq!(authority.userinfo().unwrap(), "user");
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
#[must_use]
pub fn userinfo(&'i self) -> Option<&'o EStr<Userinfo>> {
let (start, host_start) = (self.start(), self.host_bounds().0);
(start != host_start).then(|| self.uri.eslice(start, host_start - 1))
Expand All @@ -174,6 +176,7 @@ impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Authority<T> {
/// assert_eq!(authority.host(), "[::1]");
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
#[must_use]
pub fn host(&'i self) -> &'o str {
let (start, end) = self.host_bounds();
self.uri.slice(start, end)
Expand Down Expand Up @@ -208,6 +211,7 @@ impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Authority<T> {
///
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
#[must_use]
pub fn host_parsed(&'i self) -> Host<'o> {
match self.meta().host_meta {
#[cfg(feature = "net")]
Expand Down Expand Up @@ -255,6 +259,7 @@ impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Authority<T> {
/// assert_eq!(authority.port(), Some("66666"));
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
#[must_use]
pub fn port(&'i self) -> Option<&'o str> {
let (host_end, end) = (self.host_bounds().1, self.end());
(host_end != end).then(|| self.uri.slice(host_end + 1, end))
Expand Down
3 changes: 2 additions & 1 deletion src/encoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ impl<E: Encoder> EStr<E> {
///
/// Panics at compile time if `E` is not a [sub-encoder](Encoder#sub-encoders) of `SuperE`.
#[cfg(fluent_uri_unstable)]
#[must_use]
pub fn upcast<SuperE: Encoder>(&self) -> &EStr<SuperE> {
let _ = Assert::<E, SuperE>::LEFT_IS_SUB_ENCODER_OF_RIGHT;
let () = Assert::<E, SuperE>::LEFT_IS_SUB_ENCODER_OF_RIGHT;
EStr::new_validated(self.as_str())
}

Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ impl<I: ToUri> ParseError<I> {
/// Recovers the input that was attempted to parse into a [`Uri`].
///
/// [`Uri`]: crate::Uri
#[must_use]
pub fn into_input(self) -> I {
self.input
}

/// Returns the error with input erased.
#[must_use]
pub fn plain(&self) -> ParseError {
ParseError {
index: self.index,
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ impl<T: Bos<str>> Uri<T> {

impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Uri<T> {
/// Returns the URI reference as a string slice.
#[must_use]
pub fn as_str(&'i self) -> &'o str {
self.val.borrow_or_share()
}
Expand All @@ -233,6 +234,7 @@ impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Uri<T> {
/// Returns the [scheme] component.
///
/// [scheme]: https://datatracker.ietf.org/doc/html/rfc3986/#section-3.1
#[must_use]
pub fn scheme(&'i self) -> Option<&'o Scheme> {
self.scheme_end
.map(|i| Scheme::new_validated(self.slice(0, i.get())))
Expand All @@ -241,6 +243,7 @@ impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Uri<T> {
/// Returns the [authority] component.
///
/// [authority]: https://datatracker.ietf.org/doc/html/rfc3986/#section-3.2
#[must_use]
pub fn authority(&self) -> Option<&Authority<T>> {
if self.auth_meta.is_some() {
Some(Authority::new(self))
Expand All @@ -255,13 +258,15 @@ impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Uri<T> {
///
/// [path]: https://datatracker.ietf.org/doc/html/rfc3986/#section-3.3
/// [extension methods]: EStr#impl-EStr<Path>
#[must_use]
pub fn path(&'i self) -> &'o EStr<Path> {
self.eslice(self.path_bounds.0, self.path_bounds.1)
}

/// Returns the [query] component.
///
/// [query]: https://datatracker.ietf.org/doc/html/rfc3986/#section-3.4
#[must_use]
pub fn query(&'i self) -> Option<&'o EStr<Query>> {
self.query_end
.map(|i| self.eslice(self.path_bounds.1 + 1, i.get()))
Expand All @@ -275,6 +280,7 @@ impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Uri<T> {
/// Returns the [fragment] component.
///
/// [fragment]: https://datatracker.ietf.org/doc/html/rfc3986/#section-3.5
#[must_use]
pub fn fragment(&'i self) -> Option<&'o EStr<Fragment>> {
self.fragment_start().map(|i| self.eslice(i, self.len()))
}
Expand All @@ -298,6 +304,7 @@ impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Uri<T> {
/// assert!(!uri.is_relative_reference());
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
#[must_use]
pub fn is_relative_reference(&self) -> bool {
self.scheme_end.is_none()
}
Expand All @@ -323,6 +330,7 @@ impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Uri<T> {
/// assert!(!uri.is_absolute_uri());
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
#[must_use]
pub fn is_absolute_uri(&self) -> bool {
self.scheme_end.is_some() && self.fragment_start().is_none()
}
Expand Down Expand Up @@ -409,6 +417,7 @@ impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Uri<T> {
/// assert_eq!(uri.normalize(), "example://a/b/c/%7Bfoo%7D");
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
#[must_use]
pub fn normalize(&self) -> Uri<String> {
normalizer::normalize(self.as_ref())
}
Expand Down

0 comments on commit 6624098

Please sign in to comment.