1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-FileCopyrightText: 2022 Profian Inc. <opensource@profian.com>
// SPDX-License-Identifier: AGPL-3.0-only

use super::super::UserContext;
use super::Name;

use std::fmt::Display;
use std::str::FromStr;

use anyhow::{anyhow, Context as _};

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Context {
    pub owner: UserContext,
    pub name: Name,
}

impl TryFrom<(&str, &str)> for Context {
    type Error = anyhow::Error;

    fn try_from((user, repo): (&str, &str)) -> Result<Self, Self::Error> {
        let owner = user.parse().context("failed to parse user context")?;
        let name = repo.parse().context("failed to parse repository name")?;
        Ok(Self { owner, name })
    }
}

impl FromStr for Context {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (owner, name) = s
            .rsplit_once(&['/', ':'])
            .ok_or_else(|| anyhow!("`/` or ':' separator not found"))?;
        let owner = owner.parse().context("failed to parse user context")?;
        let name = name.parse().context("failed to parse repository name")?;
        Ok(Self { owner, name })
    }
}

impl Display for Context {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}/{}", self.owner, self.name)
    }
}

#[cfg(feature = "axum")]
#[axum::async_trait]
impl<B: Send> axum::extract::FromRequest<B> for Context {
    type Rejection = (axum::http::StatusCode, String);

    async fn from_request(
        req: &mut axum::extract::RequestParts<B>,
    ) -> Result<Self, Self::Rejection> {
        let owner = req.extract().await?;
        let axum::Extension(name) = req.extract().await.map_err(|e| {
            (
                axum::http::StatusCode::INTERNAL_SERVER_ERROR,
                anyhow::Error::new(e)
                    .context("failed to extract repository context")
                    .to_string(),
            )
        })?;
        Ok(Self { owner, name })
    }
}