use serde::{Deserialize, Serialize, de};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SafetySetting {
pub category: HarmCategory,
pub threshold: HarmBlockThreshold,
}
#[derive(Debug, Clone, Serialize, PartialEq)]
pub enum HarmCategory {
#[serde(rename = "HARM_CATEGORY_UNSPECIFIED")]
Unspecified,
#[serde(rename = "HARM_CATEGORY_DEROGATORY")]
Derogatory,
#[serde(rename = "HARM_CATEGORY_TOXICITY")]
Toxicity,
#[serde(rename = "HARM_CATEGORY_VIOLENCE")]
Violence,
#[serde(rename = "HARM_CATEGORY_SEXUAL")]
Sexual,
#[serde(rename = "HARM_CATEGORY_MEDICAL")]
Medical,
#[serde(rename = "HARM_CATEGORY_DANGEROUS")]
Dangerous,
#[serde(rename = "HARM_CATEGORY_HARASSMENT")]
Harassment,
#[serde(rename = "HARM_CATEGORY_HATE_SPEECH")]
HateSpeech,
#[serde(rename = "HARM_CATEGORY_SEXUALLY_EXPLICIT")]
SexuallyExplicit,
#[serde(rename = "HARM_CATEGORY_DANGEROUS_CONTENT")]
DangerousContent,
#[serde(rename = "HARM_CATEGORY_CIVIC_INTEGRITY")]
CivicIntegrity,
#[serde(rename = "HARM_CATEGORY_JAILBREAK")]
Jailbreak,
}
impl HarmCategory {
fn from_wire_str(value: &str) -> Self {
match value {
"HARM_CATEGORY_UNSPECIFIED" => Self::Unspecified,
"HARM_CATEGORY_DEROGATORY" => Self::Derogatory,
"HARM_CATEGORY_TOXICITY" => Self::Toxicity,
"HARM_CATEGORY_VIOLENCE" => Self::Violence,
"HARM_CATEGORY_SEXUAL" => Self::Sexual,
"HARM_CATEGORY_MEDICAL" => Self::Medical,
"HARM_CATEGORY_DANGEROUS" => Self::Dangerous,
"HARM_CATEGORY_HARASSMENT" => Self::Harassment,
"HARM_CATEGORY_HATE_SPEECH" => Self::HateSpeech,
"HARM_CATEGORY_SEXUALLY_EXPLICIT" => Self::SexuallyExplicit,
"HARM_CATEGORY_DANGEROUS_CONTENT" => Self::DangerousContent,
"HARM_CATEGORY_CIVIC_INTEGRITY" => Self::CivicIntegrity,
"HARM_CATEGORY_JAILBREAK" => Self::Jailbreak,
_ => Self::Unspecified,
}
}
fn from_wire_number(value: i64) -> Self {
match value {
0 => Self::Unspecified,
1 => Self::HateSpeech,
2 => Self::DangerousContent,
3 => Self::Harassment,
4 => Self::SexuallyExplicit,
5 => Self::CivicIntegrity,
6 => Self::Jailbreak,
_ => Self::Unspecified,
}
}
}
impl<'de> Deserialize<'de> for HarmCategory {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = serde_json::Value::deserialize(deserializer)?;
match value {
serde_json::Value::String(s) => Ok(Self::from_wire_str(&s)),
serde_json::Value::Number(n) => {
n.as_i64().map(Self::from_wire_number).ok_or_else(|| {
de::Error::custom("harm category must be an integer-compatible number")
})
}
_ => Err(de::Error::custom("harm category must be a string or integer")),
}
}
}
#[allow(clippy::enum_variant_names)]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum HarmBlockThreshold {
HarmBlockThresholdUnspecified,
BlockLowAndAbove,
BlockMediumAndAbove,
BlockOnlyHigh,
BlockNone,
Off,
}
#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum HarmProbability {
HarmProbabilityUnspecified,
Negligible,
Low,
Medium,
High,
}
impl HarmProbability {
fn from_wire_str(value: &str) -> Self {
match value {
"HARM_PROBABILITY_UNSPECIFIED" => Self::HarmProbabilityUnspecified,
"NEGLIGIBLE" => Self::Negligible,
"LOW" => Self::Low,
"MEDIUM" => Self::Medium,
"HIGH" => Self::High,
_ => Self::HarmProbabilityUnspecified,
}
}
fn from_wire_number(value: i64) -> Self {
match value {
0 => Self::HarmProbabilityUnspecified,
1 => Self::Negligible,
2 => Self::Low,
3 => Self::Medium,
4 => Self::High,
_ => Self::HarmProbabilityUnspecified,
}
}
}
impl<'de> Deserialize<'de> for HarmProbability {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = serde_json::Value::deserialize(deserializer)?;
match value {
serde_json::Value::String(s) => Ok(Self::from_wire_str(&s)),
serde_json::Value::Number(n) => {
n.as_i64().map(Self::from_wire_number).ok_or_else(|| {
de::Error::custom("harm probability must be an integer-compatible number")
})
}
_ => Err(de::Error::custom("harm probability must be a string or integer")),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SafetyRating {
pub category: HarmCategory,
pub probability: HarmProbability,
}