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
168
169
use exonum::{
proto::schema::{INCLUDES as EXONUM_INCLUDES, PROTO_SOURCES as EXONUM_PROTO_SOURCES},
runtime::{versioning::Version, ArtifactId, RuntimeIdentifier},
};
use exonum_api::{self as api, ApiBuilder};
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
use super::RustRuntime;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ProtoSourceFile {
pub name: String,
pub content: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ProtoSourcesQuery {
Core,
Artifact {
name: String,
version: Version,
},
}
#[derive(Debug, Default, Clone, PartialEq)]
pub struct ArtifactProtobufSpec {
pub sources: Vec<ProtoSourceFile>,
pub includes: Vec<ProtoSourceFile>,
}
impl ArtifactProtobufSpec {
pub fn new(
sources: impl IntoIterator<Item = impl Into<ProtoSourceFile>>,
includes: impl IntoIterator<Item = impl Into<ProtoSourceFile>>,
) -> Self {
Self {
sources: sources.into_iter().map(|x| x.into()).collect(),
includes: includes.into_iter().map(|x| x.into()).collect(),
}
}
}
impl From<&(&str, &str)> for ProtoSourceFile {
fn from(v: &(&str, &str)) -> Self {
Self {
name: v.0.to_owned(),
content: v.1.to_owned(),
}
}
}
fn exonum_proto_sources() -> Vec<ProtoSourceFile> {
let proto = EXONUM_PROTO_SOURCES
.as_ref()
.iter()
.map(From::from)
.collect::<Vec<_>>();
let includes = EXONUM_INCLUDES
.as_ref()
.iter()
.map(From::from)
.collect::<Vec<_>>();
proto.into_iter().chain(includes).collect()
}
fn filter_exonum_proto_sources(
files: Vec<ProtoSourceFile>,
exonum_sources: &[ProtoSourceFile],
) -> Vec<ProtoSourceFile> {
files
.into_iter()
.filter(|file| !exonum_sources.contains(file))
.collect()
}
pub fn endpoints(runtime: &RustRuntime) -> impl IntoIterator<Item = (String, ApiBuilder)> {
let artifact_proto_sources = runtime
.available_artifacts
.iter()
.map(|(artifact_id, service_factory)| {
(
artifact_id.clone(),
service_factory.artifact_protobuf_spec(),
)
})
.collect::<HashMap<_, _>>();
let exonum_sources = exonum_proto_sources();
let filtered_sources = artifact_proto_sources
.into_iter()
.map(|(artifact_id, sources)| {
let mut proto = sources.sources;
proto.extend(filter_exonum_proto_sources(
sources.includes,
&exonum_sources,
));
(artifact_id, proto)
})
.collect::<HashMap<_, _>>();
let mut builder = ApiBuilder::new();
builder
.public_scope()
.endpoint("proto-sources", {
move |query: ProtoSourcesQuery| -> api::Result<Vec<ProtoSourceFile>> {
if let ProtoSourcesQuery::Artifact { name, version } = query {
let artifact_id = ArtifactId::new(RuntimeIdentifier::Rust, name, version)
.map_err(|e| {
api::Error::bad_request()
.title("Invalid query")
.detail(format!("Invalid artifact query: {}", e))
})?;
filtered_sources.get(&artifact_id).cloned().ok_or_else(|| {
api::Error::not_found()
.title("Artifact sources not found")
.detail(format!(
"Unable to find sources for artifact {}",
artifact_id
))
})
} else {
Ok(exonum_sources.clone())
}
}
});
std::iter::once((["runtimes/", RustRuntime::NAME].concat(), builder))
}