I have some errors for the following code and I was wondering how I can solve them?

Hi

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);
}

Thanks

Please read this post of mine again and edit your post. (do not post a new topic)

Additionally please use backtics for your code instead of four spaces. If you do this, your code is syntax highlighted.

How can I solve the problems?

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.

How do I bring send_set_correction_pattern_deltas.msg[data][command] == "get" into scope. What would be the type etc.

Thanks

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.

Notice here

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

For the compiler to figure out what the type is for you, you have to actually pass it as an argument in the spot with the u32 type.

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`.

Can you explain what you want this to do, because I can't guess it?

if (msg.text() == send_set_correction_pattern_deltas_msg[data][command] == "get") {

You have two comparisons, which doesn't make much sense.

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".

This is not a field: send_set_correction_pattern_deltas_msg[data][command] == "get"

It is a comparison.

let mut send_set_correction_pattern_deltas_msg_dict = json!({
    "type": "device",
    "data": {
        "device": "aim",
        "command": "get",
    }
});

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.

Note that rustc-serialize is deprecated. You should probably be using serde to deserialize the output of msg.text(). There are some examples here.

Ok but how filter/parse this json though? How do I execute this boolean and if true how do I print out a 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" {
    ...
}

Do I still need input function arguments in the on_message function?

Seems like the answer is no, since you got it from msg.text(), not whoever called you.

Thank you so much for everything. This has worked now for what I needed.

1 Like

Hi

Why do I get this error for the following code?

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: variable does not need to be mutable
  --> src/main.rs:37:9
   |
37 |     let mut send_get_lasers_msg_dict = json!({
   |         ----^^^^^^^^^^^^^^^^^^^^^^^^
   |         |
   |         help: remove this `mut`
   |
   = note: `#[warn(unused_mut)]` on by default

    Finished dev [unoptimized + debuginfo] target(s) in 4.73s
     Running `target/debug/slm_controller_2`
message recieved= hi
message topic= hi/asas
message qos= 0
message retain flag= false

thread 'main' panicked at 'not valid json: Error("expected value", line: 1, column: 1)', src/libcore/result.rs:1188:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
fatal runtime error: failed to initiate panic, error 9
Aborted
extern crate mosquitto_client as mosq;
extern crate rustc_serialize;
use rustc_serialize::json::Json;
use mosq::Mosquitto;
use serde_json::json;
use serde_json::value::Value;

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) {
    let mut mc = m.callbacks(());
    mc.on_message(|_,msg| {
        println!("message recieved= {}",msg.text());
        println!("message topic= {}", msg.topic());
        println!("message qos= {}", msg.qos());
        println!("message retain flag= {}\n", msg.retained());
        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" {
            println!("hi");
        }
    });
    
    m.loop_forever(200).expect("broken loop");
}

fn main() {
    let m = on_connect();
    
    m.subscribe("gui/aim",0).expect("can't subscribe to gui");
    m.subscribe("hi/asas",0).expect("can't subscribe to gui");
    
    let mut send_get_lasers_msg_dict = json!({
        "type": "device",
        "data": {
            "device": "lasers",
            "command": "get",
        }
    });
    
    let send_get_lasers_msg = send_get_lasers_msg_dict.to_string();        
        
    m.publish("gui/aim", send_get_lasers_msg.as_bytes(), 0, false).unwrap();
    m.publish("hi/asas", "hi".as_bytes(), 0, false).unwrap();
    
    on_message(&m);
}

Thanks