use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "kebab-case")]
#[non_exhaustive]
pub enum Warning {
Unsupported {
feature: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
details: Option<String>,
},
Compatibility {
feature: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
details: Option<String>,
},
Deprecated {
setting: String,
message: String,
},
Other {
message: String,
},
}
impl Warning {
#[must_use]
pub fn unsupported(feature: impl Into<String>) -> Self {
Self::Unsupported {
feature: feature.into(),
details: None,
}
}
#[must_use]
pub fn unsupported_with_details(
feature: impl Into<String>,
details: impl Into<String>,
) -> Self {
Self::Unsupported {
feature: feature.into(),
details: Some(details.into()),
}
}
#[must_use]
pub fn compatibility(feature: impl Into<String>, details: Option<String>) -> Self {
Self::Compatibility {
feature: feature.into(),
details,
}
}
#[must_use]
pub fn deprecated(setting: impl Into<String>, message: impl Into<String>) -> Self {
Self::Deprecated {
setting: setting.into(),
message: message.into(),
}
}
#[must_use]
pub fn other(message: impl Into<String>) -> Self {
Self::Other {
message: message.into(),
}
}
}
impl std::fmt::Display for Warning {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Unsupported { feature, details } => match details {
Some(details) => write!(f, "unsupported {feature}: {details}"),
None => write!(f, "unsupported {feature}"),
},
Self::Compatibility { feature, details } => match details {
Some(details) => write!(f, "compatibility for {feature}: {details}"),
None => write!(f, "compatibility for {feature}"),
},
Self::Deprecated { setting, message } => {
write!(f, "deprecated setting {setting}: {message}")
}
Self::Other { message } => f.write_str(message),
}
}
}