1

In my project I want to analyze the source code of a PHP file and convert it to AST. There is already a javascript package for this, but it only works with node. So I tried to convert the PHP code to AST using a Rust library. However, I have no experience with Rust and do not get anywhere:

inside file src-tauri/src/main.rs:

#[tauri::command]
fn php_to_ast(code: &str) -> String {
    match parser::parse(code) {
        Ok(ast) => {
            format!("{:?}", ast)
        }
    }
}

invoke in typescript:

invoke('php_to_ast', { code: value }).then((result) => {
            this._ast = result;
            console.log({ast: this._ast});
        }).catch((error) => {
            console.error({error});
        });

the console error message is:

error[E0004]: non-exhaustive patterns: `Err(_)` not covered
 --> src/main.rs:8:11
  |
8 |     match parser::parse(code) {
  |           ^^^^^^^^^^^^^^^^^^^ pattern `Err(_)` not covered
  |
note: `Result<Vec<php_parser_rs::parser::ast::Statement>, ParseErrorStack>` defined here
  = note: the matched value is of type `Result<Vec<php_parser_rs::parser::ast::Statement>, ParseErrorStack>`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
  |
11~         }
12+         Err(_) => todo!()
  |

I am aware that I am not fully processing the return values from parser::parse(). For comparison, in the Rust library example, the call looks like this:

fn main() -> Result<()> {
    match parser::parse(CODE) {
        Ok(ast) => {
            println!("{:#?}", ast);
        }
        Err(err) => {
            println!("{}", err.report(CODE, None, true, false)?);

            println!("parsed so far: {:#?}", err.partial);
        }
    }

    Ok(())
}

Presumably I should also still process and return the errors (as a string with an error message?).

  • https://doc.rust-lang.org/stable/book/ch06-02-match.html#matches-are-exhaustive – Jmb Aug 03 '23 at 06:19
  • ...what is your question? You seem to already understand what you need to do...if you're just looking for validation then yes, you need to handle the error case in the match expression. How you handle it depends on what you want to do. – Jared Smith Aug 03 '23 at 12:46

0 Answers0