(path: &Path, opt: &prqlc::Options)
| 94 | } |
| 95 | |
| 96 | fn compile_path(path: &Path, opt: &prqlc::Options) -> Result<()> { |
| 97 | // filter to only .prql files |
| 98 | if path.extension() != Some(OsStr::new("prql")) { |
| 99 | return Ok(()); |
| 100 | } |
| 101 | |
| 102 | let sql_path = path.with_extension("sql"); |
| 103 | let prql_path = path; |
| 104 | |
| 105 | // read |
| 106 | let Some(prql_string) = fs::read_to_string(prql_path).ok() else { |
| 107 | // file may not exist, because this may have been a delete event |
| 108 | return Ok(()); |
| 109 | }; |
| 110 | if prql_string.is_empty() { |
| 111 | return Ok(()); |
| 112 | } |
| 113 | |
| 114 | // pre-process Jinja |
| 115 | let (prql_string, jinja_context) = jinja::pre_process(&prql_string)?; |
| 116 | |
| 117 | // compile |
| 118 | println!("Compiling {}", prql_path.display()); |
| 119 | let sql_string = match prqlc::compile(&prql_string, opt) { |
| 120 | Ok(sql_string) => sql_string, |
| 121 | Err(errs) => { |
| 122 | for err in errs.inner { |
| 123 | println!("{err}"); |
| 124 | } |
| 125 | return Err(anyhow!("failed to compile")); |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | // post-process Jinja |
| 130 | let sql_string = jinja::post_process(&sql_string, jinja_context); |
| 131 | |
| 132 | // write |
| 133 | fs::write(sql_path, sql_string)?; |
| 134 | |
| 135 | Ok(()) |
| 136 | } |
no test coverage detected