use serde::Serialize;
use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Selection {
Unconfigured,
Lmm,
Other,
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Intent {
AddOnly,
Activate,
Default,
Maintain,
}
#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Activation {
Preserve,
ProposeLmm,
}
#[derive(Debug, Error, PartialEq, Eq)]
pub enum PlanError {
#[error("无法确认当前服务商;需要先读取选定实例的实际配置")]
UnknownSelection,
#[error("应用授权已撤销;需要明确确认重新授权")]
RevokedGrant,
#[error("付费验证需要明确同意、已知报价和服务端可执行的消费上限")]
PaidVerificationNotAuthorized,
}
pub fn activation(selection: Selection, intent: Intent) -> Result<Activation, PlanError> {
if matches!(intent, Intent::AddOnly | Intent::Maintain) {
return Ok(Activation::Preserve);
}
if selection == Selection::Unknown {
return Err(PlanError::UnknownSelection);
}
if intent == Intent::Activate || selection == Selection::Unconfigured {
Ok(Activation::ProposeLmm)
} else {
Ok(Activation::Preserve)
}
}
pub fn check_revocation(revoked: bool, explicitly_reauthorize: bool) -> Result<(), PlanError> {
if revoked && !explicitly_reauthorize {
Err(PlanError::RevokedGrant)
} else {
Ok(())
}
}
pub fn check_paid_verification(
consented: bool,
quoted_micro_usd: Option<u64>,
reserved_micro_usd: Option<u64>,
) -> Result<(), PlanError> {
match (consented, quoted_micro_usd, reserved_micro_usd) {
(true, Some(quote), Some(limit)) if limit > 0 && quote <= limit => Ok(()),
_ => Err(PlanError::PaidVerificationNotAuthorized),
}
}
#[derive(Debug, Serialize)]
pub struct Preview {
pub software: &'static str,
pub outcome: &'static str,
pub changes: Vec<&'static str>,
pub account: &'static str,
pub activation: &'static str,
pub model_change: bool,
pub paid_test: bool,
pub restart: bool,
pub blockers: Vec<&'static str>,
}
impl Preview {
pub fn blocked(software: &'static str, intent: Intent) -> Self {
Self {
software,
outcome: "blocked",
changes: vec![],
account: "not_checked",
activation: if intent == Intent::Activate {
"requested_not_applied"
} else {
"preserve"
},
model_change: false,
paid_test: false,
restart: false,
blockers: vec![
"此版本尚无经过验证的目标软件写入适配器",
"CLI 登录只允许读取目录和余额;目标应用独立授权尚未接入",
"未验证软件版本、配置管理关系及实际调用路径",
],
}
}
}