use rho_providers::{model::catalog::LoginTarget, provider::AuthMode};
use super::{
composer_chrome,
line_editor::LineEditor,
line_editor_view::EditorPresentation,
view_composer::{editor_frame, ComposerFrame},
};
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum ApiKeySubmission {
Save { target: LoginTarget, key: String },
LeaveUnset { target: LoginTarget },
Reuse {
target: LoginTarget,
source: AuthMode,
},
Rejected,
}
#[derive(Clone, Copy, Debug)]
pub(super) enum BlankKey {
Reject,
LeaveUnset,
Reuse(AuthMode),
}
#[derive(Clone, Debug)]
pub(super) struct SecretInput {
pub(super) target: LoginTarget,
pub(super) editor: LineEditor,
pub(super) blank: BlankKey,
}
impl SecretInput {
pub(super) fn new(target: LoginTarget) -> Self {
Self {
target,
editor: LineEditor::new(""),
blank: BlankKey::Reject,
}
}
pub(super) fn optional(target: LoginTarget) -> Self {
Self::with_blank(target, BlankKey::LeaveUnset)
}
pub(super) fn with_blank(target: LoginTarget, blank: BlankKey) -> Self {
Self {
blank,
..Self::new(target)
}
}
pub(super) fn prompt(&self) -> String {
match self.blank {
BlankKey::Reject => format!("enter {}", self.target.label),
BlankKey::LeaveUnset => "enter API key (optional)".into(),
BlankKey::Reuse(source) => format!(
"enter {} or leave blank to use your {}",
self.target.label, source.login_label
),
}
}
pub(super) fn submission(&self) -> ApiKeySubmission {
let key = self.editor.value.trim().to_string();
let target = self.target.clone();
if !key.is_empty() {
return ApiKeySubmission::Save { target, key };
}
match self.blank {
BlankKey::Reject => ApiKeySubmission::Rejected,
BlankKey::LeaveUnset => ApiKeySubmission::LeaveUnset { target },
BlankKey::Reuse(source) => ApiKeySubmission::Reuse { target, source },
}
}
}
pub(super) fn secret_input_frame(secret: &SecretInput, width: usize) -> ComposerFrame {
let prompt = format!(
"{} {}",
secret.prompt(),
composer_chrome::join_footer_parts(["Enter save", "Esc cancel"])
);
editor_frame(
&prompt,
secret.editor.viewport(EditorPresentation::Masked, width),
width,
)
}
#[cfg(test)]
#[path = "login_secret_input_tests.rs"]
mod tests;