Computing/Rust

From Cricalix.Net
Revision as of 18:22, 25 September 2023 by Cricalix (talk | contribs)

Things I forget when doing new Rust installs, or other things I want to keep track of with Rust.

  1. Install rust-analyzer via rustup. rustup component add rust-analyzer

Reqwest GET URIs

let mut req = client.get("https://www.google.com").build().unwrap();

let query: String = form_urlencoded::Serializer::new(String::new())
   .append_key_only("foo")
   .finish();
req.url_mut().set_query(Some(&query));

https://docs.rs/form_urlencoded/latest/form_urlencoded/struct.Serializer.html#method.append_pair

sudo apt install pkg-config libssl-dev

Clap for CLI handling

cargo add clap -F derive,env

Serde and Axum

Refer to things correctly in the structs. Structs need to be #[derive(Deserialize,Debug)] for Azum JSON processing to work.

#[derive(Deserialize,Debug)]
struct AisCatcherMessage {
    protocol: String,
    encodetime: String,
    stationid: String,
    receiver: AisCatcherReceiver,
    device: AisCatcherDevice,
    msgs: Vec<serde_json::Value>,
}

async fn process_ais_message(Json(payload): Json<AisCatcherMessage>) -> impl IntoResponse {
    println!("{:?}", payload);

    for msg in payload.msgs {
        println!("{}", msg["rxtime"]);
    }
    (StatusCode::CREATED, Json(json!({"message": "Processed" })))
}