diff options
| author | sigoden <sigoden@gmail.com> | 2023-11-01 10:28:54 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-01 10:28:54 +0800 |
| commit | 7f2210dbcab017ee30e8f95771e68494b4860c59 (patch) | |
| tree | 7a0c7bdc8f6930e262bf8cfdc8ffbd57aa843e40 /src/utils/prompt_input.rs | |
| parent | 64202758ec8693dd18951c554728bb5c6b7e5eeb (diff) | |
| download | aichat-7f2210dbcab017ee30e8f95771e68494b4860c59.tar.gz | |
refactor: clients/* and config.rs (#193)
- add register_clients macro to make it easier to add a new client
- no create_client_config, just add const PROMPTS
- move ModelInfo from clients/ to config/
- model's max_tokens are optional
- improve code quanity on config/mod.rs
- add/use macro config_get_fn
Diffstat (limited to 'src/utils/prompt_input.rs')
| -rw-r--r-- | src/utils/prompt_input.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/utils/prompt_input.rs b/src/utils/prompt_input.rs new file mode 100644 index 0000000..f63823a --- /dev/null +++ b/src/utils/prompt_input.rs @@ -0,0 +1,58 @@ +use inquire::{required, validator::Validation, Text}; + +const MSG_REQUIRED: &str = "This field is required"; +const MSG_OPTIONAL: &str = "Optional field - Press ↵ to skip"; + +pub fn prompt_input_string(desc: &str, required: bool) -> anyhow::Result<String> { + let mut text = Text::new(desc); + if required { + text = text.with_validator(required!(MSG_REQUIRED)) + } else { + text = text.with_help_message(MSG_OPTIONAL) + } + text.prompt().map_err(prompt_op_err) +} + +pub fn prompt_input_integer(desc: &str, required: bool) -> anyhow::Result<String> { + let mut text = Text::new(desc); + if required { + text = text.with_validator(|text: &str| { + let out = if text.is_empty() { + Validation::Invalid(MSG_REQUIRED.into()) + } else { + validate_integer(text) + }; + Ok(out) + }) + } else { + text = text + .with_validator(|text: &str| { + let out = if text.is_empty() { + Validation::Valid + } else { + validate_integer(text) + }; + Ok(out) + }) + .with_help_message(MSG_OPTIONAL) + } + text.prompt().map_err(prompt_op_err) +} + +pub fn prompt_op_err<T>(_: T) -> anyhow::Error { + anyhow::anyhow!("Not finish questionnaire, try again later!") +} + +#[derive(Debug, Clone, Copy)] +pub enum PromptKind { + String, + Integer, +} + +fn validate_integer(text: &str) -> Validation { + if text.parse::<i32>().is_err() { + Validation::Invalid("Must be a integer".into()) + } else { + Validation::Valid + } +} |
