use serde_json::Value;
use crate::error::{ApiError, Error};
use crate::jwt::decode_jwt;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub enum Message {
Text(String),
Struct(Value),
Encrypted(String),
}
impl TryFrom<Message> for String {
type Error = ApiError;
fn try_from(message: Message) -> Result<Self, Self::Error> {
match message {
Message::Text(text) => Ok(text),
Message::Struct(value) => {
if let Ok(json) = serde_json::to_string_pretty(&value) {
Ok(json)
} else {
Ok(value.to_string())
}
}
Message::Encrypted(token) => {
let hmac_secret =
std::env::var("JWT_SECRET").map_err(|_| Error::NoJWTSecretFound)?;
let prompt = decode_jwt(&token, &hmac_secret).map(|c| c.prompt)?;
Ok(prompt)
}
}
}
}