I have an app in Rust where I want to get a token from a DB server and would store it in a global variable for reusing. I cannot get it running as I have a compiler error, please help me sort it out. I am stuck with it. The code looks like this:
use lazy_static::lazy_static;
use reqwest::{
self,
header::{ACCEPT, CONTENT_TYPE},
Client,
};
use serde::{Deserialize, Serialize};
use std::{
env,
};
#[derive(Serialize, Deserialize, Debug)]
struct GeneratedToken {
app_name: String,
access_token: String,
dtable_uuid: String,
dtable_server: String,
dtable_socket: String,
dtable_db: String,
workspace_id: i32,
dtable_name: String,
}
impl Default for GeneratedToken {
fn default() -> GeneratedToken {
GeneratedToken {
app_name: "".to_string(),
access_token: "".to_string(),
dtable_uuid: "".to_string(),
dtable_server: "".to_string(),
dtable_socket: "".to_string(),
dtable_db: "".to_string(),
workspace_id: 0,
dtable_name: "".to_string(),
}
}
}
lazy_static! {
static ref SEATABLE_BASE_URL: &'static str = "https://cloud.seatable.io/api/v2.1/";
static ref API_TOKEN: &'static str = "token...";
static ref CLIENT: Client = reqwest::Client::new();
static ref GENERATED_TOKEN: GeneratedToken = GeneratedToken::default();
}
static mut HAS_TOKEN: bool = false;
async fn get_token() -> Result<(), Box<dyn std::error::Error>> {
unsafe {
GENERATED_TOKEN = CLIENT
.get(format!("{}dtable/app-access-token/", *SEATABLE_BASE_URL))
.bearer_auth(format!("Bearer {}", *API_TOKEN))
.header(CONTENT_TYPE, "application/json")
.header(ACCEPT, "application/json")
.send()
.await?
.json::<GeneratedToken>()
.await?;
HAS_TOKEN = true;
print!("Received token: {}", GENERATED_TOKEN.access_token);
}
Ok(())
}
fn main() {
unsafe {
if !HAS_TOKEN {
get_token();
}
}
}
On the GENERATED_TOKEN = CLIENT
line and the block it follows the compiler throws this error:
'?' operator cannot convert from 'GeneratedToken' to 'GENERATED_TOKEN'