Skip to main content

subspace_client_github/issue_report/
browser.rs

1use super::{PreparedReport, body_with_diagnostics};
2
3pub const MAX_BROWSER_URL_BYTES: usize = 8_000;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum BrowserMode {
7    Prefilled,
8    /// Host must copy `clipboard_body` before opening the blank editor.
9    ClipboardRequired,
10}
11
12/// Previewable instructions, not proof of issue creation or completed I/O.
13#[derive(Debug, Clone)]
14pub struct BrowserFallback {
15    url: String,
16    body: String,
17    clipboard_body: Option<String>,
18    diagnostics_omitted: usize,
19}
20
21impl BrowserFallback {
22    // qual:allow(dry, boilerplate) reason: "The bounded encoded URL must remain bound to its reviewed effective body."
23    pub fn url(&self) -> &str {
24        &self.url
25    }
26    /// Exact prefilled text, or the full text the user must paste.
27    // qual:allow(dry, boilerplate) reason: "Readonly preview text prevents divergence from the encoded editor URL."
28    pub fn body(&self) -> &str {
29        &self.body
30    }
31    pub fn clipboard_body(&self) -> Option<&str> {
32        self.clipboard_body.as_deref()
33    }
34    // qual:api
35    // qual:allow(dry, boilerplate) reason: "The host explains diagnostic omission without changing the prepared body."
36    pub fn diagnostics_omitted(&self) -> usize {
37        self.diagnostics_omitted
38    }
39    pub fn editor_mode(&self) -> BrowserMode {
40        if self.clipboard_body.is_some() {
41            BrowserMode::ClipboardRequired
42        } else {
43            BrowserMode::Prefilled
44        }
45    }
46}
47
48// qual:allow(dry, boilerplate) reason: "The browser variant must own the chosen effective text while the API snapshot remains unchanged."
49pub(super) fn prepare(report: &PreparedReport) -> BrowserFallback {
50    for keep in (0..=report.diagnostic_lines.len()).rev() {
51        let body = body_with_diagnostics(&report.user_body, &report.diagnostic_lines[..keep]);
52        let url = encoded_url(&report.repository.new_issue_url(), &report.title, &body);
53        if url.len() <= MAX_BROWSER_URL_BYTES {
54            return BrowserFallback {
55                url,
56                body,
57                clipboard_body: None,
58                diagnostics_omitted: report.diagnostic_lines.len() - keep,
59            };
60        }
61    }
62    BrowserFallback {
63        url: report.repository.new_issue_url(),
64        body: report.body.clone(),
65        clipboard_body: Some(report.body.clone()),
66        diagnostics_omitted: 0,
67    }
68}
69
70fn encoded_url(editor: &str, title: &str, body: &str) -> String {
71    let query = url::form_urlencoded::Serializer::new(String::new())
72        .append_pair("title", title)
73        .append_pair("body", body)
74        .finish();
75    format!("{editor}&{query}")
76}