Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change DIDUrl::join to borrow self #871

Merged
merged 4 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bindings/wasm/docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ Sets the `query` component of the `DIDUrl`.
<a name="DIDUrl+join"></a>

### didUrl.join(segment) ⇒ [<code>DIDUrl</code>](#DIDUrl)
Append a string representing a path, query, and/or fragment to this `DIDUrl`.
Append a string representing a path, query, and/or fragment, returning a new `DIDUrl`.

Must begin with a valid delimiter character: '/', '?', '#'. Overwrites the existing URL
segment and any following segments in order of path, query, then fragment.
Expand Down
4 changes: 2 additions & 2 deletions bindings/wasm/src/did/wasm_did_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl WasmDIDUrl {
self.0.set_query(value.as_deref()).wasm_result()
}

/// Append a string representing a path, query, and/or fragment to this `DIDUrl`.
/// Append a string representing a path, query, and/or fragment, returning a new `DIDUrl`.
///
/// Must begin with a valid delimiter character: '/', '?', '#'. Overwrites the existing URL
/// segment and any following segments in order of path, query, then fragment.
Expand All @@ -79,7 +79,7 @@ impl WasmDIDUrl {
/// - joining a query will clear the fragment.
/// - joining a fragment will only overwrite the fragment.
#[wasm_bindgen]
pub fn join(self, segment: &str) -> Result<WasmDIDUrl> {
pub fn join(&self, segment: &str) -> Result<WasmDIDUrl> {
self.0.join(segment).map(WasmDIDUrl::from).wasm_result()
}

Expand Down
1 change: 0 additions & 1 deletion identity-did/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ repository = "https://github.com/iotaledger/identity.rs"
description = "An implementation of the Decentralized Identifiers standard."

[dependencies]
async-trait = { version = "0.1", default-features = false }
did_url = { version = "0.1", default-features = false, features = ["std", "serde"] }
form_urlencoded = { version = "1.0.1", default-features = false }
identity-core = { version = "=0.5.0", path = "../identity-core" }
Expand Down
22 changes: 11 additions & 11 deletions identity-did/src/did/did_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ where
self.url.query_pairs()
}

/// Append a string representing a `path`, `query`, and/or `fragment` to this [`DIDUrl`].
/// Append a string representing a `path`, `query`, and/or `fragment`, returning a new [`DIDUrl`].
///
/// Must begin with a valid delimiter character: '/', '?', '#'. Overwrites the existing URL
/// segment and any following segments in order of path, query, then fragment.
Expand All @@ -393,7 +393,7 @@ where
/// - joining a path will overwrite the path and clear the query and fragment.
/// - joining a query will overwrite the query and clear the fragment.
/// - joining a fragment will only overwrite the fragment.
pub fn join(self, segment: impl AsRef<str>) -> Result<Self, DIDError> {
pub fn join(&self, segment: impl AsRef<str>) -> Result<Self, DIDError> {
let segment: &str = segment.as_ref();

// Accept only a relative path, query, or fragment to reject altering the method id segment.
Expand Down Expand Up @@ -662,13 +662,13 @@ mod tests {
#[test]
fn test_join_valid() {
let did_url = CoreDIDUrl::parse("did:example:1234567890").unwrap();
assert_eq!(did_url.clone().join("/path").unwrap().to_string(), "did:example:1234567890/path");
assert_eq!(did_url.clone().join("?query").unwrap().to_string(), "did:example:1234567890?query");
assert_eq!(did_url.clone().join("#fragment").unwrap().to_string(), "did:example:1234567890#fragment");
assert_eq!(did_url.join("/path").unwrap().to_string(), "did:example:1234567890/path");
assert_eq!(did_url.join("?query").unwrap().to_string(), "did:example:1234567890?query");
assert_eq!(did_url.join("#fragment").unwrap().to_string(), "did:example:1234567890#fragment");

assert_eq!(did_url.clone().join("/path?query").unwrap().to_string(), "did:example:1234567890/path?query");
assert_eq!(did_url.clone().join("/path#fragment").unwrap().to_string(), "did:example:1234567890/path#fragment");
assert_eq!(did_url.clone().join("?query#fragment").unwrap().to_string(), "did:example:1234567890?query#fragment");
assert_eq!(did_url.join("/path?query").unwrap().to_string(), "did:example:1234567890/path?query");
assert_eq!(did_url.join("/path#fragment").unwrap().to_string(), "did:example:1234567890/path#fragment");
assert_eq!(did_url.join("?query#fragment").unwrap().to_string(), "did:example:1234567890?query#fragment");

let did_url = did_url.join("/path?query#fragment").unwrap();
assert_eq!(did_url.to_string(), "did:example:1234567890/path?query#fragment");
Expand All @@ -684,9 +684,9 @@ mod tests {
assert!(CoreDIDUrl::parse("did:example:1234567890#invalid{fragment}").is_err());

let did_url = CoreDIDUrl::parse("did:example:1234567890").unwrap();
assert!(did_url.clone().join("noleadingdelimiter").is_err());
assert!(did_url.clone().join("/invalid{path}").is_err());
assert!(did_url.clone().join("?invalid{query}").is_err());
assert!(did_url.join("noleadingdelimiter").is_err());
assert!(did_url.join("/invalid{path}").is_err());
assert!(did_url.join("?invalid{query}").is_err());
assert!(did_url.join("#invalid{fragment}").is_err());
}

Expand Down