use std::fmt;
use serde::de::Error as _;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Methodology {
RteDirect,
AcvAdeme,
}
impl Methodology {
pub fn as_str(&self) -> &'static str {
match self {
Methodology::RteDirect => "rte-direct",
Methodology::AcvAdeme => "acv-ademe",
}
}
}
impl fmt::Display for Methodology {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Estimator {
Central,
Prudent,
}
impl Estimator {
pub fn as_str(&self) -> &'static str {
match self {
Estimator::Central => "central",
Estimator::Prudent => "prudent",
}
}
}
impl fmt::Display for Estimator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum EligibilityFramework {
Rfnbo,
LowCarbon,
}
impl EligibilityFramework {
pub fn as_str(&self) -> &'static str {
match self {
EligibilityFramework::Rfnbo => "rfnbo",
EligibilityFramework::LowCarbon => "low-carbon",
}
}
}
impl fmt::Display for EligibilityFramework {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Interval {
Hour,
Day,
}
impl Interval {
pub fn as_str(&self) -> &'static str {
match self {
Interval::Hour => "hour",
Interval::Day => "day",
}
}
}
impl fmt::Display for Interval {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum FlowDirection {
Import,
Export,
Balanced,
}
impl FlowDirection {
pub fn as_str(&self) -> &'static str {
match self {
FlowDirection::Import => "import",
FlowDirection::Export => "export",
FlowDirection::Balanced => "balanced",
}
}
}
impl fmt::Display for FlowDirection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ThresholdDirection {
Below,
Above,
}
impl ThresholdDirection {
pub fn as_str(&self) -> &'static str {
match self {
ThresholdDirection::Below => "below",
ThresholdDirection::Above => "above",
}
}
}
impl fmt::Display for ThresholdDirection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum WebhookStatus {
Active,
Disabled,
}
impl WebhookStatus {
pub fn as_str(&self) -> &'static str {
match self {
WebhookStatus::Active => "active",
WebhookStatus::Disabled => "disabled",
}
}
}
impl fmt::Display for WebhookStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum EligibilitySignalVerdict {
Pass,
Fail,
Indeterminate,
}
impl EligibilitySignalVerdict {
pub fn as_str(&self) -> &'static str {
match self {
EligibilitySignalVerdict::Pass => "pass",
EligibilitySignalVerdict::Fail => "fail",
EligibilitySignalVerdict::Indeterminate => "indeterminate",
}
}
}
impl fmt::Display for EligibilitySignalVerdict {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum EligibilitySignalProvenance {
Observed,
Forecast,
Other(String),
}
impl EligibilitySignalProvenance {
pub fn as_str(&self) -> &str {
match self {
EligibilitySignalProvenance::Observed => "observed",
EligibilitySignalProvenance::Forecast => "forecast",
EligibilitySignalProvenance::Other(s) => s.as_str(),
}
}
}
impl fmt::Display for EligibilitySignalProvenance {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl From<&str> for EligibilitySignalProvenance {
fn from(value: &str) -> Self {
match value {
"observed" => EligibilitySignalProvenance::Observed,
"forecast" => EligibilitySignalProvenance::Forecast,
other => EligibilitySignalProvenance::Other(other.to_string()),
}
}
}
impl Serialize for EligibilitySignalProvenance {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for EligibilitySignalProvenance {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = String::deserialize(deserializer).map_err(D::Error::custom)?;
Ok(EligibilitySignalProvenance::from(value.as_str()))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum IntensityIndex {
VeryLow,
Low,
Moderate,
High,
VeryHigh,
Other(String),
}
impl IntensityIndex {
pub fn as_str(&self) -> &str {
match self {
IntensityIndex::VeryLow => "very-low",
IntensityIndex::Low => "low",
IntensityIndex::Moderate => "moderate",
IntensityIndex::High => "high",
IntensityIndex::VeryHigh => "very-high",
IntensityIndex::Other(label) => label.as_str(),
}
}
}
impl fmt::Display for IntensityIndex {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl From<&str> for IntensityIndex {
fn from(label: &str) -> Self {
match label {
"very-low" => IntensityIndex::VeryLow,
"low" => IntensityIndex::Low,
"moderate" => IntensityIndex::Moderate,
"high" => IntensityIndex::High,
"very-high" => IntensityIndex::VeryHigh,
other => IntensityIndex::Other(other.to_string()),
}
}
}
impl From<String> for IntensityIndex {
fn from(label: String) -> Self {
IntensityIndex::from(label.as_str())
}
}
impl Serialize for IntensityIndex {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for IntensityIndex {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let label = String::deserialize(deserializer).map_err(D::Error::custom)?;
Ok(IntensityIndex::from(label))
}
}
#[cfg(test)]
mod tests {
use super::*;
macro_rules! assert_serde_matches_as_str {
($($value:expr),+ $(,)?) => {
$(
let value = $value;
let json = serde_json::to_string(&value).expect("sérialisation");
assert_eq!(json, format!("\"{}\"", value.as_str()));
let back = serde_json::from_str(&json).expect("désérialisation");
assert_eq!(value, back);
)+
};
}
#[test]
fn serde_matches_http_slug() {
assert_serde_matches_as_str!(Methodology::RteDirect, Methodology::AcvAdeme);
assert_serde_matches_as_str!(Estimator::Central, Estimator::Prudent);
assert_serde_matches_as_str!(EligibilityFramework::Rfnbo, EligibilityFramework::LowCarbon);
assert_serde_matches_as_str!(Interval::Hour, Interval::Day);
assert_serde_matches_as_str!(
FlowDirection::Import,
FlowDirection::Export,
FlowDirection::Balanced
);
assert_serde_matches_as_str!(ThresholdDirection::Below, ThresholdDirection::Above);
assert_serde_matches_as_str!(WebhookStatus::Active, WebhookStatus::Disabled);
assert_serde_matches_as_str!(
EligibilitySignalVerdict::Pass,
EligibilitySignalVerdict::Fail,
EligibilitySignalVerdict::Indeterminate
);
}
#[test]
fn eligibility_signal_provenance_round_trips_known_and_unknown() {
for slug in ["observed", "forecast", "measured-2027"] {
let value = EligibilitySignalProvenance::from(slug);
let json = serde_json::to_string(&value).expect("sérialisation");
assert_eq!(json, format!("\"{slug}\""));
let back: EligibilitySignalProvenance =
serde_json::from_str(&json).expect("désérialisation");
assert_eq!(back, value);
}
assert!(matches!(
EligibilitySignalProvenance::from("measured-2027"),
EligibilitySignalProvenance::Other(_)
));
}
#[test]
fn intensity_index_round_trips_known_slugs() {
for (slug, expected) in [
("very-low", IntensityIndex::VeryLow),
("low", IntensityIndex::Low),
("moderate", IntensityIndex::Moderate),
("high", IntensityIndex::High),
("very-high", IntensityIndex::VeryHigh),
] {
let index = IntensityIndex::from(slug);
assert_eq!(index, expected);
assert_eq!(index.as_str(), slug);
assert_eq!(index.to_string(), slug);
assert_eq!(IntensityIndex::from(slug.to_string()), expected);
}
}
#[test]
fn intensity_index_unknown_slug_stays_open() {
let index = IntensityIndex::from("nouvelle-bande");
assert_eq!(index.as_str(), "nouvelle-bande");
assert_eq!(index.to_string(), "nouvelle-bande");
assert!(matches!(index, IntensityIndex::Other(_)));
assert!(matches!(
IntensityIndex::from("nouvelle-bande".to_string()),
IntensityIndex::Other(_)
));
}
#[test]
fn intensity_index_serde_round_trips_known_and_unknown_slugs() {
for slug in ["very-low", "low", "moderate", "high", "very-high"] {
let index = IntensityIndex::from(slug);
assert!(!matches!(index, IntensityIndex::Other(_)), "{slug}");
let json = serde_json::to_string(&index).expect("sérialisation");
assert_eq!(json, format!("\"{slug}\""));
let back: IntensityIndex = serde_json::from_str(&json).expect("désérialisation");
assert_eq!(back, index);
}
let unknown: IntensityIndex =
serde_json::from_str("\"nouvelle-bande\"").expect("libellé inconnu toléré");
assert_eq!(unknown, IntensityIndex::Other("nouvelle-bande".to_string()));
let json = serde_json::to_string(&unknown).expect("sérialisation");
assert_eq!(json, "\"nouvelle-bande\"");
let back: IntensityIndex = serde_json::from_str(&json).expect("désérialisation");
assert_eq!(back, unknown);
}
}