arks-server 1.3.2

Git server: upload-pack / receive-pack + smart/dumb HTTP + daemon (M11/M25)
Documentation
//! Typed route URL builders and declarative per-route access policies.

use super::ctx::MountPrefix;
use super::html::url_encode_component;

/// Declarative authorization & archival policy evaluated centrally by Web UI middleware.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AccessPolicy {
    /// Public endpoint (login, register, assets, robots.txt).
    Public,
    /// Read-only repository page (enforces repository visibility when private).
    ReadRepo,
    /// State-mutating repository action (requires write RBAC, CSRF, and non-archived repo).
    WriteRepo,
    /// Repository settings mutation permitted even while archived (e.g. unarchiving).
    WriteRepoUnarchive,
    /// Administrative endpoint (requires admin role and CSRF on mutations).
    Admin,
}

/// Typed application routes generating percent-encoded, mount-aware URLs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AppRoute<'a> {
    Home,
    Login,
    Register,
    Logout,
    Search,
    Notifications,
    Releases,
    Branches,
    Tags,
    Commits {
        rev: Option<&'a str>,
    },
    Commit {
        oid: &'a str,
    },
    Compare {
        spec: Option<&'a str>,
    },
    Tree {
        rev: &'a str,
        path: &'a str,
    },
    Blob {
        rev: &'a str,
        path: &'a str,
    },
    Issues {
        state: Option<&'a str>,
        label: Option<&'a str>,
    },
    IssueDetail {
        id: u64,
    },
    Pulls,
    PullNew,
    PullDetail {
        branch: &'a str,
    },
    Actions,
    ActionRun {
        id: u64,
    },
    Projects,
    Wiki {
        slug: Option<&'a str>,
    },
    Security,
    Insights,
    Settings {
        section: Option<&'a str>,
    },
    Admin,
    Agents,
    /// Extension path (e.g. `v2.0` `"/graph"`, `"/stacks"`, `"/agents/topology"`).
    Custom(&'a str),
}

impl<'a> AppRoute<'a> {
    /// Format this route under `mount`, percent-encoding dynamic path and query values.
    pub fn to_url(self, mount: &MountPrefix) -> String {
        match self {
            AppRoute::Home => mount.path("/"),
            AppRoute::Login => mount.path("/login"),
            AppRoute::Register => mount.path("/register"),
            AppRoute::Logout => mount.path("/logout"),
            AppRoute::Search => mount.path("/search"),
            AppRoute::Notifications => mount.path("/notifications"),
            AppRoute::Releases => mount.path("/releases"),
            AppRoute::Branches => mount.path("/branches"),
            AppRoute::Tags => mount.path("/tags"),
            AppRoute::Commits { rev: None } => mount.path("/commits"),
            AppRoute::Commits { rev: Some(r) } => {
                mount.path(&format!("/commits/{}", encode_ref_path(r)))
            }
            AppRoute::Commit { oid } => {
                mount.path(&format!("/commit/{}", url_encode_component(oid)))
            }
            AppRoute::Compare { spec: None } => mount.path("/compare"),
            AppRoute::Compare { spec: Some(s) } => mount.path(&format!("/compare/{s}")),
            AppRoute::Tree { rev, path } => {
                if path.is_empty() {
                    mount.path(&format!("/tree/{}", encode_ref_path(rev)))
                } else {
                    mount.path(&format!(
                        "/tree/{}/{}",
                        encode_ref_path(rev),
                        encode_ref_path(path)
                    ))
                }
            }
            AppRoute::Blob { rev, path } => mount.path(&format!(
                "/blob/{}/{}",
                encode_ref_path(rev),
                encode_ref_path(path)
            )),
            AppRoute::Issues { state, label } => {
                let mut url = mount.path("/issues");
                let mut sep = '?';
                if let Some(st) = state {
                    url.push(sep);
                    url.push_str("state=");
                    url.push_str(&url_encode_component(st));
                    sep = '&';
                }
                if let Some(lbl) = label {
                    url.push(sep);
                    url.push_str("label=");
                    url.push_str(&url_encode_component(lbl));
                }
                url
            }
            AppRoute::IssueDetail { id } => mount.path(&format!("/issues/{id}")),
            AppRoute::Pulls => mount.path("/pulls"),
            AppRoute::PullNew => mount.path("/pulls/new"),
            AppRoute::PullDetail { branch } => {
                mount.path(&format!("/pulls/{}", encode_ref_path(branch)))
            }
            AppRoute::Actions => mount.path("/actions"),
            AppRoute::ActionRun { id } => mount.path(&format!("/actions/runs/{id}")),
            AppRoute::Projects => mount.path("/projects"),
            AppRoute::Wiki { slug: None } => mount.path("/wiki"),
            AppRoute::Wiki { slug: Some(s) } => {
                mount.path(&format!("/wiki/{}", url_encode_component(s)))
            }
            AppRoute::Security => mount.path("/security"),
            AppRoute::Insights => mount.path("/insights"),
            AppRoute::Settings { section: None } => mount.path("/settings"),
            AppRoute::Settings { section: Some(s) } => {
                mount.path(&format!("/settings/{}", encode_ref_path(s)))
            }
            AppRoute::Admin => mount.path("/admin"),
            AppRoute::Agents => mount.path("/agents"),
            AppRoute::Custom(rel) => mount.path(rel),
        }
    }
}

fn encode_ref_path(path: &str) -> String {
    path.split('/')
        .map(url_encode_component)
        .collect::<Vec<_>>()
        .join("/")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn typed_routes_apply_mount_prefix_and_percent_encoding() {
        let root = MountPrefix::root();
        let multi = MountPrefix::new("alpha");

        assert_eq!(
            AppRoute::Issues {
                state: Some("open"),
                label: Some("bug & ui"),
            }
            .to_url(&multi),
            "/alpha/issues?state=open&label=bug%20%26%20ui"
        );
        assert_eq!(
            AppRoute::PullDetail {
                branch: "feat/my branch"
            }
            .to_url(&root),
            "/pulls/feat/my%20branch"
        );
        assert_eq!(AppRoute::Custom("/graph").to_url(&multi), "/alpha/graph");
    }
}