Skip to main content

edda_types/
edition.rs

1//! Which edition of the rules a campaign plays by, chosen when the campaign
2//! is created. Only where the editions differ does it matter (so far: when
3//! Paladins and Rangers get spell slots).
4
5use std::{fmt, str::FromStr};
6
7use serde::{Deserialize, Serialize};
8
9/// Which rules a campaign plays by: the 2014 or the 2024 ones.
10#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
11#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
12pub enum Edition {
13    #[serde(rename = "2014")]
14    Rules2014,
15    /// The default only for campaigns created before editions were
16    /// recorded; new campaigns always name one.
17    #[default]
18    #[serde(rename = "2024")]
19    Rules2024,
20}
21
22impl fmt::Display for Edition {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        f.write_str(match self {
25            Edition::Rules2014 => "2014",
26            Edition::Rules2024 => "2024",
27        })
28    }
29}
30
31/// An edition other than 2014 or 2024.
32#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
33#[error("unknown edition \"{0}\"; use 2014 or 2024")]
34pub struct UnknownEdition(String);
35
36impl FromStr for Edition {
37    type Err = UnknownEdition;
38
39    fn from_str(s: &str) -> Result<Self, Self::Err> {
40        match s.trim() {
41            "2014" => Ok(Edition::Rules2014),
42            "2024" => Ok(Edition::Rules2024),
43            other => Err(UnknownEdition(other.to_string())),
44        }
45    }
46}