1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::{fmt, fmt::Display};

use git_conventional::FooterToken;
use serde::{Deserialize, Serialize};

use crate::changes::ChangeType;

/// Where a custom release section comes from, for example, the custom change type "extra" in
/// a change file might correspond to a section called "Extras" in the changelog.
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub enum SectionSource {
    CommitFooter(CommitFooter),
    CustomChangeType(CustomChangeType),
}

impl From<CommitFooter> for SectionSource {
    fn from(footer: CommitFooter) -> Self {
        Self::CommitFooter(footer)
    }
}

impl From<CustomChangeType> for SectionSource {
    fn from(change_type: CustomChangeType) -> Self {
        Self::CustomChangeType(change_type)
    }
}

impl Display for SectionSource {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::CommitFooter(footer) => footer.fmt(f),
            Self::CustomChangeType(change_type) => change_type.fmt(f),
        }
    }
}

/// A non-standard conventional commit type (e.g., the "doc" in "doc: some message")
/// or a non-standard changeset type ([`changesets::ChangeType::Custom`]).
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(transparent)]
pub struct CustomChangeType(String);

impl PartialEq<str> for CustomChangeType {
    fn eq(&self, other: &str) -> bool {
        self.0 == other
    }
}

#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(transparent)]
pub struct CommitFooter(String);

impl Display for CommitFooter {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<&str> for CommitFooter {
    fn from(token: &str) -> Self {
        Self(token.into())
    }
}

impl From<FooterToken<'_>> for CommitFooter {
    fn from(token: FooterToken) -> Self {
        Self(token.as_str().into())
    }
}

impl Display for CustomChangeType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<String> for CustomChangeType {
    fn from(token: String) -> Self {
        Self(token)
    }
}

impl From<&str> for CustomChangeType {
    fn from(token: &str) -> Self {
        Self(token.into())
    }
}

impl From<CustomChangeType> for String {
    fn from(custom: CustomChangeType) -> Self {
        custom.0
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Sections(pub Vec<(SectionName, Vec<ChangeType>)>);

impl Sections {
    #[must_use]
    pub fn defaults() -> Vec<(SectionName, ChangeType)> {
        vec![
            (SectionName::from("Breaking Changes"), ChangeType::Breaking),
            (SectionName::from("Features"), ChangeType::Feature),
            (SectionName::from("Fixes"), ChangeType::Fix),
            ("Notes".into(), CommitFooter::from("Changelog-Note").into()),
        ]
    }

    pub fn iter(&self) -> impl Iterator<Item = &(SectionName, Vec<ChangeType>)> {
        self.0.iter()
    }

    #[allow(dead_code)]
    pub(crate) fn is_default(&self) -> bool {
        let defaults = Self::defaults();
        self.0.iter().enumerate().all(|(index, (name, sources))| {
            if sources.len() != 1 {
                return false;
            }
            sources.first().is_some_and(|source| {
                defaults
                    .get(index)
                    .is_some_and(|(default_name, default_source)| {
                        name == default_name && source == default_source
                    })
            })
        })
    }

    pub(crate) fn contains_footer(&self, footer: &git_conventional::Footer) -> bool {
        self.0.iter().any(|(_, sources)| {
            sources.iter().any(|source| match source {
                ChangeType::Custom(SectionSource::CommitFooter(footer_token)) => {
                    footer_token.0.eq_ignore_ascii_case(footer.token().as_str())
                }
                _ => false,
            })
        })
    }
}

impl Default for Sections {
    fn default() -> Self {
        Self(
            Self::defaults()
                .into_iter()
                .map(|(name, source)| (name, vec![source]))
                .collect(),
        )
    }
}

impl IntoIterator for Sections {
    type Item = (SectionName, Vec<ChangeType>);
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(transparent)]
pub struct SectionName(String);

impl Display for SectionName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<&str> for SectionName {
    fn from(token: &str) -> Self {
        Self(token.into())
    }
}

impl AsRef<str> for SectionName {
    fn as_ref(&self) -> &str {
        &self.0
    }
}