subspace-contracts 0.2.0

Subspace Framework: typed, versioned board-to-board contracts. Internal crate; apps depend on `subspace-framework`.
Documentation
//! URL parsing for issue links: Linear links, raw authorities and bare
//! app-relative addresses in plain text.

use std::ops::Range;
use url::Url;

/// The identifier in a Linear issue link of `workspace`, the first path
/// segment of that workspace's links.
pub(super) fn linear_identifier(url: &str, workspace: &str) -> Option<String> {
    let authority = authority(url)?;
    let target = Url::parse(url).ok()?;
    // Inspect the original authority too: Url removes an explicit default port
    // and empty user information during normalisation.
    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))
}