From b56b992910ad5c066d462baddbd598abed1a93c2 Mon Sep 17 00:00:00 2001 From: Hamir Mahal Date: Sat, 7 Dec 2024 14:52:44 -0800 Subject: [PATCH 1/2] style: simplify some statements for readability --- url/src/lib.rs | 8 ++++---- url/src/origin.rs | 8 ++++---- url/src/parser.rs | 6 +++--- url/tests/unit.rs | 10 +++------- url/tests/wpt.rs | 50 +++++++++++++++++++---------------------------- 5 files changed, 34 insertions(+), 48 deletions(-) diff --git a/url/src/lib.rs b/url/src/lib.rs index e015acce7..5706f7f68 100644 --- a/url/src/lib.rs +++ b/url/src/lib.rs @@ -1904,7 +1904,7 @@ impl Url { (_, Some(new)) => { let path_and_after = self.slice(self.path_start..).to_owned(); self.serialization.truncate(self.host_end as usize); - write!(&mut self.serialization, ":{}", new).unwrap(); + write!(&mut self.serialization, ":{new}").unwrap(); let old_path_start = self.path_start; let new_path_start = to_u32(self.serialization.len()).unwrap(); self.path_start = new_path_start; @@ -2087,14 +2087,14 @@ impl Url { self.username_end += 2; self.host_start += 2; } - write!(&mut self.serialization, "{}", host).unwrap(); + write!(&mut self.serialization, "{host}").unwrap(); self.host_end = to_u32(self.serialization.len()).unwrap(); self.host = host.into(); if let Some(new_port) = opt_new_port { self.port = new_port; if let Some(port) = new_port { - write!(&mut self.serialization, ":{}", port).unwrap(); + write!(&mut self.serialization, ":{port}").unwrap(); } } let new_suffix_pos = to_u32(self.serialization.len()).unwrap(); @@ -2990,7 +2990,7 @@ fn path_to_file_url_segments_windows( } Prefix::UNC(server, share) | Prefix::VerbatimUNC(server, share) => { let host = Host::parse(server.to_str().ok_or(())?).map_err(|_| ())?; - write!(serialization, "{}", host).unwrap(); + write!(serialization, "{host}").unwrap(); host_end = to_u32(serialization.len()).unwrap(); host_internal = host.into(); serialization.push('/'); diff --git a/url/src/origin.rs b/url/src/origin.rs index a039f4529..804b6447a 100644 --- a/url/src/origin.rs +++ b/url/src/origin.rs @@ -80,9 +80,9 @@ impl Origin { Origin::Opaque(_) => "null".to_owned(), Origin::Tuple(ref scheme, ref host, port) => { if default_port(scheme) == Some(port) { - format!("{}://{}", scheme, host) + format!("{scheme}://{host}") } else { - format!("{}://{}:{}", scheme, host, port) + format!("{scheme}://{host}:{port}") } } } @@ -101,9 +101,9 @@ impl Origin { _ => host.clone(), }; if default_port(scheme) == Some(port) { - format!("{}://{}", scheme, host) + format!("{scheme}://{host}") } else { - format!("{}://{}:{}", scheme, host, port) + format!("{scheme}://{host}:{port}") } } } diff --git a/url/src/parser.rs b/url/src/parser.rs index e26b50281..0f7f519da 100644 --- a/url/src/parser.rs +++ b/url/src/parser.rs @@ -950,7 +950,7 @@ impl<'a> Parser<'a> { scheme_type: SchemeType, ) -> ParseResult<(u32, HostInternal, Option, Input<'i>)> { let (host, remaining) = Parser::parse_host(input, scheme_type)?; - write!(&mut self.serialization, "{}", host).unwrap(); + write!(&mut self.serialization, "{host}").unwrap(); let host_end = to_u32(self.serialization.len())?; if let Host::Domain(h) = &host { if h.is_empty() { @@ -971,7 +971,7 @@ impl<'a> Parser<'a> { (None, remaining) }; if let Some(port) = port { - write!(&mut self.serialization, ":{}", port).unwrap() + write!(&mut self.serialization, ":{port}").unwrap() } Ok((host_end, host.into(), port, remaining)) } @@ -1058,7 +1058,7 @@ impl<'a> Parser<'a> { HostInternal::None } host => { - write!(&mut self.serialization, "{}", host).unwrap(); + write!(&mut self.serialization, "{host}").unwrap(); has_host = true; host.into() } diff --git a/url/tests/unit.rs b/url/tests/unit.rs index b35966103..5d7c4507b 100644 --- a/url/tests/unit.rs +++ b/url/tests/unit.rs @@ -1239,16 +1239,12 @@ fn test_make_relative() { let make_relative = base_uri.make_relative(&relative_uri).unwrap(); assert_eq!( make_relative, *relative, - "base: {}, uri: {}, relative: {}", - base, uri, relative + "base: {base}, uri: {uri}, relative: {relative}" ); assert_eq!( base_uri.join(relative).unwrap().as_str(), *uri, - "base: {}, uri: {}, relative: {}", - base, - uri, - relative + "base: {base}, uri: {uri}, relative: {relative}" ); } @@ -1263,7 +1259,7 @@ fn test_make_relative() { let base_uri = url::Url::parse(base).unwrap(); let relative_uri = url::Url::parse(uri).unwrap(); let make_relative = base_uri.make_relative(&relative_uri); - assert_eq!(make_relative, None, "base: {}, uri: {}", base, uri); + assert_eq!(make_relative, None, "base: {base}, uri: {uri}"); } } diff --git a/url/tests/wpt.rs b/url/tests/wpt.rs index e0199a8c2..db1b6608a 100644 --- a/url/tests/wpt.rs +++ b/url/tests/wpt.rs @@ -171,7 +171,7 @@ fn main() { if should_skip(&name, filter.as_deref()) { continue; } - print!("{} ... ", name); + print!("{name} ... "); let res = run_url_test(url_test); report(name, res, &mut errors, &mut expected_failures); @@ -189,7 +189,7 @@ fn main() { continue; } - print!("{} ... ", name); + print!("{name} ... "); let res = run_setter_test(&kind, test); report(name, res, &mut errors, &mut expected_failures); @@ -205,8 +205,8 @@ fn main() { println!(); for (name, err) in errors { - println!(" name: {}", name); - println!(" err: {}", err); + println!(" name: {name}"); + println!(" err: {err}"); println!(); } @@ -223,7 +223,7 @@ fn main() { println!(); for name in expected_failures { - println!(" {}", name); + println!(" {name}"); } println!(); @@ -279,7 +279,7 @@ fn run_url_test( let base = match base { Some(base) => { let base = - Url::parse(&base).map_err(|e| format!("errored while parsing base: {}", e))?; + Url::parse(&base).map_err(|e| format!("errored while parsing base: {e}"))?; Some(base) } None => None, @@ -288,7 +288,7 @@ fn run_url_test( let res = Url::options() .base_url(base.as_ref()) .parse(&input) - .map_err(|e| format!("errored while parsing input: {}", e)); + .map_err(|e| format!("errored while parsing input: {e}")); match result { UrlTestResult::Ok(ok) => check_url_ok(res, ok), @@ -307,7 +307,7 @@ fn check_url_ok(res: Result, ok: UrlTestOk) -> Result<(), String> { let url = match res { Ok(url) => url, Err(err) => { - return Err(format!("expected success, but errored: {:?}", err)); + return Err(format!("expected success, but errored: {err:?}")); } }; @@ -390,7 +390,7 @@ fn run_setter_test( expected, }: SetterTest, ) -> Result<(), String> { - let mut url = Url::parse(&href).map_err(|e| format!("errored while parsing href: {}", e))?; + let mut url = Url::parse(&href).map_err(|e| format!("errored while parsing href: {e}"))?; match kind { "protocol" => { @@ -415,7 +415,7 @@ fn run_setter_test( "search" => url::quirks::set_search(&mut url, &new_value), "hash" => url::quirks::set_hash(&mut url, &new_value), _ => { - return Err(format!("unknown setter kind: {:?}", kind)); + return Err(format!("unknown setter kind: {kind:?}")); } } @@ -423,8 +423,7 @@ fn run_setter_test( let href = url::quirks::href(&url); if href != expected_href { return Err(format!( - "expected href {:?}, but got {:?}", - expected_href, href + "expected href {expected_href:?}, but got {href:?}" )); } } @@ -433,8 +432,7 @@ fn run_setter_test( let protocol = url::quirks::protocol(&url); if protocol != expected_protocol { return Err(format!( - "expected protocol {:?}, but got {:?}", - expected_protocol, protocol + "expected protocol {expected_protocol:?}, but got {protocol:?}" )); } } @@ -443,8 +441,7 @@ fn run_setter_test( let username = url::quirks::username(&url); if username != expected_username { return Err(format!( - "expected username {:?}, but got {:?}", - expected_username, username + "expected username {expected_username:?}, but got {username:?}" )); } } @@ -453,8 +450,7 @@ fn run_setter_test( let password = url::quirks::password(&url); if password != expected_password { return Err(format!( - "expected password {:?}, but got {:?}", - expected_password, password + "expected password {expected_password:?}, but got {password:?}" )); } } @@ -463,8 +459,7 @@ fn run_setter_test( let host = url::quirks::host(&url); if host != expected_host { return Err(format!( - "expected host {:?}, but got {:?}", - expected_host, host + "expected host {expected_host:?}, but got {host:?}" )); } } @@ -473,8 +468,7 @@ fn run_setter_test( let hostname = url::quirks::hostname(&url); if hostname != expected_hostname { return Err(format!( - "expected hostname {:?}, but got {:?}", - expected_hostname, hostname + "expected hostname {expected_hostname:?}, but got {hostname:?}" )); } } @@ -483,8 +477,7 @@ fn run_setter_test( let port = url::quirks::port(&url); if port != expected_port { return Err(format!( - "expected port {:?}, but got {:?}", - expected_port, port + "expected port {expected_port:?}, but got {port:?}" )); } } @@ -493,8 +486,7 @@ fn run_setter_test( let pathname = url::quirks::pathname(&url); if pathname != expected_pathname { return Err(format!( - "expected pathname {:?}, but got {:?}", - expected_pathname, pathname + "expected pathname {expected_pathname:?}, but got {pathname:?}" )); } } @@ -503,8 +495,7 @@ fn run_setter_test( let search = url::quirks::search(&url); if search != expected_search { return Err(format!( - "expected search {:?}, but got {:?}", - expected_search, search + "expected search {expected_search:?}, but got {search:?}" )); } } @@ -513,8 +504,7 @@ fn run_setter_test( let hash = url::quirks::hash(&url); if hash != expected_hash { return Err(format!( - "expected hash {:?}, but got {:?}", - expected_hash, hash + "expected hash {expected_hash:?}, but got {hash:?}" )); } } From d8d6b7a2a99f783a7b6aafb5d4f180e4ee58b9dc Mon Sep 17 00:00:00 2001 From: Hamir Mahal Date: Sat, 7 Dec 2024 14:55:39 -0800 Subject: [PATCH 2/2] fix: formatting in `url/tests/wpt.rs` --- url/tests/wpt.rs | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/url/tests/wpt.rs b/url/tests/wpt.rs index db1b6608a..cd5fc92dc 100644 --- a/url/tests/wpt.rs +++ b/url/tests/wpt.rs @@ -278,8 +278,7 @@ fn run_url_test( ) -> Result<(), String> { let base = match base { Some(base) => { - let base = - Url::parse(&base).map_err(|e| format!("errored while parsing base: {e}"))?; + let base = Url::parse(&base).map_err(|e| format!("errored while parsing base: {e}"))?; Some(base) } None => None, @@ -422,9 +421,7 @@ fn run_setter_test( if let Some(expected_href) = expected.href { let href = url::quirks::href(&url); if href != expected_href { - return Err(format!( - "expected href {expected_href:?}, but got {href:?}" - )); + return Err(format!("expected href {expected_href:?}, but got {href:?}")); } } @@ -458,9 +455,7 @@ fn run_setter_test( if let Some(expected_host) = expected.host { let host = url::quirks::host(&url); if host != expected_host { - return Err(format!( - "expected host {expected_host:?}, but got {host:?}" - )); + return Err(format!("expected host {expected_host:?}, but got {host:?}")); } } @@ -476,9 +471,7 @@ fn run_setter_test( if let Some(expected_port) = expected.port { let port = url::quirks::port(&url); if port != expected_port { - return Err(format!( - "expected port {expected_port:?}, but got {port:?}" - )); + return Err(format!("expected port {expected_port:?}, but got {port:?}")); } } @@ -503,9 +496,7 @@ fn run_setter_test( if let Some(expected_hash) = expected.hash { let hash = url::quirks::hash(&url); if hash != expected_hash { - return Err(format!( - "expected hash {expected_hash:?}, but got {hash:?}" - )); + return Err(format!("expected hash {expected_hash:?}, but got {hash:?}")); } }