pavex/blueprint/
conversions.rs

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! Conversions between `pavex_bp_schema` and `pavex_bp` types.
use crate::blueprint::constructor::{CloningStrategy, Lifecycle};
use crate::blueprint::linter::Lint;
use crate::blueprint::reflection::RawIdentifiers;
use crate::router::AllowedMethods;
use pavex_bp_schema::{Callable, Location, Type};
use pavex_reflection::RegisteredAt;

#[track_caller]
pub(super) fn raw_identifiers2callable(callable: RawIdentifiers) -> Callable {
    if callable.macro_name != "f" {
        panic!(
            "You need to use the `f!` macro to register function-like components (e.g. a constructor).\n\
            Here you used the `t!` macro, which is reserved type-like components, like state inputs."
        )
    }
    let registered_at = RegisteredAt {
        crate_name: callable.crate_name.to_owned(),
        module_path: callable.module_path.to_owned(),
    };
    Callable {
        callable: pavex_bp_schema::RawIdentifiers {
            registered_at,
            import_path: callable.import_path.to_owned(),
        },
        location: Location::caller(),
    }
}

#[track_caller]
pub(super) fn raw_identifiers2type(callable: RawIdentifiers) -> Type {
    if callable.macro_name != "t" {
        panic!(
            "You need to use the `t!` macro to register type-like components (e.g. a state input).\n\
            Here you used the `f!` macro, which is reserved for function-like components, \
            like constructors or request handlers."
        )
    }
    let registered_at = RegisteredAt {
        crate_name: callable.crate_name.to_owned(),
        module_path: callable.module_path.to_owned(),
    };
    Type {
        type_: pavex_bp_schema::RawIdentifiers {
            registered_at,
            import_path: callable.import_path.to_owned(),
        },
        location: Location::caller(),
    }
}

pub(super) fn lifecycle2lifecycle(lifecycle: Lifecycle) -> pavex_bp_schema::Lifecycle {
    match lifecycle {
        Lifecycle::RequestScoped => pavex_bp_schema::Lifecycle::RequestScoped,
        Lifecycle::Transient => pavex_bp_schema::Lifecycle::Transient,
        Lifecycle::Singleton => pavex_bp_schema::Lifecycle::Singleton,
    }
}

pub(super) fn cloning2cloning(cloning: CloningStrategy) -> pavex_bp_schema::CloningStrategy {
    match cloning {
        CloningStrategy::CloneIfNecessary => pavex_bp_schema::CloningStrategy::CloneIfNecessary,
        CloningStrategy::NeverClone => pavex_bp_schema::CloningStrategy::NeverClone,
    }
}

pub(super) fn method_guard2method_guard(
    method_guard: crate::blueprint::router::MethodGuard,
) -> pavex_bp_schema::MethodGuard {
    match method_guard.allowed_methods() {
        AllowedMethods::Some(m) => pavex_bp_schema::MethodGuard::Some(
            m.into_iter().map(|m| m.as_str().to_owned()).collect(),
        ),
        AllowedMethods::All => pavex_bp_schema::MethodGuard::Any,
    }
}

pub(super) fn lint2lint(lint: Lint) -> pavex_bp_schema::Lint {
    match lint {
        Lint::Unused => pavex_bp_schema::Lint::Unused,
    }
}