Skip to content

Commit 09fbd99

Browse files
authored
Merge pull request #39 from GrandEngineering/dev
remove prototype trait
2 parents 148ea78 + 3c88a49 commit 09fbd99

File tree

8 files changed

+310
-132
lines changed

8 files changed

+310
-132
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

engine/proto/engine.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ service Engine {
88
rpc CreateTask(Task) returns (Task);
99
rpc DeleteTask(TaskSelector) returns (empty);
1010
rpc GetTasks(TaskPageRequest) returns (TaskPage);
11+
rpc CheckAuth(empty) returns (empty);
1112
}
1213
message TaskSelector {
1314
TaskState state = 1;

engine/src/bin/packer.rs

Lines changed: 163 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -110,69 +110,182 @@ async fn main() {
110110
buf.push(unw);
111111
}
112112
let ns = buf.join("\n");
113-
let mut file = File::create("schema.rustforge.toml").unwrap();
114-
file.write_all(ns.as_bytes()).unwrap();
113+
match File::create("schema.rustforge.toml") {
114+
Ok(mut file) => {
115+
if let Err(e) = file.write_all(ns.as_bytes()) {
116+
error!("Failed to write schema file: {}", e);
117+
} else {
118+
info!("Wrote schema.rustforge.toml");
119+
}
120+
}
121+
Err(e) => {
122+
error!("Failed to create schema file: {}", e);
123+
}
124+
}
115125
}
116126
Commands::Unpack(input) => {
117127
if input.input.exists() {
118-
let mut final_out: Vec<String> = Vec::new();
119128
info!("Unpacking File: {}", input.input.to_string_lossy());
120129
let mut buf = Vec::new();
121-
File::open(input.input)
122-
.unwrap()
123-
.read_to_end(&mut buf)
124-
.unwrap();
125-
let k: TaskQueue = bincode::deserialize(&buf).unwrap();
126-
for tasks in k.tasks {
127-
let tt = api.task_registry.tasks.get(&tasks.0.clone()).unwrap();
128-
for task in tasks.1 {
129-
if tt.verify(task.bytes.clone()) {
130-
let tmp_nt = tt.from_bytes(&task.bytes);
131-
final_out.push(format![
132-
r#"[["{}:{}"]]"#,
133-
tasks.0.0.clone(),
134-
tasks.0.1.clone()
135-
]);
136-
final_out.push(tmp_nt.to_toml());
137-
info!("{:?}", tmp_nt);
138-
};
130+
131+
// Attempt to open and read the input file. If either step fails,
132+
// we do not proceed to deserialization or writing the output file.
133+
match File::open(&input.input) {
134+
Ok(mut f) => {
135+
if let Err(e) = f.read_to_end(&mut buf) {
136+
error!(
137+
"Failed to read input file {}: {}",
138+
input.input.display(),
139+
e
140+
);
141+
// reading failed -> do not proceed to deserialize or write
142+
return;
143+
}
144+
}
145+
Err(e) => {
146+
error!("Failed to open input file {}: {}", input.input.display(), e);
147+
// opening failed -> do not proceed to deserialize or write
148+
return;
139149
}
140150
}
141-
let ns = final_out.join("\n");
142-
let mut file = File::create("output.rustforge.toml").unwrap();
143-
file.write_all(ns.as_bytes()).unwrap();
151+
152+
// Try to deserialize. Only on successful deserialization do we
153+
// process entries and write the output TOML file.
154+
let maybe_queue: Option<TaskQueue> =
155+
match bincode::deserialize::<TaskQueue>(&buf) {
156+
Ok(k) => Some(k),
157+
Err(e) => {
158+
error!("Failed to deserialize task queue: {}", e);
159+
None
160+
}
161+
};
162+
163+
if let Some(k) = maybe_queue {
164+
let mut final_out: Vec<String> = Vec::new();
165+
166+
for tasks in k.tasks {
167+
match api.task_registry.tasks.get(&tasks.0.clone()) {
168+
Some(tt) => {
169+
for task in tasks.1 {
170+
if tt.verify(task.bytes.clone()) {
171+
let tmp_nt = tt.from_bytes(&task.bytes);
172+
final_out.push(format![
173+
r#"[["{}:{}"]]"#,
174+
tasks.0.0.clone(),
175+
tasks.0.1.clone()
176+
]);
177+
final_out.push(tmp_nt.to_toml());
178+
info!("{:?}", tmp_nt);
179+
}
180+
}
181+
}
182+
None => {
183+
error!("Unknown template for {}:{}", tasks.0.0, tasks.0.1);
184+
}
185+
}
186+
}
187+
188+
let ns = final_out.join("\n");
189+
match File::create("output.rustforge.toml") {
190+
Ok(mut file) => {
191+
if let Err(e) = file.write_all(ns.as_bytes()) {
192+
error!("Failed to write output.rustforge.toml: {}", e);
193+
} else {
194+
info!("Wrote output.rustforge.toml");
195+
}
196+
}
197+
Err(e) => {
198+
error!("Failed to create output.rustforge.toml: {}", e);
199+
}
200+
}
201+
} else {
202+
// Deserialization failed; we logged the error above and intentionally do not
203+
// create/write the output file to avoid producing an empty output.
204+
}
144205
}
145206
}
146207
Commands::Pack(input) => {
147208
if input.input.exists() {
148209
info!("Packing File: {}", input.input.to_string_lossy());
149-
let toml_str = std::fs::read_to_string(input.input).unwrap();
150-
let raw: RawDoc = toml::from_str(&toml_str).unwrap();
151-
let entries = parse_entries(raw);
152-
for entry in entries {
153-
let template = api
154-
.task_registry
155-
.get(&ID(entry.namespace.as_str(), entry.id.as_str()))
156-
.unwrap();
157-
let toml_string = toml::to_string(&entry.data).unwrap();
158-
let t = template.from_toml(toml_string);
159-
let mut tmp = api
160-
.task_queue
161-
.tasks
162-
.get(&ID(entry.namespace.as_str(), entry.id.as_str()))
163-
.unwrap()
164-
.clone();
165-
tmp.push(StoredTask {
166-
id: "".into(), //ids are minted on the server
167-
bytes: t.to_bytes(),
168-
});
169-
api.task_queue
170-
.tasks
171-
.insert(ID(entry.namespace.as_str(), entry.id.as_str()), tmp);
210+
match std::fs::read_to_string(&input.input) {
211+
Ok(toml_str) => {
212+
match toml::from_str::<RawDoc>(&toml_str) {
213+
Ok(raw) => {
214+
let entries = parse_entries(raw);
215+
for entry in entries {
216+
match api
217+
.task_registry
218+
.get(&ID(entry.namespace.as_str(), entry.id.as_str()))
219+
{
220+
Some(template) => {
221+
match toml::to_string(&entry.data) {
222+
Ok(toml_string) => {
223+
let t = template.from_toml(toml_string);
224+
let key = ID(
225+
entry.namespace.as_str(),
226+
entry.id.as_str(),
227+
);
228+
let mut vec = api
229+
.task_queue
230+
.tasks
231+
.get(&key)
232+
.cloned()
233+
.unwrap_or_default();
234+
vec.push(StoredTask {
235+
id: "".into(), //ids are minted on the server
236+
bytes: t.to_bytes(),
237+
});
238+
api.task_queue.tasks.insert(key, vec);
239+
}
240+
Err(e) => {
241+
error!(
242+
"Failed to convert entry data to TOML string: {}",
243+
e
244+
);
245+
}
246+
}
247+
}
248+
None => {
249+
error!(
250+
"Template not found for {}:{}",
251+
entry.namespace, entry.id
252+
);
253+
}
254+
}
255+
}
256+
match bincode::serialize(&api.task_queue) {
257+
Ok(data) => match File::create("output.rustforge.bin") {
258+
Ok(mut file) => {
259+
if let Err(e) = file.write_all(&data) {
260+
error!(
261+
"Failed to write output.rustforge.bin: {}",
262+
e
263+
);
264+
} else {
265+
info!("Wrote output.rustforge.bin");
266+
}
267+
}
268+
Err(e) => {
269+
error!(
270+
"Failed to create output.rustforge.bin: {}",
271+
e
272+
);
273+
}
274+
},
275+
Err(e) => {
276+
error!("Failed to serialize task queue: {}", e);
277+
}
278+
}
279+
}
280+
Err(e) => {
281+
error!("Failed to parse input TOML: {}", e);
282+
}
283+
}
284+
}
285+
Err(e) => {
286+
error!("Failed to read input file {}: {}", input.input.display(), e);
287+
}
172288
}
173-
let data = bincode::serialize(&api.task_queue).unwrap();
174-
let mut file = File::create("output.rustforge.bin").unwrap();
175-
file.write_all(&data).unwrap();
176289
} else {
177290
error!("File does not exist: {}", input.input.to_string_lossy())
178291
}

0 commit comments

Comments
 (0)