From c91797f54566e01ddea191c5af11a270eb25401f Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Fri, 3 Nov 2023 11:41:45 -0400 Subject: [PATCH] Add a simple repr. Closes: #2 --- Cargo.lock | 41 +++++++++++++++++++++---------------- Cargo.toml | 4 ++-- src/lib.rs | 8 ++++++-- tests/test_url.py | 52 ++++++++++++++++++++++++++--------------------- 4 files changed, 61 insertions(+), 44 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7ba1f69..e51b532 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,6 +29,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "idna" version = "0.4.0" @@ -41,9 +47,9 @@ dependencies = [ [[package]] name = "indoc" -version = "1.0.9" +version = "2.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa799dd5ed20a7e349f3b4639aa80d74549c81716d9ec4f994c9b5815598306" +checksum = "1e186cfbae8084e513daff4240b4797e342f988cecda4fb6c939150f96315fd8" [[package]] name = "libc" @@ -116,9 +122,9 @@ dependencies = [ [[package]] name = "pyo3" -version = "0.19.2" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e681a6cfdc4adcc93b4d3cf993749a4552018ee0a9b65fc0ccfad74352c72a38" +checksum = "04e8453b658fe480c3e70c8ed4e3d3ec33eb74988bd186561b0cc66b85c3bc4b" dependencies = [ "cfg-if", "indoc", @@ -133,9 +139,9 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.19.2" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "076c73d0bc438f7a4ef6fdd0c3bb4732149136abd952b110ac93e4edb13a6ba5" +checksum = "a96fe70b176a89cff78f2fa7b3c930081e163d5379b4dcdf993e3ae29ca662e5" dependencies = [ "once_cell", "target-lexicon", @@ -143,9 +149,9 @@ dependencies = [ [[package]] name = "pyo3-ffi" -version = "0.19.2" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e53cee42e77ebe256066ba8aa77eff722b3bb91f3419177cf4cd0f304d3284d9" +checksum = "214929900fd25e6604661ed9cf349727c8920d47deff196c4e28165a6ef2a96b" dependencies = [ "libc", "pyo3-build-config", @@ -153,9 +159,9 @@ dependencies = [ [[package]] name = "pyo3-macros" -version = "0.19.2" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfeb4c99597e136528c6dd7d5e3de5434d1ceaf487436a3f03b2d56b6fc9efd1" +checksum = "dac53072f717aa1bfa4db832b39de8c875b7c7af4f4a6fe93cdbf9264cf8383b" dependencies = [ "proc-macro2", "pyo3-macros-backend", @@ -165,10 +171,11 @@ dependencies = [ [[package]] name = "pyo3-macros-backend" -version = "0.19.2" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "947dc12175c254889edc0c02e399476c2f652b4b9ebd123aa655c224de259536" +checksum = "7774b5a8282bd4f25f803b1f0d945120be959a36c72e08e7cd031c792fdfd424" dependencies = [ + "heck", "proc-macro2", "quote", "syn", @@ -206,9 +213,9 @@ checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" [[package]] name = "syn" -version = "1.0.109" +version = "2.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" dependencies = [ "proc-macro2", "quote", @@ -259,9 +266,9 @@ dependencies = [ [[package]] name = "unindent" -version = "0.1.11" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1766d682d402817b5ac4490b3c3002d91dfa0d22812f341609f97b08757359c" +checksum = "c7de7d73e1754487cb58364ee906a499937a0dfabd86bcb980fa99ec8c8fa2ce" [[package]] name = "url" @@ -276,7 +283,7 @@ dependencies = [ [[package]] name = "url-py" -version = "0.7.1" +version = "0.8.0" dependencies = [ "pyo3", "url", diff --git a/Cargo.toml b/Cargo.toml index 7fa0347..30be226 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "url-py" -version = "0.7.1" +version = "0.8.0" edition = "2021" [lib] @@ -11,5 +11,5 @@ crate-type = ["cdylib"] url = "2" [dependencies.pyo3] -version = "0.19.2" +version = "0.20.0" features = ["extension-module"] diff --git a/src/lib.rs b/src/lib.rs index 759d5dd..333d0ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,8 +45,8 @@ fn from_result(input: Result) -> PyResult { #[pymethods] impl UrlPy { - fn __str__(&self) -> &str { - self.inner.as_str() + fn __repr__(&self) -> String { + format!("", self.inner.as_str()) } fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> PyObject { @@ -57,6 +57,10 @@ impl UrlPy { } } + fn __str__(&self) -> &str { + self.inner.as_str() + } + fn __truediv__(&self, other: &str) -> PyResult { // .join()'s behavior depends on whether the URL has a trailing slash or not -- // which is good! But here for division we follow the convention of e.g. yarl that someone diff --git a/tests/test_url.py b/tests/test_url.py index a3abd5c..628e15d 100644 --- a/tests/test_url.py +++ b/tests/test_url.py @@ -48,21 +48,6 @@ def test_join(): assert str(css_url) == "http://servo.github.io/rust-url/main.css" -@pytest.mark.parametrize( - "base_url", - ["http://foo.com/bar", "http://foo.com/bar/"], -) -def test_slash(base_url): - """ - Support the / operator as many Python types have decided to. - - Whether the base URL ends in a slash or not, e.g. yarl.URL adds one, so we - follow that behavior for this (and not for .join()). - """ - joined = URL.parse(base_url) / "baz" - assert str(joined) == "http://foo.com/bar/baz" - - def test_invalid_ipv6_address(): with pytest.raises(url.InvalidIPv6Address): URL.parse("http://[:::1]") @@ -78,14 +63,6 @@ def test_invalid_junk(): URL.parse("https:/12949a;df;;@@@") -def test_eq(): - """ - Support the / operator as many Python types have decided to. - """ - assert URL.parse("http://example.com") == URL.parse("http://example.com") - assert URL.parse("http://foo.com") != URL.parse("http://bar.com") - - def test_parse_with_params(): url = URL.parse_with_params( "https://example.net?dont=clobberme", @@ -101,3 +78,32 @@ def test_make_relative(): url = URL.parse("https://example.net/a/c.png") relative = base.make_relative(url) assert relative == "c.png" + + +@pytest.mark.parametrize( + "base_url", + ["http://foo.com/bar", "http://foo.com/bar/"], +) +def test_slash(base_url): + """ + Support the / operator as many Python types have decided to. + + Whether the base URL ends in a slash or not, e.g. yarl.URL adds one, so we + follow that behavior for this (and not for .join()). + """ + joined = URL.parse(base_url) / "baz" + assert str(joined) == "http://foo.com/bar/baz" + + +def test_str(): + example = "http://example.com/" + assert str(URL.parse(example)) == example + + +def test_repr(): + assert repr(URL.parse("http://example.com")) == "" + + +def test_eq(): + assert URL.parse("http://example.com") == URL.parse("http://example.com") + assert URL.parse("http://foo.com") != URL.parse("http://bar.com")