Computing/Rust: Difference between revisions
From Cricalix.Net
(Created page with "Things I forget when doing new Rust installs, or other things I want to keep track of with Rust. # Install rust-analyzer via rustup. <code>rustup component add rust-analyzer</code> ==== Reqwest GET URIs ==== <syntaxhighlight lang="rust"> 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)); </syntaxhighl...") |
mNo edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 11: | Line 11: | ||
.finish(); | .finish(); | ||
req.url_mut().set_query(Some(&query)); | req.url_mut().set_query(Some(&query)); | ||
</syntaxhighlight> | </syntaxhighlight>https://docs.rs/form_urlencoded/latest/form_urlencoded/struct.Serializer.html#method.append_pair | ||
<code>sudo apt install pkg-config libssl-dev</code> | |||
==== Clap for CLI handling ==== | ==== Clap for CLI handling ==== | ||
<code>cargo add clap -F derive,env</code> | <code>cargo add clap -F derive,env</code> | ||
==== 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. 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)] | |||
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" }))) | |||
} | |||
</syntaxhighlight> |
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.
- 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" })))
}