Skip to content

Commit

Permalink
refactor to Interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmindlin committed Jul 25, 2024
1 parent ef52a87 commit 241466d
Show file tree
Hide file tree
Showing 11 changed files with 1,141 additions and 1,036 deletions.
12 changes: 1 addition & 11 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ scout-interpreter = { version = "0.6.0", path = "./scout-interpreter/" }
scout-lexer = { version = "0.6.0", path = "./scout-lexer/" }
scout-parser = { version = "0.6.0", path = "./scout-parser/" }
rustyline = "8.0.0"
colored = "2"
fantoccini = { version = "0.19.3", features = ["rustls-tls"] }
tokio = { version = "1", features = ["full"] }
serde_json = "1.0"
Expand Down
1 change: 1 addition & 0 deletions scout-interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ image = "0.25.1"
scout-lexer = { version = "0.6.0", path = "../scout-lexer/" }
url = "2.5.2"
reqwest = { version = "0.12", features = ["json", "cookies"] }
envy = "0.4.2"

[dev-dependencies]
test-case = "3.3.1"
79 changes: 79 additions & 0 deletions scout-interpreter/src/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use crate::{env::EnvPointer, eval::ScrapeResultsPtr, EnvVars, Interpreter};

#[derive(Debug)]
pub enum BuilderError {
BrowserStartup(String),
EnvError(String),
}

#[derive(Default)]
pub struct InterpreterBuilder {
env: Option<EnvPointer>,
crawler: Option<fantoccini::Client>,
results: Option<ScrapeResultsPtr>,
}

impl InterpreterBuilder {
pub fn with_env(mut self, env: EnvPointer) -> Self {
self.env = Some(env);
self
}

pub fn with_crawler(mut self, crawler: fantoccini::Client) -> Self {
self.crawler = Some(crawler);
self
}

pub fn with_results(mut self, results: ScrapeResultsPtr) -> Self {
self.results = Some(results);
self
}

pub async fn build(self) -> Result<Interpreter, BuilderError> {
let env_vars =
envy::from_env::<EnvVars>().map_err(|e| BuilderError::EnvError(e.to_string()))?;
let crawler = match self.crawler {
Some(c) => Ok(c),
None => new_crawler(&env_vars).await,
}?;
let interpreter = Interpreter::new(
self.env.unwrap_or(EnvPointer::default()),
self.results.unwrap_or(ScrapeResultsPtr::default()),
crawler,
);
Ok(interpreter)
}
}

async fn new_crawler(env_vars: &EnvVars) -> Result<fantoccini::Client, BuilderError> {
let mut caps = serde_json::map::Map::new();
if !env_vars.scout_debug {
let opts = serde_json::json!({ "args": ["--headless"] });
caps.insert("moz:firefoxOptions".into(), opts);
}
if let Some(proxy) = env_vars.scout_proxy.clone() {
let opt = serde_json::json!({
"proxyType": "manual",
"httpProxy": proxy,
});
caps.insert("proxy".into(), opt);
}
let conn_url = format!("http://localhost:{}", env_vars.scout_port);
let crawler = fantoccini::ClientBuilder::native()
.capabilities(caps)
.connect(&conn_url)
.await
.map_err(|e| BuilderError::BrowserStartup(e.to_string()))?;
Ok(crawler)
}

impl std::fmt::Display for BuilderError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BuilderError::BrowserStartup(e) => write!(f, "{}", e),
BuilderError::EnvError(e) => write!(f, "{}", e),
}
}
}

impl std::error::Error for BuilderError {}
4 changes: 2 additions & 2 deletions scout-interpreter/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use scout_parser::ast::Identifier;
use serde_json::Value;

use crate::{
eval::{EvalError, EvalResult, ScrapeResultsPtr},
object::{json_to_obj, Object},
EvalError, EvalResult, ScrapeResultsPtr,
};

macro_rules! assert_param_len {
($arg:expr, $len:expr) => {
if $arg.len() < $len {
return Err($crate::EvalError::InvalidFnParams);
return Err($crate::eval::EvalError::InvalidFnParams);
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion scout-interpreter/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Object;
use crate::object::Object;
use futures::future::BoxFuture;
use futures::lock::Mutex;
use futures::FutureExt;
Expand Down
Loading

0 comments on commit 241466d

Please sign in to comment.