zilliz 1.4.3

TUI and CLI tool for managing Zilliz Cloud clusters and Milvus operations
Documentation
//! In-TUI sign-in wizard state.
//!
//! The wizard is a flat struct on `App`. We keep one optional `WizardState`
//! and let each screen read/mutate the fields it cares about.

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,
}

/// One in-flight device-code session. Holds the response + the polling start
/// time so the view can render a countdown.
#[derive(Debug, Clone)]
pub struct DeviceCodeSession {
    pub response: DeviceCodeResponse,
    pub started_at: Instant,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WizardFocus {
    /// Typing into the API-key field.
    Input,
    /// Field defocused. The key is always saved; this state exists so the user
    /// can step off the input to use the `b` (back) / `?` (help) shortcuts
    /// without those characters being typed into the key.
    Save,
}

#[derive(Debug, Clone)]
pub struct WizardState {
    pub region: Option<Region>,
    pub region_cursor: usize, // 0 = Global, 1 = China
    pub method: Option<AuthMethod>,
    pub method_cursor: usize, // 0 = Browser, 1 = ApiKey
    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
        }
    }
}