Skip to content

Commit

Permalink
Fix WASM build + Clippies
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr committed Mar 26, 2024
1 parent 26d7802 commit 14083df
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 31 deletions.
13 changes: 13 additions & 0 deletions crates/core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ pub trait Context {
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>>;

#[cfg(all(
feature = "network_requests_external",
feature = "external_functions_ffi",
not(feature = "network_requests"),
target_arch = "wasm32"
))]
fn exec_external(
&self,
code: &[u8],
param_names: Vec<String>,
input_bindings: &[&str],
) -> Result<Vec<u8>>;

// FIXME: Don't depend on Grit's file handling in Context.
fn files(&self) -> &FileOwners;

Expand Down
24 changes: 14 additions & 10 deletions crates/core/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use std::path::Path;

#[cfg(not(feature = "grit-parser"))]
use anyhow::anyhow;
use anyhow::Context;
use crate::pattern::api::InputFile;
use anyhow::Result;
use marzano_language::{language::Language, target_language::TargetLanguage};
use serde_json::to_string_pretty;
use marzano_language::target_language::TargetLanguage;
use std::path::Path;
use tree_sitter::Parser;

use crate::{pattern::api::InputFile, tree_sitter_serde::tree_sitter_node_to_json};

#[cfg(feature = "grit-parser")]
pub fn parse_input_file(lang: &TargetLanguage, input: &str, path: &Path) -> Result<InputFile> {
use crate::tree_sitter_serde::tree_sitter_node_to_json;
use marzano_language::language::Language;
use serde_json::to_string_pretty;

let mut parser = Parser::new().unwrap();
parser.set_language(lang.get_ts_language()).unwrap();
let tree = parser.parse(input.as_bytes(), None).unwrap().unwrap();
Expand All @@ -27,14 +25,18 @@ pub fn parse_input_file(lang: &TargetLanguage, input: &str, path: &Path) -> Resu
})
}
#[cfg(not(feature = "grit-parser"))]
pub fn parse_input_file(lang: &TargetLanguage, input: &str, path: &Path) -> Result<InputFile> {
pub fn parse_input_file(_lang: &TargetLanguage, _input: &str, _path: &Path) -> Result<InputFile> {
use anyhow::anyhow;

Err(anyhow!(
"enable grit-parser feature flag to parse a grit file"
))
}

#[cfg(feature = "grit-parser")]
pub fn make_grit_parser() -> Result<Parser> {
use anyhow::Context;

let mut parser = Parser::new().unwrap();
parser
.set_language(&tree_sitter_gritql::language().into())
Expand All @@ -44,6 +46,8 @@ pub fn make_grit_parser() -> Result<Parser> {

#[cfg(not(feature = "grit-parser"))]
pub fn make_grit_parser() -> Result<Parser> {
use anyhow::anyhow;

Err(anyhow!(
"enable grit-parser feature flag to make a grit parser"
))
Expand Down
15 changes: 15 additions & 0 deletions crates/core/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,21 @@ impl<'a> Context for MarzanoContext<'a> {
self.built_ins.call(call, context, state, logs)
}

#[cfg(all(
feature = "network_requests_external",
feature = "external_functions_ffi",
not(feature = "network_requests"),
target_arch = "wasm32"
))]
fn exec_external(
&self,
code: &[u8],
param_names: Vec<String>,
input_bindings: &[&str],
) -> Result<Vec<u8>> {
(self.runtime.exec_external)(code, param_names, input_bindings)
}

// FIXME: Don't depend on Grit's file handling in context.
fn files(&self) -> &FileOwners {
self.files
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/pattern/function_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl FunctionDefinition for ForeignFunctionDefinition {

// START Simple externalized version
#[cfg(all(feature = "external_functions_ffi", target_arch = "wasm32"))]
let result = (context.runtime.exec_external)(&self.code, param_names, &resolved_str)?;
let result = context.exec_external(&self.code, param_names, &resolved_str)?;

// END Simple externalized version

Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/suppress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn is_suppress_comment(
return Ok(false);
};
let ignored_rules = ignore_spec.split(',').map(|s| s.trim()).collect::<Vec<_>>();
Ok(ignored_rules.contains(&&current_name[..]))
Ok(ignored_rules.contains(&current_name))
}

fn comment_applies_to_range(
Expand Down
2 changes: 1 addition & 1 deletion crates/language/src/target_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl PatternLanguage {
}

#[cfg(not(feature = "builtin-parser"))]
pub fn get_language_with_parser(parser: &mut Parser, body: &str) -> Option<Self> {
pub fn get_language_with_parser(_parser: &mut Parser, _body: &str) -> Option<Self> {
unimplemented!("grit_parser is unavailable when feature flag [builtin-parser] is off.")
}

Expand Down
42 changes: 24 additions & 18 deletions crates/util/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::env;

use anyhow::anyhow;
use anyhow::Result;
use http::HeaderMap;
use std::env;
#[cfg(feature = "network_requests")]
use tokio::runtime::Handle;

Expand All @@ -25,6 +23,23 @@ pub struct ExecutionContext {
pub ignore_limit_pattern: bool,
}

#[cfg(all(
feature = "network_requests_external",
feature = "external_functions_ffi",
not(feature = "network_requests"),
target_arch = "wasm32"
))]
type FetchFn = fn(url: &str, headers: &HeaderMap, json: &serde_json::Value) -> Result<String>;

#[cfg(all(
feature = "network_requests_external",
feature = "external_functions_ffi",
not(feature = "network_requests"),
target_arch = "wasm32"
))]
type ExecExternalFn =
fn(code: &[u8], param_names: Vec<String>, input_bindings: &[&str]) -> Result<Vec<u8>>;

/// This variant of execution context depends on an *external* system making HTTP requests.
/// It is particularly useful for the WebAssembly variant of Marzano.
#[cfg(all(
Expand All @@ -36,9 +51,8 @@ pub struct ExecutionContext {
#[derive(Clone, Debug)]
pub struct ExecutionContext {
llm_api: Option<LanguageModelAPI>,
fetch: fn(url: &str, headers: &HeaderMap, json: &serde_json::Value) -> Result<String>,
pub exec_external:
fn(code: &[u8], param_names: Vec<String>, input_bindings: &[&str]) -> Result<Vec<u8>>,
fetch: FetchFn,
pub exec_external: ExecExternalFn,
pub ignore_limit_pattern: bool,
}

Expand Down Expand Up @@ -87,14 +101,7 @@ impl ExecutionContext {
not(feature = "network_requests"),
target_arch = "wasm32"
))]
pub fn new(
fetch: fn(url: &str, headers: &HeaderMap, json: &serde_json::Value) -> Result<String>,
exec_external: fn(
code: &[u8],
param_names: Vec<String>,
input_bindings: &[&str],
) -> Result<Vec<u8>>,
) -> ExecutionContext {
pub fn new(fetch: FetchFn, exec_external: ExecExternalFn) -> ExecutionContext {
Self {
llm_api: None,
fetch,
Expand All @@ -116,10 +123,9 @@ impl ExecutionContext {
url: &str,
token: &str,
) -> Result<String> {
let handle = self
.handle
.as_ref()
.ok_or_else(|| anyhow!("llm request must be made from within a tokio runtime"))?;
let handle = self.handle.as_ref().ok_or_else(|| {
anyhow::anyhow!("llm request must be made from within a tokio runtime")
})?;
let client = self.reqwest.clone();
let url = url.to_owned();
let token = token.to_owned();
Expand Down

0 comments on commit 14083df

Please sign in to comment.