I have some errors for the following code and I was wondering how I can solve them?
error: chained comparison operators require parentheses
--> src/main.rs:22:31
|
22 | if msg.text() == send_set_correction_pattern_deltas.msg[data][command] == get {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0425]: cannot find value `send_set_correction_pattern_deltas` in this scope
--> src/main.rs:22:34
|
22 | if msg.text() == send_set_correction_pattern_deltas.msg[data][command] == get {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find value `data` in this scope
--> src/main.rs:22:73
|
22 | if msg.text() == send_set_correction_pattern_deltas.msg[data][command] == get {
| ^^^^ not found in this scope
error[E0425]: cannot find value `command` in this scope
--> src/main.rs:22:79
|
22 | if msg.text() == send_set_correction_pattern_deltas.msg[data][command] == get {
| ^^^^^^^ not found in this scope
error[E0425]: cannot find value `get` in this scope
--> src/main.rs:22:91
|
22 | if msg.text() == send_set_correction_pattern_deltas.msg[data][command] == get {
| ^^^ not found in this scope
warning: unused import: `rustc_serialize::json::Json`
--> src/main.rs:3:5
|
3 | use rustc_serialize::json::Json;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0425`.
error: could not compile `slm_controller_2`.
To learn more, run the command again with --verbose.
extern crate mosquitto_client as mosq;
extern crate rustc_serialize;
use rustc_serialize::json::Json;
use mosq::Mosquitto;
use serde_json::json;
fn on_connect() -> mosq::Mosquitto {
let m = Mosquitto::new("test");
m.connect("broker.hivemq.com",1883).expect("can't connect");
m
}
fn on_message(m: & mosq::Mosquitto, gui: &mut mosq::TopicMatcher<'_>) {
let mut mc = m.callbacks(());
mc.on_message(|_,msg| {
if ! msg.retained() {
if gui.matches(&msg) {
println!("message recieved= {}",msg.text());
println!("message topic= {}", msg.topic());
println!("message qos= {}", msg.qos());
println!("message retain flag= {}\n", msg.retained());
if msg.text() == send_set_correction_pattern_deltas.msg[data][command] == get {
println!("hi");
}
}
}
});
m.loop_forever(200).expect("broken loop");
}
fn main() {
let m = on_connect();
let mut gui = m.subscribe("gui/aim",0).expect("can't subscribe to gui");
let mut send_set_correction_pattern_deltas_msg_dict = json!({
"type": "device",
"data": {
"device": "aim",
"command": "get",
}
});
let send_set_correction_pattern_deltas_msg = send_set_correction_pattern_deltas_msg_dict.to_string();
m.publish("gui/aim", send_set_correction_pattern_deltas_msg.as_bytes(), 1, false).unwrap();
on_message(&m, &mut gui);
}
If you have trouble understand the error message, here are some tips:
cannot find value `some_name_here` in this scope
means that you are talking about a value called some_name_here that the compiler cannot find, thus it fails. To fix this, you need to make that value available.
The chained comparison operators require parentheses error is due to you having multiple == in a single comparison. You should figure out what you actually want the code to do here, and update the code accordingly.
I don't know what the type is. If you wish to pass it by argument, try defining an argument with the type u32. This will cause it to fail, and the error message will tell you what the type should be.
Note that the thing that isn't in scope is send_set_correction_pattern_deltas, not the entire equality expression.
Compiling slm_controller_2 v0.1.0 (/home/pi/rust/slm_controller_2)
error: chained comparison operators require parentheses
--> src/main.rs:22:32
|
22 | if (msg.text() == send_set_correction_pattern_deltas.msg[data][command] == "get") {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0425]: cannot find value `data` in this scope
--> src/main.rs:22:74
|
22 | if (msg.text() == send_set_correction_pattern_deltas.msg[data][command] == "get") {
| ^^^^ not found in this scope
error[E0425]: cannot find value `command` in this scope
--> src/main.rs:22:80
|
22 | if (msg.text() == send_set_correction_pattern_deltas.msg[data][command] == "get") {
| ^^^^^^^ not found in this scope
warning: unused import: `rustc_serialize::json::Json`
--> src/main.rs:3:5
|
3 | use rustc_serialize::json::Json;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: unnecessary parentheses around `if` condition
--> src/main.rs:22:20
|
22 | if (msg.text() == send_set_correction_pattern_deltas.msg[data][command] == "get") {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses
|
= note: `#[warn(unused_parens)]` on by default
error[E0609]: no field `msg` on type `&u32`
--> src/main.rs:22:70
|
22 | if (msg.text() == send_set_correction_pattern_deltas.msg[data][command] == "get") {
| ^^^
error[E0277]: can't compare `bool` with `&str`
--> src/main.rs:22:89
|
22 | if (msg.text() == send_set_correction_pattern_deltas.msg[data][command] == "get") {
| ^^ no implementation for `bool == &str`
|
= help: the trait `std::cmp::PartialEq<&str>` is not implemented for `bool`
error[E0061]: this function takes 3 parameters but 2 parameters were supplied
--> src/main.rs:49:5
|
13 | fn on_message(m: &mosq::Mosquitto, gui: &mut mosq::TopicMatcher<'_>, send_set_correction_pattern_deltas: &u32) {
| -------------------------------------------------------------------------------------------------------------- defined here
...
49 | on_message(&m, &mut gui);
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected 3 parameters
error: aborting due to 6 previous errors
Some errors have detailed explanations: E0061, E0277, E0425, E0609.
For more information about an error, try `rustc --explain E0061`.
error: could not compile `slm_controller_2`.
The type for send_set_correction_pattern_deltas is not specified and also [data] and [command] is not in scope.
Ok I have the typing but I still have the following problems.
Compiling slm_controller_2 v0.1.0 (/home/pi/rust/slm_controller_2)
error: chained comparison operators require parentheses
--> src/main.rs:22:32
|
22 | if (msg.text() == send_set_correction_pattern_deltas_msg[data][command] == "get") {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0425]: cannot find value `data` in this scope
--> src/main.rs:22:74
|
22 | if (msg.text() == send_set_correction_pattern_deltas_msg[data][command] == "get") {
| ^^^^ not found in this scope
error[E0425]: cannot find value `command` in this scope
--> src/main.rs:22:80
|
22 | if (msg.text() == send_set_correction_pattern_deltas_msg[data][command] == "get") {
| ^^^^^^^ not found in this scope
warning: unused import: `rustc_serialize::json::Json`
--> src/main.rs:3:5
|
3 | use rustc_serialize::json::Json;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: unnecessary parentheses around `if` condition
--> src/main.rs:22:20
|
22 | if (msg.text() == send_set_correction_pattern_deltas_msg[data][command] == "get") {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses
|
= note: `#[warn(unused_parens)]` on by default
error[E0277]: can't compare `bool` with `&str`
--> src/main.rs:22:89
|
22 | if (msg.text() == send_set_correction_pattern_deltas_msg[data][command] == "get") {
| ^^ no implementation for `bool == &str`
|
= help: the trait `std::cmp::PartialEq<&str>` is not implemented for `bool`
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0277, E0425.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `slm_controller_2`.
msg.text() refers to the message part of the mqtt message. If the message is equal to send_set_correction_pattern_deltas_msg[data][command] == "get" which is a field inside the "send_set_correction_pattern_deltas_msg" json then print a message "hi".
Inside this json there is a get command and if it equals this command then print a message "hi".
Either way if [data][command] = "get" then print a "hi" message.
You can use serde_json::from_str to create a serde_json::value::Value, then use the methods on that type (see linked doc) to extract command:
use serde_json::value::Value;
let value: Value = serde_json::from_str(msg.text()).expect("not valid json");
println!("{:?}", value["data"]["command"]);
if value["data"]["command"].as_str().expect("not string") == "get" {
...
}