pax_manifest/
server.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use crate::parsing::Reflectable;
use crate::{PaxManifest, TypeId};
use pax_runtime_api::{CoercionRules, HelperFunctions, Interpolatable, PaxValue, ToPaxValue};
use serde::{Deserialize, Serialize};
use std::cell::Ref;

#[derive(Serialize, Deserialize)]
pub struct PublishRequest {
    pub manifest: PaxManifest,
    //github_username: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct PublishResponseSuccess {
    pub pull_request_url: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub enum PublishResponse {
    #[default]
    Undefined,
    Success(PublishResponseSuccess),
    Error(ResponseError),
}

#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct ResponseError {
    pub message: String,
}

impl Interpolatable for PublishResponse {}
impl Interpolatable for PublishResponseSuccess {}
impl Interpolatable for ResponseError {}

impl ToPaxValue for PublishResponse {
    fn to_pax_value(self) -> PaxValue {
        match self {
            PublishResponse::Success(success) => PaxValue::Enum(
                "PublishResponse".to_string(),
                "Success".to_string(),
                vec![success.clone().to_pax_value()],
            ),
            PublishResponse::Error(error) => PaxValue::Enum(
                "PublishResponse".to_string(),
                "Error".to_string(),
                vec![error.clone().to_pax_value()],
            ),
            PublishResponse::Undefined => PaxValue::Enum(
                "PublishResponse".to_string(),
                "Undefined".to_string(),
                vec![],
            ),
        }
    }
}

impl ToPaxValue for PublishResponseSuccess {
    fn to_pax_value(self) -> PaxValue {
        PaxValue::Enum(
            "".to_string(),
            "PublishResponseSuccess".to_string(),
            vec![self.pull_request_url.clone().to_pax_value()],
        )
    }
}

impl ToPaxValue for ResponseError {
    fn to_pax_value(self) -> PaxValue {
        PaxValue::Enum(
            "".to_string(),
            "ResponseError".to_string(),
            vec![self.message.clone().to_pax_value()],
        )
    }
}

impl CoercionRules for PublishResponse {
    fn try_coerce(value: PaxValue) -> Result<Self, String> {
        match value {
            PaxValue::Enum(_, variant, values) => match variant.as_str() {
                "Success" => {
                    let success = PublishResponseSuccess::try_coerce(values[0].clone())?;
                    Ok(PublishResponse::Success(success))
                }
                "Error" => {
                    let error = ResponseError::try_coerce(values[0].clone())?;
                    Ok(PublishResponse::Error(error))
                }
                _ => Err(format!("Invalid variant: {}", variant)),
            },
            _ => Err("Invalid PaxValue".to_string()),
        }
    }
}

impl CoercionRules for PublishResponseSuccess {
    fn try_coerce(value: PaxValue) -> Result<Self, String> {
        match value {
            PaxValue::Enum(_, variant, values) => match variant.as_str() {
                "PublishResponseSuccess" => {
                    let pull_request_url = String::try_coerce(values[0].clone())?;
                    Ok(PublishResponseSuccess { pull_request_url })
                }
                _ => Err(format!("Invalid variant: {}", variant)),
            },
            _ => Err("Invalid PaxValue".to_string()),
        }
    }
}

impl CoercionRules for ResponseError {
    fn try_coerce(value: PaxValue) -> Result<Self, String> {
        match value {
            PaxValue::Enum(_, variant, values) => match variant.as_str() {
                "ResponseError" => {
                    let message = String::try_coerce(values[0].clone())?;
                    Ok(ResponseError { message })
                }
                _ => Err(format!("Invalid variant: {}", variant)),
            },
            _ => Err("Invalid PaxValue".to_string()),
        }
    }
}

impl Reflectable for PublishResponse {
    fn get_self_pascal_identifier() -> String {
        "PublishResponse".to_string()
    }

    fn get_type_id() -> TypeId {
        TypeId::build_singleton(
            "pax_manifest::server::PublishResponse",
            Some(&Self::get_self_pascal_identifier()),
        )
    }
}

impl Reflectable for PublishResponseSuccess {
    fn get_self_pascal_identifier() -> String {
        "PublishResponseSuccess".to_string()
    }

    fn get_type_id() -> TypeId {
        TypeId::build_singleton(
            "pax_manifest::server::PublishResponseSuccess",
            Some(&Self::get_self_pascal_identifier()),
        )
    }
}

impl Reflectable for ResponseError {
    fn get_self_pascal_identifier() -> String {
        "ResponseError".to_string()
    }

    fn get_type_id() -> TypeId {
        TypeId::build_singleton(
            "pax_manifest::server::ResponseError",
            Some(&Self::get_self_pascal_identifier()),
        )
    }
}

impl HelperFunctions for PublishResponseSuccess {}
impl HelperFunctions for PublishResponse {}
impl HelperFunctions for ResponseError {}