use std::ops::Range;
use url::Url;
pub(super) fn linear_identifier(url: &str, workspace: &str) -> Option<String> {
let authority = authority(url)?;
let target = Url::parse(url).ok()?;
if target.scheme() != "https"
|| target.host_str() != Some("linear.app")
|| authority.contains([':', '@', '\\'])
|| !target.username().is_empty()
|| target.password().is_some()
{
return None;
}
let tail = target
.path()
.strip_prefix('/')?
.strip_prefix(workspace)?
.strip_prefix("/issue/")?;
Some(tail.split('/').next()?.into())
}
pub(super) fn authority(url: &str) -> Option<&str> {
if stripped_by_url_parser(url) {
return None;
}
url.split_once(':')?
.1
.strip_prefix("//")?
.split(['/', '?', '#'])
.next()
.filter(|authority| !authority.is_empty())
}
fn stripped_by_url_parser(url: &str) -> bool {
url.contains(['\t', '\n', '\r'])
|| url.as_bytes().first().is_some_and(|byte| *byte <= b' ')
|| url.as_bytes().last().is_some_and(|byte| *byte <= b' ')
}
pub(super) fn relative_address(text: &str, start: usize) -> Option<(Range<usize>, &str)> {
let boundary = text[..start].chars().next_back();
if boundary.is_some_and(|c| !c.is_whitespace() && !matches!(c, '(' | '[' | '{')) {
return None;
}
let tail = &text[start..];
let end = tail.find(char::is_whitespace).unwrap_or(tail.len());
let url = tail[..end].trim_end_matches([')', ']', '}', ',', '.', ';', ':', '!', '?']);
Some((start..start + url.len(), url))
}