drawbridge_type/tree/
context.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use super::super::TagContext;
4use super::Path;
5
6use std::fmt::Display;
7
8#[derive(Clone, Debug, Eq, Hash, PartialEq)]
9pub struct Context {
10    pub tag: TagContext,
11    pub path: Path,
12}
13
14impl Display for Context {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        write!(f, "{}/{}", self.tag, self.path)
17    }
18}
19
20#[cfg(feature = "axum")]
21#[axum::async_trait]
22impl<B: Send> axum::extract::FromRequest<B> for Context {
23    type Rejection = (axum::http::StatusCode, String);
24
25    async fn from_request(
26        req: &mut axum::extract::RequestParts<B>,
27    ) -> Result<Self, Self::Rejection> {
28        let tag = req.extract().await?;
29        let axum::Extension(path) = req.extract().await.map_err(|e| {
30            (
31                axum::http::StatusCode::INTERNAL_SERVER_ERROR,
32                anyhow::Error::new(e)
33                    .context("failed to extract tree context")
34                    .to_string(),
35            )
36        })?;
37        Ok(Self { tag, path })
38    }
39}