Using reqwest, I'm trying to convert the response into a structure. I'm following the example from this site.
My file: twilio.rs, I have the following code
//use serde::{Serialize, Deserialize};
use serde_json::json;
use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Serialize, Deserialize)]
pub struct TwilioResponse {
body: String,
...
}
pub mod twilio_service{
pub struct Twilio {
url: String,
...
}
impl Twilio {
pub fn new(url: String, phone_number: String, account_sid: String, auth_token: String, message: String, send_to: String) -> Twilio {
Twilio {
url,
phone_number,
account_sid,
auth_token,
message,
send_to
}
}
pub async fn send(&self) {
let url = format!("{}{}{}", self.url, self.account_sid, "/Messages.json");
let client = reqwest::Client::new();
let res = client.post(url)
.basic_auth(&self.account_sid, Some(&self.auth_token))
.header("Content-Type", "application/x-www-form-urlencoded")
.form(&[("From", &self.phone_number), ("To", &self.send_to), ("Body", &self.message)])
.send()
.await
.unwrap();
let text_response = res.text().await.unwrap();
let json: twilio::TwilioResponse = serde_json::from_str(&text_response).unwrap();
}
}
}
The problem I have is when I compile the app, I get the following error:
let json: twilio::TwilioResponse = serde_json::from_str(&text_response).unwrap();
^^^^^^ use of undeclared crate or module `twilio`
If I modify the code to:
let json: TwilioResponse = serde_json::from_str(&text_response).unwrap();
I then get the error
let json: TwilioResponse = serde_json::from_str(&text_response).unwrap();
^^^^^^^^^^^^^^ not found in this scope
This is my first attempt in Rust and needing advise. Thanks
The problem with your first attempt is that a bare twilio
in the use refers to either a crate or a submodule of the current module or some other imported symbol, neither of which exist.
Depending on where you declare the twilio
module there are a couple of ways to import it.
twilio_service
is a submodule of twilio
you can use the super
keyword, which means the module above this one:
use super::TwilioResponse;
twilio
is directly declared in your lib.rs
or main.rs
you can use crate
to refer to the crate root and specify the full path from there:
use crate::possible_more_modules::twilio::TwilioResponse;
There is a third possibility, you actually meant to use a crate named twilio
if so you have to add that to your [dependencies]
in Cargo.toml
:
[dependencies]
twilio = "*"