Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example in jaq-core/src/lib.rs fails to compile #251

Open
liquidaty opened this issue Dec 31, 2024 · 3 comments
Open

example in jaq-core/src/lib.rs fails to compile #251

liquidaty opened this issue Dec 31, 2024 · 3 comments

Comments

@liquidaty
Copy link

liquidaty commented Dec 31, 2024

The example described in jaq-core/src/lib.rs fails to compile:

error[E0277]: the trait bound `Val: From<Value>` is not satisfied
  --> src/main.rs:26:53
   |
26 |    let mut out = filter.run((Ctx::new([], &inputs), Val::from(input)));
   |                                                     ^^^ the trait `From<Value>` is not implemented for `Val`
   |
   = help: the following other types implement trait `From<T>`:
             `Val` implements `From<bool>`
             `Val` implements `From<f64>`
             `Val` implements `From<isize>`
             `Val` implements `From<std::string::String>`

Changing each occurrence of Val::from(x) to Val::from(x.to_string()) fixes.

@liquidaty
Copy link
Author

Correction: adding to_string() lets it compile, but then the behavior is incorrect. Could anyone provide a trivial example of how to use lib.rs to execute a filter on a given string, and print the output to console?

Error using the below main.rs:

thread 'main' panicked at src/main.rs:29:4:
assertion `left == right` failed
  left: Some(Err(Error(Str([Str("cannot use "), Val(Str("[\"Hello\",\"world\"]")), Str(" as "), Str("iterable (array or object)")]))))
 right: Some(Ok(Str("\"Hello\"")))

main.rs:

use jaq_core::{load, Compiler, Ctx, Error, FilterT, RcIter};
use jaq_json::Val;
use serde_json::{json, Value};

fn main() {
   let input = json!(["Hello", "world"]);
   let program = File { code: ".[]", path: () };

   use load::{Arena, File, Loader};
   
   let loader = Loader::new(jaq_std::defs().chain(jaq_json::defs()));
   let arena = Arena::default();
   
   // parse the filter
   let modules = loader.load(&arena, program).unwrap();
   
   // compile the filter
   let filter = Compiler::default()
     .with_funs(jaq_std::funs().chain(jaq_json::funs()))
     .compile(modules)
     .unwrap();
   
   let inputs = RcIter::new(core::iter::empty());
   
   // iterator over the output values
   let mut out = filter.run((Ctx::new([], &inputs), Val::from(input.to_string())));

   assert_eq!(out.next(), Some(Ok(Val::from(json!("Hello").to_string()))));
   assert_eq!(out.next(), Some(Ok(Val::from(json!("world").to_string()))));
   assert_eq!(out.next(), None);
}

Cargo.toml:

[package]
name = "my_jaq_example"
version = "0.1.0"
edition = "2021"

[dependencies]
serde_json = "1.0.134"
jaq-core = "2.0.0"
jaq-json = "1.0.0"
jaq-std = "2.0.0"

@liquidaty
Copy link
Author

Update: got a working example latest jaq as of 2024-12-31, lmk if helpful to share. I borrowed code from src/main.rs to do so, and it is substantially different from what is documented in src/lib.rs

@enotodden
Copy link

enotodden commented Jan 2, 2025

Found that the example code that uses the json! macro works when enabling the serde_json feature for the jaq-json crate. Hope it helps if anyone lands here with the same issue.

@liquidaty liquidaty changed the title jaq-core/src/lib.rs documentation error: Val::from(x) should be Val::from(x.to_string()) example in jaq-core/src/lib.rs fails to compile Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants