Get function signature of an imported library

Hello,

How can I get a function signature of an imported library function? I'm currently trying to parse the source file using the "syn" crate (let syntax = syn::parse_file(&src).expect("Unable to parse file");) and do operations based on function's arguments types.

I need a generic way to do this, like how the rust-analyzer / rls help in code completion based on the imported / use functions.

For Ex: In the following code, how to get the function signature of "rsa.private_key_to_pem_passphrase"

extern crate openssl;

use openssl::rsa::Rsa;
use openssl::symm::Cipher;

fn main() {
    let passphrase = "rust_by_example";

    let rsa = Rsa::generate(1024).unwrap();
    let private_key: Vec<u8> = rsa.private_key_to_pem_passphrase(Cipher::aes_128_cbc(), passphrase.as_bytes()).unwrap();
    let public_key: Vec<u8> = rsa.public_key_to_pem().unwrap();

    println!("Private key: {}", String::from_utf8(private_key).unwrap());
    println!("Public key: {}", String::from_utf8(public_key).unwrap());
}

Thanks for your time.

What do you mean by "imported function" - what are you making here?

For a command line tool, it will be rather complex - I think you will need to access Cargo via one of https://docs.rs/cargo or https://docs.rs/cargo-metadata (I'm not sure which), and then partially compile the crate using Rustc's rustc-ap-* crates on crates.io.

To follow on Kestrer's comment, you could use rust-analyzer's crates instead of rustc-ap-* (ra_ap_* on crates.io). These may be easier to understand and manipulate.

Note that in either case, the crates are very unstable and (IMO) rather obscure: they can be used in other projects than their own, but that's not their primary goal (in contrast to syn). Unfortunately there is no standart way to analyze Rust code this way.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.