Skip to main content

subspace_client_github/issue_report/
draft.rs

1use super::{Error, ErrorKind};
2
3const MAX_TITLE_CHARS: usize = 256;
4
5#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
6#[non_exhaustive]
7pub enum IssueType {
8    Bug,
9    Ux,
10    FeatureRequest,
11    Other,
12    #[default]
13    Unknown,
14}
15
16impl IssueType {
17    pub fn label(self) -> &'static str {
18        self.descriptor().1
19    }
20
21    pub fn name(self) -> &'static str {
22        self.descriptor().0
23    }
24
25    fn descriptor(self) -> (&'static str, &'static str) {
26        match self {
27            Self::Bug => ("Bug", "bug"),
28            Self::Ux => ("UX", "ux"),
29            Self::FeatureRequest => ("Feature request", "enhancement"),
30            Self::Other => ("Other", "other"),
31            Self::Unknown => ("Unknown", "unknown"),
32        }
33    }
34}
35
36#[derive(Debug, Default, Clone)]
37pub struct Draft {
38    title: String,
39    description: String,
40    issue_type: Option<IssueType>,
41    steps: String,
42}
43
44// qual:allow(dry, boilerplate) reason: "Native builders preserve the required private-field reporting API without adding a derive dependency."
45impl Draft {
46    pub fn new(description: impl Into<String>) -> Self {
47        Self {
48            description: description.into(),
49            ..Self::default()
50        }
51    }
52
53    pub fn with_title(mut self, title: impl Into<String>) -> Self {
54        self.title = title.into();
55        self
56    }
57
58    pub fn with_issue_type(mut self, issue_type: IssueType) -> Self {
59        self.issue_type = Some(issue_type);
60        self
61    }
62
63    pub fn with_steps(mut self, steps: impl Into<String>) -> Self {
64        self.steps = steps.into();
65        self
66    }
67
68    // qual:allow(dry, boilerplate) reason: "The form's draft text is exposed through readers and replacement builders, not mutable public fields."
69    pub fn title(&self) -> &str {
70        &self.title
71    }
72
73    // qual:allow(dry, boilerplate) reason: "The host reads user text without bypassing the replacement-builder boundary."
74    pub fn description(&self) -> &str {
75        &self.description
76    }
77
78    pub fn issue_type(&self) -> IssueType {
79        self.issue_type.unwrap_or_default()
80    }
81
82    // qual:allow(dry, boilerplate) reason: "The host reads exact reproduction text without mutating a prepared snapshot."
83    pub fn steps(&self) -> &str {
84        &self.steps
85    }
86}
87
88pub(super) fn validated_title(draft: &Draft) -> Result<String, Error> {
89    let first_line = draft
90        .description
91        .lines()
92        .find(|line| !line.trim().is_empty())
93        .ok_or(ErrorKind::DescriptionRequired)?;
94    let title = draft.title.trim();
95    if title.is_empty() {
96        let derived: String = first_line.trim().chars().take(MAX_TITLE_CHARS).collect();
97        if derived.chars().any(char::is_control) {
98            return Err(ErrorKind::InvalidTitle.into());
99        }
100        return Ok(derived);
101    }
102    if title.chars().count() > MAX_TITLE_CHARS || title.chars().any(char::is_control) {
103        return Err(ErrorKind::InvalidTitle.into());
104    }
105    Ok(title.into())
106}
107
108pub(super) fn body(draft: &Draft) -> String {
109    let mut body = format!(
110        "## Description\n\n{}\n\n## Type\n\n{}",
111        draft.description,
112        draft.issue_type().name()
113    );
114    if !draft.steps.is_empty() {
115        body.push_str(&format!("\n\n## Steps to reproduce\n\n{}", draft.steps));
116    }
117    body
118}