use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
pub const DEFAULT_API_BASE: &str = "https://backend.decane.app";
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Claims {
pub user_id: String,
pub subject: String,
pub project_id: Option<String>,
pub token_id: Option<String>,
pub issued_at: Option<u64>,
pub expires_at: Option<u64>,
pub raw: Map<String, Value>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Addresses {
#[serde(default)]
pub evm: Option<String>,
#[serde(default)]
pub solana: Option<String>,
#[serde(default)]
pub tron: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LinkedAccountType {
Wallet,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ChainType {
Ethereum,
Solana,
Tron,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LinkedAccount {
#[serde(rename = "type")]
pub account_type: LinkedAccountType,
pub chain_type: ChainType,
pub address: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct User {
pub id: String,
pub addresses: Addresses,
pub linked_accounts: Vec<LinkedAccount>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum UserIdentifier {
Email {
value: String,
},
Phone {
value: String,
},
Custom {
value: String,
#[serde(rename = "providerId")]
provider_id: String,
},
KingsChat {
value: String,
},
X {
value: String,
},
}
impl UserIdentifier {
pub fn email(value: impl Into<String>) -> Self {
Self::Email {
value: value.into(),
}
}
pub fn phone(value: impl Into<String>) -> Self {
Self::Phone {
value: value.into(),
}
}
pub fn custom(value: impl Into<String>, provider_id: impl Into<String>) -> Self {
Self::Custom {
value: value.into(),
provider_id: provider_id.into(),
}
}
pub fn kingschat(value: impl Into<String>) -> Self {
Self::KingsChat {
value: value.into(),
}
}
pub fn x(value: impl Into<String>) -> Self {
Self::X {
value: value.into(),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateUserOptions {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub create_wallet: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub metadata: Option<RecordPatch>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WalletOutcome {
Created,
Exists,
None,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreatedUserInfo {
pub id: String,
pub auth_method: String,
#[serde(default)]
pub identifier: Option<String>,
pub addresses: Addresses,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreatedUser {
pub user: CreatedUserInfo,
pub created: bool,
pub wallet: WalletOutcome,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum RecordValue {
Bool(bool),
Int(i64),
Float(f64),
String(String),
}
impl From<bool> for RecordValue {
fn from(v: bool) -> Self {
Self::Bool(v)
}
}
impl From<i64> for RecordValue {
fn from(v: i64) -> Self {
Self::Int(v)
}
}
impl From<i32> for RecordValue {
fn from(v: i32) -> Self {
Self::Int(v.into())
}
}
impl From<f64> for RecordValue {
fn from(v: f64) -> Self {
Self::Float(v)
}
}
impl From<&str> for RecordValue {
fn from(v: &str) -> Self {
Self::String(v.to_owned())
}
}
impl From<String> for RecordValue {
fn from(v: String) -> Self {
Self::String(v)
}
}
pub type Record = BTreeMap<String, RecordValue>;
pub type RecordPatch = BTreeMap<String, Option<RecordValue>>;
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthProfile {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub picture: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthResult {
#[serde(rename = "jwt")]
pub access_token: String,
pub user_id: String,
#[serde(default)]
pub is_new_user: bool,
#[serde(default)]
pub has_share: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub profile: Option<AuthProfile>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct KingsChatConfig {
pub client_id: String,
pub authorization_url: String,
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn user_identifier_is_tagged_on_type() {
assert_eq!(
serde_json::to_value(UserIdentifier::email("Ada@Example.com")).unwrap(),
json!({"type": "email", "value": "Ada@Example.com"})
);
assert_eq!(
serde_json::to_value(UserIdentifier::custom("auth0|1", "prov-1")).unwrap(),
json!({"type": "custom", "value": "auth0|1", "providerId": "prov-1"})
);
assert_eq!(
serde_json::to_value(UserIdentifier::kingschat("kc-9")).unwrap(),
json!({"type": "kingschat", "value": "kc-9"})
);
assert_eq!(
serde_json::to_value(UserIdentifier::x("123")).unwrap(),
json!({"type": "x", "value": "123"})
);
}
#[test]
fn record_values_are_untagged_with_bool_before_int() {
let record: Record =
serde_json::from_value(json!({"shine_perps": false, "plan": "pro", "n": 1, "f": 1.5}))
.unwrap();
assert_eq!(record["shine_perps"], RecordValue::Bool(false));
assert_eq!(record["plan"], RecordValue::String("pro".into()));
assert_eq!(record["n"], RecordValue::Int(1));
assert_eq!(record["f"], RecordValue::Float(1.5));
let patch: RecordPatch = serde_json::from_value(json!({"old": null, "a": true})).unwrap();
assert_eq!(patch["old"], None);
assert_eq!(patch["a"], Some(RecordValue::Bool(true)));
assert_eq!(
serde_json::to_value(&patch).unwrap(),
json!({"a": true, "old": null})
);
}
#[test]
fn auth_result_renames_jwt_to_access_token() {
let r: AuthResult = serde_json::from_value(
json!({"jwt": "t", "userId": "u", "isNewUser": true, "hasShare": false}),
)
.unwrap();
assert_eq!(r.access_token, "t");
assert_eq!(r.profile, None);
}
#[test]
fn create_user_options_omit_unset_fields() {
assert_eq!(
serde_json::to_value(CreateUserOptions::default()).unwrap(),
json!({})
);
}
}