Skip to content

Commit bb7955a

Browse files
committed
Fixed testcases for no_std
1 parent db92dd2 commit bb7955a

File tree

7 files changed

+172
-35
lines changed

7 files changed

+172
-35
lines changed

url/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ percent-encoding = { version = "2.2.0", path = "../percent_encoding" }
3434
serde = {version = "1.0", optional = true, features = ["derive"]}
3535

3636
[features]
37-
[features]
38-
default = ["std", "idna"]
37+
default = ["std"]
3938
std = ["idna/std", "alloc"]
4039
alloc = []
41-
default = []
4240
# UNSTABLE FEATURES (requires Rust nightly)
4341
# Enable to use the #[debugger_visualizer] attribute.
4442
debugger_visualizer = []

url/src/host.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use alloc::borrow::ToOwned;
10-
use alloc::string::{String, ToString};
11-
use alloc::vec::Vec;
12-
use core::cmp;
13-
use core::fmt::{self, Formatter};
9+
use alloc::{
10+
borrow::ToOwned,
11+
string::{String, ToString},
12+
vec::Vec,
13+
};
14+
use core::{
15+
cmp,
16+
fmt::{self, Formatter},
17+
};
1418
use std::net::{Ipv4Addr, Ipv6Addr};
1519

1620
use percent_encoding::{percent_decode, utf8_percent_encode, CONTROLS};

url/src/lib.rs

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ url = { version = "2", features = ["serde"] }
130130

131131
pub use form_urlencoded;
132132

133+
// For forwards compatibility
134+
#[cfg(feature = "std")]
135+
extern crate std;
136+
137+
extern crate alloc;
138+
139+
#[cfg(not(feature = "alloc"))]
140+
compile_error!("the `alloc` feature must be enabled");
141+
133142
#[cfg(feature = "serde")]
134143
extern crate serde;
135144

