use super::{PreparedReport, body_with_diagnostics};
pub const MAX_BROWSER_URL_BYTES: usize = 8_000;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BrowserMode {
Prefilled,
ClipboardRequired,
}
#[derive(Debug, Clone)]
pub struct BrowserFallback {
url: String,
body: String,
clipboard_body: Option<String>,
diagnostics_omitted: usize,
}
impl BrowserFallback {
pub fn url(&self) -> &str {
&self.url
}
pub fn body(&self) -> &str {
&self.body
}
pub fn clipboard_body(&self) -> Option<&str> {
self.clipboard_body.as_deref()
}
pub fn diagnostics_omitted(&self) -> usize {
self.diagnostics_omitted
}
pub fn editor_mode(&self) -> BrowserMode {
if self.clipboard_body.is_some() {
BrowserMode::ClipboardRequired
} else {
BrowserMode::Prefilled
}
}
}
pub(super) fn prepare(report: &PreparedReport) -> BrowserFallback {
for keep in (0..=report.diagnostic_lines.len()).rev() {
let body = body_with_diagnostics(&report.user_body, &report.diagnostic_lines[..keep]);
let url = encoded_url(&report.repository.new_issue_url(), &report.title, &body);
if url.len() <= MAX_BROWSER_URL_BYTES {
return BrowserFallback {
url,
body,
clipboard_body: None,
diagnostics_omitted: report.diagnostic_lines.len() - keep,
};
}
}
BrowserFallback {
url: report.repository.new_issue_url(),
body: report.body.clone(),
clipboard_body: Some(report.body.clone()),
diagnostics_omitted: 0,
}
}
fn encoded_url(editor: &str, title: &str, body: &str) -> String {
let query = url::form_urlencoded::Serializer::new(String::new())
.append_pair("title", title)
.append_pair("body", body)
.finish();
format!("{editor}&{query}")
}