Skip to content

Commit

Permalink
crawl stmt
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmindlin committed Jun 24, 2024
1 parent b9ff85a commit 5cb6b63
Show file tree
Hide file tree
Showing 11 changed files with 333 additions and 161 deletions.
132 changes: 18 additions & 114 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ colored = "2"
fantoccini = "0.19.3"
tokio = { version = "1", features = ["full"] }
serde_json = "1.0"
clap = { version = "4.5.7", features = ["derive"] }
envy = "0.4.2"
serde = { version = "1.0.203", features = ["derive"] }

# The profile that 'cargo dist' will build with
[profile.dist]
Expand Down
1 change: 1 addition & 0 deletions scout-interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
image = "0.25.1"
scout-lexer = { path = "../scout-lexer/" }
url = "2.5.2"

[dev-dependencies]
test-case = "*"
40 changes: 36 additions & 4 deletions scout-interpreter/src/builtin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{env, sync::Arc};

use fantoccini::{
actions::{InputSource, KeyAction, KeyActions},
Expand All @@ -19,6 +19,7 @@ macro_rules! assert_param_len {

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum BuiltinKind {
Args,
Print,
TextContent,
Href,
Expand All @@ -30,12 +31,17 @@ pub enum BuiltinKind {
Contains,
Type,
KeyPress,
Number,
Url,
}

impl BuiltinKind {
pub fn is_from(s: &str) -> Option<Self> {
use BuiltinKind::*;
match s {
"url" => Some(Url),
"number" => Some(Number),
"args" => Some(Args),
"print" => Some(Print),
"textContent" => Some(TextContent),
"trim" => Some(Trim),
Expand All @@ -59,6 +65,34 @@ impl BuiltinKind {
) -> EvalResult {
use BuiltinKind::*;
match self {
Url => {
let url = crawler.current_url().await?;
Ok(Arc::new(Object::Str(url.to_string())))
}
Number => {
assert_param_len!(args, 1);
if let Object::Str(s) = &*args[0] {
match s.parse::<f64>() {
Ok(n) => Ok(Arc::new(Object::Number(n))),
Err(_) => Err(EvalError::InvalidUsage(
"input to number() must be a valid number".into(),
)),
}
} else {
Err(EvalError::InvalidUsage(
"number() takes a str as input".to_owned(),
))
}
}
Args => {
let env_args = env::args().collect::<Vec<String>>();
let mut out = Vec::new();
// start at 2 because idx 0 is the executable location & idx 1 is the filename
for idx in 2..env_args.len() {
out.push(Arc::new(Object::Str(env_args[idx].clone())));
}
Ok(Arc::new(Object::List(out)))
}
Type => {
assert_param_len!(args, 1);
Ok(Arc::new(Object::Str(args[0].type_str().to_string())))
Expand Down Expand Up @@ -140,9 +174,7 @@ impl BuiltinKind {
assert_param_len!(args, 2);
match (&*args[0], &*args[1]) {
(Object::Node(elem), Object::Str(s)) => {
elem.send_keys(s)
.map_err(|_| EvalError::BrowserError)
.await?;
elem.send_keys(s).map_err(EvalError::BrowserError).await?;

if args.len() > 2 && args[2].is_truthy() {
let actions =
Expand Down
Loading

0 comments on commit 5cb6b63

Please sign in to comment.