@@ -1317,9 +1326,23 @@ impl Url {
13171326
///
13181327
/// ```
13191328
/// use url::Url;
1320-
/// # use std::error::Error;
13211329
///
1322-
/// # fn run() -> Result<(), Box<dyn Error>> {
1330+
/// # use url::ParseError;
1331+
/// # #[derive(Debug)]
1332+
/// # /// A simple wrapper error struct for `no_std` support
1333+
/// # struct TestError;
1334+
/// # impl From<ParseError> for TestError {
1335+
/// # fn from(value: ParseError) -> Self {
1336+
/// # TestError {}
1337+
/// # }
1338+
/// # }
1339+
/// # impl From<&str> for TestError {
1340+
/// # fn from(value: &str) -> Self {
1341+
/// # TestError {}
1342+
/// # }
1343+
/// # }
1344+
///
1345+
/// # fn run() -> Result<(), TestError> {
13231346
/// let url = Url::parse("https://example.com/foo/bar")?;
13241347
/// let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?;
13251348
/// assert_eq!(path_segments.next(), Some("foo"));
@@ -1724,9 +1747,22 @@ impl Url {
17241747
///
17251748
/// ```
17261749
/// use url::Url;
1727-
/// # use std::error::Error;
1750+
/// # use url::ParseError;
1751+
/// # #[derive(Debug)]
1752+
/// # /// A simple wrapper error struct for `no_std` support
1753+
/// # struct TestError;
1754+
/// # impl From<ParseError> for TestError {
1755+
/// # fn from(value: ParseError) -> Self {
1756+
/// # TestError {}
1757+
/// # }
1758+
/// # }
1759+
/// # impl From<&str> for TestError {
1760+
/// # fn from(value: &str) -> Self {
1761+
/// # TestError {}
1762+
/// # }
1763+
/// # }
17281764
///
1729-
/// # fn run() -> Result<(), Box<dyn Error>> {
1765+
/// # fn run() -> Result<(), TestError> {
17301766
/// let mut url = Url::parse("ssh://example.net:2048/")?;
17311767
///
17321768
/// url.set_port(Some(4096)).map_err(|_| "cannot be base")?;
@@ -1743,9 +1779,22 @@ impl Url {
17431779
///
17441780
/// ```rust
17451781
/// use url::Url;
1746-
/// # use std::error::Error;
1782+
/// # use url::ParseError;
1783+
/// # #[derive(Debug)]
1784+
/// # /// A simple wrapper error struct for `no_std` support
1785+
/// # struct TestError;
1786+
/// # impl From<ParseError> for TestError {
1787+
/// # fn from(value: ParseError) -> Self {
1788+
/// # TestError {}
1789+
/// # }
1790+
/// # }
1791+
/// # impl From<&str> for TestError {
1792+
/// # fn from(value: &str) -> Self {
1793+
/// # TestError {}
1794+
/// # }
1795+
/// # }
17471796
///
1748-
/// # fn run() -> Result<(), Box<dyn Error>> {
1797+
/// # fn run() -> Result<(), TestError> {
17491798
/// let mut url = Url::parse("https://example.org/")?;
17501799
///
17511800
/// url.set_port(Some(443)).map_err(|_| "cannot be base")?;
@@ -2428,7 +2477,10 @@ impl Url {
24282477
/// ```
24292478
///
24302479
/// This method is only available if the `std` Cargo feature is enabled.
2431-
#[cfg(all(feature = "std", any(unix, windows, target_os = "redox", target_os = "wasi")))]
2480+
#[cfg(all(
2481+
feature = "std",
2482+
any(unix, windows, target_os = "redox", target_os = "wasi")
2483+
))]
24322484
#[allow(clippy::result_unit_err)]
24332485
pub fn from_file_path<P: AsRef<Path>>(path: P) -> Result<Url, ()> {
24342486
let mut serialization = "file://".to_owned();
@@ -2467,7 +2519,10 @@ impl Url {
24672519
/// and usually does not include them (e.g. in `Path::parent()`).
24682520
///
24692521
/// This method is only available if the `std` Cargo feature is enabled.
2470-
#[cfg(all(feature = "std", any(unix, windows, target_os = "redox", target_os = "wasi")))]
2522+
#[cfg(all(
2523+
feature = "std",
2524+
any(unix, windows, target_os = "redox", target_os = "wasi")
2525+
))]
24712526
#[allow(clippy::result_unit_err)]
24722527
pub fn from_directory_path<P: AsRef<Path>>(path: P) -> Result<Url, ()> {
24732528
let mut url = Url::from_file_path(path)?;
@@ -2586,7 +2641,10 @@ impl Url {
25862641
///
25872642
/// This method is only available if the `std` Cargo feature is enabled.
25882643
#[inline]
2589-
#[cfg(all(feature = "std", any(unix, windows, target_os = "redox", target_os = "wasi")))]
2644+
#[cfg(all(
2645+
feature = "std",
2646+
any(unix, windows, target_os = "redox", target_os = "wasi")
2647+
))]
25902648
#[allow(clippy::result_unit_err)]
25912649
pub fn to_file_path(&self) -> Result<PathBuf, ()> {
25922650
if let Some(segments) = self.path_segments() {
@@ -2902,7 +2960,6 @@ fn file_url_segments_to_pathbuf(
29022960
host: Option<&str>,
29032961
segments: str::Split<'_, char>,
29042962
) -> Result<PathBuf, ()> {
2905-
use alloc::vec::Vec;
29062963
use percent_encoding::percent_decode;
29072964
use std::ffi::OsStr;
29082965
#[cfg(any(unix, target_os = "redox"))]

url/src/origin.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
use crate::host::Host;
1010
use crate::parser::default_port;
1111
use crate::Url;
12-
use alloc::borrow::ToOwned;
13-
use alloc::string::String;
12+
use alloc::{borrow::ToOwned, string::String};
1413
use core::sync::atomic::{AtomicUsize, Ordering};
15-
use idna::domain_to_unicode;
1614

1715
pub fn url_origin(url: &Url) -> Origin {
1816
let scheme = url.scheme();

url/src/parser.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use alloc::borrow::ToOwned;
109
use alloc::string::{String, ToString};
1110
use core::fmt::{self, Formatter, Write};
1211
use core::str;

url/src/path_segments.rs

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,22 @@ use core::str;
2020
///
2121
/// ```rust
2222
/// use url::Url;
23-
/// # use std::error::Error;
23+
/// # use url::ParseError;
24+
/// # #[derive(Debug)]
25+
/// # /// A simple wrapper error struct for `no_std` support
26+
/// # struct TestError;
27+
/// # impl From<ParseError> for TestError {
28+
/// # fn from(value: ParseError) -> Self {
29+
/// # TestError {}
30+
/// # }
31+
/// # }
32+
/// # impl From<&str> for TestError {
33+
/// # fn from(value: &str) -> Self {
34+
/// # TestError {}
35+
/// # }
36+
/// # }
2437
///
25-
/// # fn run() -> Result<(), Box<dyn Error>> {
38+
/// # fn run() -> Result<(), TestError> {
2639
/// let mut url = Url::parse("mailto:me@example.com")?;
2740
/// assert!(url.path_segments_mut().is_err());
2841
///
@@ -79,9 +92,22 @@ impl<'a> PathSegmentsMut<'a> {
7992
///
8093
/// ```rust
8194
/// use url::Url;
82-
/// # use std::error::Error;
95+
/// # use url::ParseError;
96+
/// # #[derive(Debug)]
97+
/// # /// A simple wrapper error struct for `no_std` support
98+
/// # struct TestError;
99+
/// # impl From<ParseError> for TestError {
100+
/// # fn from(value: ParseError) -> Self {
101+
/// # TestError {}
102+
/// # }
103+
/// # }
104+
/// # impl From<&str> for TestError {
105+
/// # fn from(value: &str) -> Self {
106+
/// # TestError {}
107+
/// # }
108+
/// # }
83109
///
84-
/// # fn run() -> Result<(), Box<dyn Error>> {
110+
/// # fn run() -> Result<(), TestError> {
85111
/// let mut url = Url::parse("https://github.com/servo/rust-url/")?;
86112
/// url.path_segments_mut().map_err(|_| "cannot be base")?
87113
/// .clear().push("logout");
@@ -107,9 +133,22 @@ impl<'a> PathSegmentsMut<'a> {
107133
///
108134
/// ```rust
109135
/// use url::Url;
110-
/// # use std::error::Error;
136+
/// # use url::ParseError;
137+
/// # #[derive(Debug)]
138+
/// # /// A simple wrapper error struct for `no_std` support
139+
/// # struct TestError;
140+
/// # impl From<ParseError> for TestError {
141+
/// # fn from(value: ParseError) -> Self {
142+
/// # TestError {}
143+
/// # }
144+
/// # }
145+
/// # impl From<&str> for TestError {
146+
/// # fn from(value: &str) -> Self {
147+
/// # TestError {}
148+
/// # }
149+
/// # }
111150
///
112-
/// # fn run() -> Result<(), Box<dyn Error>> {
151+
/// # fn run() -> Result<(), TestError> {
113152
/// let mut url = Url::parse("https://github.com/servo/rust-url/")?;
114153
/// url.path_segments_mut().map_err(|_| "cannot be base")?
115154
/// .push("pulls");
@@ -182,9 +221,22 @@ impl<'a> PathSegmentsMut<'a> {
182221
///
183222
/// ```rust
184223
/// use url::Url;
185-
/// # use std::error::Error;
224+
/// # use url::ParseError;
225+
/// # #[derive(Debug)]
226+
/// # /// A simple wrapper error struct for `no_std` support
227+
/// # struct TestError;
228+
/// # impl From<ParseError> for TestError {
229+
/// # fn from(value: ParseError) -> Self {
230+
/// # TestError {}
231+
/// # }
232+
/// # }
233+
/// # impl From<&str> for TestError {
234+
/// # fn from(value: &str) -> Self {
235+
/// # TestError {}
236+
/// # }
237+
/// # }
186238
///
187-
/// # fn run() -> Result<(), Box<dyn Error>> {
239+
/// # fn run() -> Result<(), TestError> {
188240
/// let mut url = Url::parse("https://github.com/")?;
189241
/// let org = "servo";
190242
/// let repo = "rust-url";
@@ -202,9 +254,22 @@ impl<'a> PathSegmentsMut<'a> {
202254
///
203255
/// ```rust
204256
/// use url::Url;
205-
/// # use std::error::Error;
257+
/// # use url::ParseError;
258+
/// # #[derive(Debug)]
259+
/// # /// A simple wrapper error struct for `no_std` support
260+
/// # struct TestError;
261+
/// # impl From<ParseError> for TestError {
262+
/// # fn from(value: ParseError) -> Self {
263+
/// # TestError {}
264+
/// # }
265+
/// # }
266+
/// # impl From<&str> for TestError {
267+
/// # fn from(value: &str) -> Self {
268+
/// # TestError {}
269+
/// # }
270+
/// # }
206271
///
207-
/// # fn run() -> Result<(), Box<dyn Error>> {
272+
/// # fn run() -> Result<(), TestError> {
208273
/// let mut url = Url::parse("https://github.com/servo")?;
209274
/// url.path_segments_mut().map_err(|_| "cannot be base")?
210275
/// .extend(&["..", "rust-url", ".", "pulls"]);

0 commit comments

Comments
 (0)