Skip to content

Commit 6f11cb6

Browse files
asmellochanced
andauthored
improve Token::from_encoded (#110)
* improve Token::from_encoded * updated changelog --------- Co-authored-by: Chance <[email protected]>
1 parent 29840e7 commit 6f11cb6

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
integration.
2424

2525
### Changed
26+
- `Token::from_encoded` now accepts owned or borrowed strings (any type that
27+
implements `Into<Cow<'_, str>>`).
2628
- Sealed the `Diagnose` trait.
2729
- Implementation of the `Default` trait for `Pointer` now doesn't constrain the lifetime.
2830

src/token.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ impl<'a> Token<'a> {
7474
/// ## Errors
7575
/// Returns `InvalidEncodingError` if the input string is not a valid RFC
7676
/// 6901 (`~` must be followed by `0` or `1`)
77-
pub fn from_encoded(s: &'a str) -> Result<Self, EncodingError> {
77+
pub fn from_encoded(s: impl Into<Cow<'a, str>>) -> Result<Self, EncodingError> {
78+
let inner = s.into();
7879
let mut escaped = false;
79-
for (offset, b) in s.bytes().enumerate() {
80+
for (offset, b) in inner.bytes().enumerate() {
8081
match b {
8182
b'/' => {
8283
return Err(EncodingError {
@@ -102,11 +103,11 @@ impl<'a> Token<'a> {
102103
}
103104
if escaped {
104105
return Err(EncodingError {
105-
offset: s.len(),
106+
offset: inner.len(),
106107
source: InvalidEncoding::Tilde,
107108
});
108109
}
109-
Ok(Self { inner: s.into() })
110+
Ok(Self { inner })
110111
}
111112

112113
/// Constructs a `Token` from an arbitrary string.

0 commit comments

Comments
 (0)