Example 1


Rust adjustments for Example "Get Node Information".


What adjustments do I need to make in Rust?

In summary, here are the steps you need to take to create the API function.

Cargo.toml

Add these crates to the existing ones.

[dependencies]
iota-client = { version = "2.0.1-rc" }

tokio = { version = "1.28.2", features = ["full"] }
anyhow = { version = "1.0.71" }

# Serialization/Deserialization
serde = { version = "1.0.164", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.97", default-features = false }

api.rs - Used Paths

use iota_client::Client;
use tokio::runtime::Runtime;
use anyhow::Result;

api.rs - Struct NetworkInfo

This struct bundles various network information. Through code generation of the Flutter-Rust-Bridge, the struct is translated into a Flutter class and becomes part of the bridge_definitions.dart file.

Therefore, it is not necessary to explicitly create it in Flutter! On the other hand, this means that the code generator needs to be executed once for the class to be seamlessly used in Flutter without any errors.

#[derive(Debug, Clone)]
pub struct NetworkInfo {
    pub node_url: String,
    pub faucet_url: String,
}

api.rs - Function get_node_info()

#[allow(dead_code)]
pub fn get_node_info(network_info: NetworkInfo) -> Result<String> {

    let rt = Runtime::new().unwrap();
    rt.block_on(async {

        let node_url = network_info.node_url;

        // Create a client with that node.
        let client = Client::builder()
            .with_node(&node_url)?
            .with_ignore_node_health()
            .finish()?;

        // Get node info.
        let info = client.get_info().await?;

        Ok(serde_json::to_string_pretty(&info).unwrap())
        //Ok(info.node_info.base_token.name)
    })

}

Checks using cargo build


All checks should work without any issue. Please also refer to the corresponding video (2023-09-03: ToDo).


New: Checks using cargo build

In the next subchapters, we will test individually for each example whether our library can be cross-compiled to the target platforms Android, macOS, and iOS.

This way, we can independently test the correctness of the dependencies in Cargo.toml, and the paths and the syntax of our Rust Code in api.rs, regardless of the Flutter build process.

πŸ‘‰ Β  How to manually cross-compile to a target of your choice

To examine the various targets, you should navigate from playground_app root directory to the rust directory:

cd rust

Then, within the rust directory, excute the following commands.

Android

If you haven't already, install the cargo-ndk command using:

cargo install cargo-ndk

I only check the ABI arm64-v8a.

cargo ndk -t arm64-v8a build

macOS

cargo build --target aarch64-apple-darwin

iOS Simulator or iOS Device

cargo build --target aarch64-apple-ios-sim
cargo build --target aarch64-apple-ios