const MAX_OWNER: usize = 39;
const MAX_NAME: usize = 100;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IssueRepository(String);
impl IssueRepository {
pub fn parse(value: &str) -> Option<Self> {
let value = value.trim();
let (owner, name) = value.split_once('/')?;
(valid_owner(owner) && valid_name(name)).then(|| Self(value.to_owned()))
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn issues_url(&self) -> String {
format!("https://github.com/{}/issues", self.0)
}
pub fn new_issue_url(&self) -> String {
format!("{}/new?template=blank", self.issues_url())
}
pub fn is_created_issue_url(&self, url: &str) -> bool {
let prefix = format!("{}/", self.issues_url());
let Some(number) = url.strip_prefix(&prefix) else {
return false;
};
!number.is_empty()
&& number.bytes().all(|byte| byte.is_ascii_digit())
&& number.parse::<u64>().is_ok_and(|number| number > 0)
}
}
fn valid_owner(owner: &str) -> bool {
(1..=MAX_OWNER).contains(&owner.len())
&& owner
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
&& !owner.starts_with('-')
&& !owner.ends_with('-')
}
fn valid_name(name: &str) -> bool {
(1..=MAX_NAME).contains(&name.len())
&& name
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
&& name != "."
&& name != ".."
}