use std::time::Instant;
use crate::auth::device_code::{CancellationToken, DeviceCodeResponse};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Region {
Global,
China,
}
impl Region {
pub fn endpoint(self) -> &'static str {
match self {
Region::Global => "https://api.cloud.zilliz.com",
Region::China => "https://api.cloud.zilliz.com.cn",
}
}
pub fn slug(self) -> &'static str {
match self {
Region::Global => "global",
Region::China => "china",
}
}
pub fn supports_browser(self) -> bool {
matches!(self, Region::Global)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AuthMethod {
Browser,
ApiKey,
}
#[derive(Debug, Clone)]
pub struct DeviceCodeSession {
pub response: DeviceCodeResponse,
pub started_at: Instant,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WizardFocus {
Input,
Save,
}
#[derive(Debug, Clone)]
pub struct WizardState {
pub region: Option<Region>,
pub region_cursor: usize, pub method: Option<AuthMethod>,
pub method_cursor: usize, pub device_code: Option<DeviceCodeSession>,
pub cancel: CancellationToken,
pub poll_in_flight: bool,
pub api_key_buf: String,
pub api_key_visible: bool,
pub api_key_focus: WizardFocus,
pub error: Option<String>,
}
impl Default for WizardState {
fn default() -> Self {
Self {
region: None,
region_cursor: 0,
method: None,
method_cursor: 0,
device_code: None,
cancel: CancellationToken::new(),
poll_in_flight: false,
api_key_buf: String::new(),
api_key_visible: false,
api_key_focus: WizardFocus::Input,
error: None,
}
}
}
impl WizardState {
pub fn new() -> Self {
Self::default()
}
pub fn region_after_cursor(&self) -> Region {
if self.region_cursor == 0 {
Region::Global
} else {
Region::China
}
}
pub fn method_after_cursor(&self) -> AuthMethod {
if self.method_cursor == 0 {
AuthMethod::Browser
} else {
AuthMethod::ApiKey
}
}
}