Skip to content

Commit

Permalink
chore: prepare for ada v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Aug 30, 2023
1 parent 8e4f1eb commit 307e7bd
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 32 deletions.
38 changes: 35 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/collect_links/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ path = "collect_links.rs"

[dependencies]
lychee-lib = { path = "../../lychee-lib", version = "0.13.0", default-features = false }
ada-url = { version = "1.4.3" }
ada-url = { path = "../../../ada-rust", features = ["serde"] }
tokio = { version = "1.32.0", features = ["full"] }
regex = "1.9.3"
http = "0.2.9"
Expand Down
2 changes: 1 addition & 1 deletion lychee-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ version = "0.13.0"
[dependencies]
lychee-lib = { path = "../lychee-lib", version = "0.13.0", default-features = false }

ada-url = { version = "1.4.3", features = ["serde"] }
ada-url = { path = "../../ada-rust", features = ["serde"] }
anyhow = "1.0.75"
assert-json-diff = "2.0.2"
clap = { version = "4.3.23", features = ["env", "derive"] }
Expand Down
2 changes: 1 addition & 1 deletion lychee-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ async fn run(opts: &LycheeOptions) -> Result<i32> {
.use_html5ever(std::env::var("LYCHEE_USE_HTML5EVER").map_or(false, |x| x == "1"));

if opts.config.dump_inputs {
let sources = collector.collect_sources(inputs).await;
let sources = collector.collect_sources(inputs);
let exit_code = commands::dump_inputs(sources, opts.config.output.as_ref()).await?;

return Ok(exit_code as i32);
Expand Down
2 changes: 1 addition & 1 deletion lychee-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repository = "https://github.com/lycheeverse/lychee"
version = "0.13.0"

[dependencies]
ada-url = { version = "1.4.3", features = ["serde"] }
ada-url = { path = "../../ada-rust", features = ["serde"] }
async-stream = "0.3.5"
cached = "0.44.0"
check-if-email-exists = { version = "0.9.0", optional = true }
Expand Down
10 changes: 5 additions & 5 deletions lychee-lib/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ impl Collector {

/// Collect all sources from a list of [`Input`]s. For further details,
/// see also [`Input::get_sources`](crate::Input#method.get_sources).
pub async fn collect_sources(self, inputs: Vec<Input>) -> impl Stream<Item = Result<String>> {
pub fn collect_sources(self, inputs: Vec<Input>) -> impl Stream<Item = Result<String>> {
stream::iter(inputs)
.par_then_unordered(None, move |input| async move { input.get_sources().await })
.par_then_unordered(None, move |input| async move { input.get_sources() })
.flatten()
}

Expand All @@ -82,7 +82,7 @@ impl Collector {
let skip_missing_inputs = self.skip_missing_inputs;
let contents = stream::iter(inputs)
.par_then_unordered(None, move |input| async move {
input.get_contents(skip_missing_inputs).await
input.get_contents(skip_missing_inputs)
})
.flatten();

Expand Down Expand Up @@ -140,7 +140,7 @@ mod tests {
let file_path = temp_dir.path().join("README");
let _file = File::create(&file_path).unwrap();
let input = Input::new(&file_path.as_path().display().to_string(), None, true, None)?;
let contents: Vec<_> = input.get_contents(true).await.collect::<Vec<_>>().await;
let contents: Vec<_> = input.get_contents(true).collect::<Vec<_>>().await;

assert_eq!(contents.len(), 1);
assert_eq!(contents[0].as_ref().unwrap().file_type, FileType::Plaintext);
Expand All @@ -150,7 +150,7 @@ mod tests {
#[tokio::test]
async fn test_url_without_extension_is_html() -> Result<()> {
let input = Input::new("https://example.com/", None, true, None)?;
let contents: Vec<_> = input.get_contents(true).await.collect::<Vec<_>>().await;
let contents: Vec<_> = input.get_contents(true).collect::<Vec<_>>().await;

assert_eq!(contents.len(), 1);
assert_eq!(contents[0].as_ref().unwrap().file_type, FileType::Html);
Expand Down
3 changes: 2 additions & 1 deletion lychee-lib/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ pub(crate) fn website(url: &str) -> Uri {

/// Creates a mail URI from a string
pub(crate) fn mail(address: &str) -> Uri {
let input = "mailto:".to_string() + address;
if address.starts_with("mailto:") {
Url::parse(address, None)
} else {
Url::parse(&("mailto:".to_string() + address), None)
Url::parse(input.as_str(), None)
}
.expect("Expected valid Mail Address")
.into()
Expand Down
11 changes: 4 additions & 7 deletions lychee-lib/src/types/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,7 @@ impl Input {
/// Returns an error if the contents can not be retrieved
/// because of an underlying I/O error (e.g. an error while making a
/// network request or retrieving the contents from the file system)
pub async fn get_contents(
self,
skip_missing: bool,
) -> impl Stream<Item = Result<InputContent>> {
pub fn get_contents(self, skip_missing: bool) -> impl Stream<Item = Result<InputContent>> {
try_stream! {
match self.source {
InputSource::RemoteUrl(ref url) => {
Expand All @@ -198,7 +195,7 @@ impl Input {
ref pattern,
ignore_case,
} => {
for await content in self.glob_contents(pattern, ignore_case).await {
for await content in self.glob_contents(pattern, ignore_case) {
let content = content?;
yield content;
}
Expand Down Expand Up @@ -271,7 +268,7 @@ impl Input {
/// # Errors
///
/// Returns an error if the globbing fails with the expanded pattern.
pub async fn get_sources(self) -> impl Stream<Item = Result<String>> {
pub fn get_sources(self) -> impl Stream<Item = Result<String>> {
try_stream! {
match self.source {
InputSource::RemoteUrl(url) => yield url.to_string(),
Expand Down Expand Up @@ -315,7 +312,7 @@ impl Input {
Ok(input_content)
}

async fn glob_contents(
fn glob_contents(
&self,
pattern: &str,
ignore_case: bool,
Expand Down
2 changes: 1 addition & 1 deletion lychee-lib/src/types/uri/valid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Uri {
#[inline]
/// Changes this URL's scheme.
pub(crate) fn set_scheme(&mut self, scheme: &str) {
self.url.set_protocol(scheme);
let _ = self.url.set_protocol(scheme);
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion lychee-lib/src/utils/fragment_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl FragmentChecker {
}

fn remove_fragment(mut url: Url) -> String {
url.set_hash("");
url.set_hash(None);
url.into()
}
}
15 changes: 5 additions & 10 deletions lychee-lib/src/utils/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,7 @@ fn construct_url(base: &Option<Url>, text: &str) -> Result<Url> {
})
}

fn create_uri_from_path(
src: &Path,
dst: &str,
base: &Option<Base>,
) -> Result<Option<ada_url::Url>> {
fn create_uri_from_path(src: &Path, dst: &str, base: &Option<Base>) -> Result<Option<Url>> {
let (dst, frag) = url::remove_get_params_and_separate_fragment(dst);
// Avoid double-encoding already encoded destination paths by removing any
// potential encoding (e.g. `web%20site` becomes `web site`).
Expand All @@ -156,13 +152,12 @@ fn create_uri_from_path(
let decoded = percent_decode_str(dst).decode_utf8()?;
let resolved = path::resolve(src, &PathBuf::from(&*decoded), base)?;
match resolved {
Some(path) => ada_url::Url::parse(path.to_str().unwrap(), Some("file://"))
Some(path) => Url::parse(path.to_str().unwrap(), Some("file://"))
.map(|mut url| {
url.set_hash(frag.unwrap_or(""));
url
url.set_hash(frag);
Some(url)
})
.map(Some)
.map_err(|_e| ErrorKind::InvalidUrlFromPath(path)),
.map_err(|_e| ErrorKind::InvalidUrlFromPath(path.clone())),
None => Ok(None),
}
}
Expand Down

0 comments on commit 307e7bd

Please sign in to comment.