use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::{FontspectorError, Testable};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FixResult {
Available,
Unfixable,
MoreInfoNeeded(MoreInfoRequest),
Fixed,
NotBroken,
FixFailed(String),
}
impl FixResult {
pub fn is_success(&self) -> bool {
matches!(self, FixResult::Fixed | FixResult::NotBroken)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MoreInfoRequest(pub Vec<DialogField>);
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DialogField {
pub key: String,
pub prompt: String,
pub field_type: DialogFieldType,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Choice {
pub value: String,
pub description: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DialogFieldType {
Choice(Vec<Choice>),
Text,
Number,
Boolean,
}
impl DialogField {
pub fn new_choice(key: &str, prompt: &str, options: Vec<(&str, &str)>) -> Self {
DialogField {
key: key.to_string(),
prompt: prompt.to_string(),
field_type: DialogFieldType::Choice(
options
.into_iter()
.map(|(value, description)| Choice {
value: value.to_string(),
description: description.to_string(),
})
.collect(),
),
}
}
pub fn new_text(key: &str, prompt: &str) -> Self {
DialogField {
key: key.to_string(),
prompt: prompt.to_string(),
field_type: DialogFieldType::Text,
}
}
pub fn new_number(key: &str, prompt: &str) -> Self {
DialogField {
key: key.to_string(),
prompt: prompt.to_string(),
field_type: DialogFieldType::Number,
}
}
pub fn new_boolean(key: &str, prompt: &str) -> Self {
DialogField {
key: key.to_string(),
prompt: prompt.to_string(),
field_type: DialogFieldType::Boolean,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct MoreInfoReplies(pub HashMap<String, Value>);
pub type HotfixFunction =
dyn Fn(&mut Testable, Option<MoreInfoReplies>) -> Result<FixResult, FontspectorError>;