Skip to content

Commit

Permalink
feat: better formatting for trees in scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
cestef committed Jun 19, 2024
1 parent b6e56d9 commit 376497a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/cli/interactive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use tokio::sync::Mutex;

use crate::{
runner::scripting::ScriptingResponse,
utils::tree::{tree, TreeData, TreeNode},
utils::tree::{tree_data, tree_node, TreeData, TreeNode},
};

use super::opts::Opts;
Expand Down Expand Up @@ -61,8 +61,11 @@ pub async fn main_interactive(opts: Opts) -> Result<()> {
let rl = Arc::new(Mutex::new(rl));
let mut engine = Engine::new();

let tree_module = exported_module!(tree);
let tree_module = exported_module!(tree_node);
let tree_data_module = exported_module!(tree_data);
engine.register_global_module(tree_module.into());
engine.register_global_module(tree_data_module.into());

engine.build_type::<TreeData>();
engine.build_type::<ScriptingResponse>();
let engine = Arc::new(Mutex::new(engine));
Expand Down
7 changes: 5 additions & 2 deletions src/runner/scripting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::BTreeMap;

use crate::{
cli::opts::Opts,
utils::tree::{tree, TreeData},
utils::tree::{tree_data, tree_node, TreeData},
};
use color_eyre::eyre::{eyre, Result};
use colored::Colorize;
Expand Down Expand Up @@ -53,9 +53,12 @@ pub async fn run_scripts(
progress: ProgressBar,
) -> Result<()> {
let mut engine = Engine::new();
let tree_module = exported_module!(tree);
let tree_module = exported_module!(tree_node);
let tree_data_module = exported_module!(tree_data);

engine.register_global_module(tree_module.into());
engine.register_global_module(tree_data_module.into());

let mut root_scope = Scope::new();
root_scope.push("data", data.clone());
root_scope.push("opts", opts.clone());
Expand Down
33 changes: 32 additions & 1 deletion src/utils/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rhai::plugin::*;
use rhai::{CustomType, TypeBuilder};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt::Debug;
use std::{collections::HashMap, sync::Arc};

use crate::{
Expand Down Expand Up @@ -37,7 +38,7 @@ pub struct TreeData {
}

#[export_module]
pub mod tree {
pub mod tree_node {
#[rhai_fn(get = "children")]
pub fn children(data: &mut TreeNode<TreeData>) -> Dynamic {
let mut children = Vec::new();
Expand All @@ -51,6 +52,36 @@ pub mod tree {
pub fn data(data: &mut TreeNode<TreeData>) -> TreeData {
data.data.clone()
}

#[rhai_fn(global)]
pub fn to_string(data: &mut TreeNode<TreeData>) -> String {
format!(
"TreeNode {{ data: {}, children: [{}] }}",
tree_data::to_string(&mut data.data),
data.children.len()
)
}
}

#[export_module]
pub mod tree_data {
#[rhai_fn(global)]
pub fn to_string(data: &mut TreeData) -> String {
format!(
"TreeData {{ url: {}, depth: {}, path: {}, status_code: {}, url_type: {:?}, extra: {:?} }}",
data.url,
data.depth,
data.status_code,
data.path,
match &data.url_type {
UrlType::Directory => "dir",
UrlType::File(ext) => ext,
UrlType::Unknown => "unknown",
UrlType::None => "",
},
data.extra
)
}
#[rhai_fn(get = "response")]
pub fn get_response(data: &mut TreeData) -> Dynamic {
if let Some(response) = &data.response {
Expand Down

0 comments on commit 376497a

Please sign in to comment.