(&self, sources: &'a mut SourceTree, main_path: &'a str)
| 361 | } |
| 362 | |
| 363 | fn execute<'a>(&self, sources: &'a mut SourceTree, main_path: &'a str) -> Result<Vec<u8>> { |
| 364 | let main_path = main_path |
| 365 | .split('.') |
| 366 | .filter(|x| !x.is_empty()) |
| 367 | .map(str::to_string) |
| 368 | .collect_vec(); |
| 369 | |
| 370 | Ok(match self { |
| 371 | Command::Parse { format, .. } => { |
| 372 | let ast = prql_to_pl_tree(sources)?; |
| 373 | match format { |
| 374 | Format::Json => serde_json::to_string_pretty(&ast)?.into_bytes(), |
| 375 | Format::Yaml => serde_yaml::to_string(&ast)?.into_bytes(), |
| 376 | } |
| 377 | } |
| 378 | Command::Lex { format, .. } => { |
| 379 | let s = sources.sources.values().exactly_one().or_else(|_| { |
| 380 | // TODO: allow multiple sources |
| 381 | bail!("Currently `lex` only works with a single source, but found multiple sources") |
| 382 | })?; |
| 383 | let tokens = prql_to_tokens(s)?; |
| 384 | match format { |
| 385 | Format::Json => serde_json::to_string_pretty(&tokens)?.into_bytes(), |
| 386 | Format::Yaml => serde_yaml::to_string(&tokens)?.into_bytes(), |
| 387 | } |
| 388 | } |
| 389 | Command::Collect(_) => { |
| 390 | let mut root_module_def = prql_to_pl_tree(sources)?; |
| 391 | |
| 392 | drop_module_def(&mut root_module_def.stmts, "std"); |
| 393 | |
| 394 | pl_to_prql(&root_module_def)?.into_bytes() |
| 395 | } |
| 396 | Command::Debug(DebugCommand::Annotate(_)) => { |
| 397 | let (_, source) = sources.sources.clone().into_iter().exactly_one().or_else( |
| 398 | |_| bail!( |
| 399 | "Currently `annotate` only works with a single source, but found multiple sources: {:?}", |
| 400 | sources.sources.keys() |
| 401 | .map(|x| x.display().to_string()) |
| 402 | .sorted() |
| 403 | .map(|x| format!("`{x}`")) |
| 404 | .join(", ") |
| 405 | ) |
| 406 | )?; |
| 407 | |
| 408 | // TODO: potentially if there is code performing a role beyond |
| 409 | // presentation, it should be a library function; and we could |
| 410 | // promote it to the `prqlc` crate. |
| 411 | let root_mod = prql_to_pl(&source)?; |
| 412 | |
| 413 | // resolve |
| 414 | let ctx = semantic::resolve(root_mod)?; |
| 415 | |
| 416 | let frames = if let Ok((main, _)) = ctx.find_main_rel(&[]) { |
| 417 | semantic::reporting::collect_frames(*main.clone().into_relation_var().unwrap()) |
| 418 | .frames |
| 419 | } else { |
| 420 | vec![] |
no test coverage detected