Skip to content

Commit

Permalink
support file:// schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
suaviloquence committed Jul 24, 2024
1 parent 26fd392 commit ac3d8ba
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
40 changes: 26 additions & 14 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{borrow::Cow, cell::OnceCell, collections::BTreeMap, sync::Arc};

use anyhow::Context;
use execution_mode::ExecutionMode;
use reqwest::IntoUrl;
use reqwest::Url;

use value::{EValue, ListIter};

Expand Down Expand Up @@ -117,25 +117,32 @@ impl<'ast> Interpreter<'ast> {
}

#[inline]
pub async fn interpret<U: IntoUrl>(
pub async fn interpret(
&self,
root_url: U,
root_url: Url,
head: Option<AstRef<'ast, StatementList<'ast>>>,
) -> Result<DataVariables<'ast>> {
let html = self.get_html(root_url).await?;
self.interpret_block(html.root_element(), head, None).await
}

async fn get_html<U: IntoUrl>(&self, url: U) -> Result<scraper::Html> {
let text = self
.client
.get(url)
.send()
.await
.context("Error sending HTTP request")?
.text()
.await
.context("Error getting HTTP body text")?;
async fn get_html(&self, url: Url) -> Result<scraper::Html> {
let text = match url.scheme() {
"http" | "https" => self
.client
.get(url)
.send()
.await
.context("Error sending HTTP request")?
.text()
.await
.context("Error getting HTTP body text")?,
"file" => tokio::fs::read_to_string(url.path())
.await
.with_context(|| format!("Error reading from file `{}`", url.path()))?,
other => anyhow::bail!("unknown URL scheme `{other}`"),
};

Ok(scraper::Html::parse_document(&text))
}

Expand Down Expand Up @@ -185,7 +192,12 @@ impl<'ast> Interpreter<'ast> {

let root_element = if let Some(url) = &element.url {
let url: Arc<str> = self.eval_inline(url, ctx)?.try_unwrap()?;
html = self.get_html(&*url).await?;
html = self
.get_html(
url.parse()
.with_context(|| format!("`{url}` is not a valid URL"))?,
)
.await?;
html.root_element()
} else {
ctx.element
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ async fn main() -> anyhow::Result<()> {

let interpreter = Interpreter::new(&ast);

let results = interpreter.interpret(url, head).await?;
let results = interpreter
.interpret(
url.parse()
.with_context(|| format!("Couldn't parse `{url}` into a URL"))?,
head,
)
.await?;

let results = Value::Structure(
results
Expand Down

0 comments on commit ac3d8ba

Please sign in to comment.