cpu/
bugreport.rs

1//! Generate bug-report URLs.
2use url::{ParseError, Url};
3
4// We have a version override for idna_adapter in the Cargo.toml file,
5// because GITHUB_URL is ASCII-only and we want to keep the size of
6// the binary down.
7use idna_adapter as _;
8
9const GITHUB_URL: &str = "https://github.com/";
10const ORG_NAME: &str = "TX-2";
11const REPO_NAME: &str = "TX-2-simulator";
12
13#[derive(Debug, Clone, Copy, PartialOrd, Ord, Hash, Eq, PartialEq)]
14pub enum IssueType {
15    Io,
16    Opcode,
17}
18
19/// Provide a URL at which a human can report a bug.
20///
21/// # Arguments
22/// * `title` - description of the problem
23/// * `issue_type` - what kind of issue this is, if known
24#[must_use]
25pub fn bug_report_url(title: &str, issue_type: Option<IssueType>) -> Url {
26    bug_report_url_internal(title, issue_type).expect("bug-report URLs should always be valid")
27}
28
29fn bug_report_url_internal(title: &str, issue_type: Option<IssueType>) -> Result<Url, ParseError> {
30    let mut url = Url::parse(GITHUB_URL)?.join(&format!("{ORG_NAME}/{REPO_NAME}/issues/new"))?;
31    url.query_pairs_mut()
32        .append_pair("template", "bug_report.md")
33        .append_pair("title", title);
34    match issue_type {
35        Some(IssueType::Io) => {
36            url.query_pairs_mut().append_pair("labels", "I/O");
37        }
38        Some(IssueType::Opcode) => {
39            url.query_pairs_mut().append_pair("labels", "Opcode");
40        }
41        None => (), // add no labels.
42    }
43    Ok(url)
44}
45
46#[test]
47fn test_bug_report_url() {
48    assert_eq!(
49        bug_report_url("Eat some falafel", Some(IssueType::Io)).to_string(),
50        "https://github.com/TX-2/TX-2-simulator/issues/new?template=bug_report.md&title=Eat+some+falafel&labels=I%2FO"
51    );
52}