Computing/Rust: Difference between revisions

From Cricalix.Net
mNo edit summary
mNo edit summary
 
Line 18: Line 18:
<code>cargo add clap -F derive,env</code>
<code>cargo add clap -F derive,env</code>


Serde and Axum
==== Serde and Axum ====


Refer to things correctly in the structs. Structs need to be <code>#[derive(Deserialize,Debug)]</code> for Azum JSON processing to work.<syntaxhighlight lang="rust">
Refer to things correctly in the structs. Structs need to be <code>#[derive(Deserialize,Debug)]</code> for Azum JSON processing to work. Avoid the rawvalue stuff if possible, because lifetimes become a problem and the blocking error is <code>higher-ranked lifetime error</code> pointing at the <code>post()</code> handler in the Axum routing block of code.<syntaxhighlight lang="rust">
#[derive(Deserialize,Debug)]
#[derive(Deserialize,Debug)]
struct AisCatcherMessage {
struct AisCatcherMessage {

Latest revision as of 18:24, 25 September 2023

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. Avoid the rawvalue stuff if possible, because lifetimes become a problem and the blocking error is higher-ranked lifetime error pointing at the post() handler in the Axum routing block of code.

#[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" })))
}