Skip to content

Commit

Permalink
minor parsing refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dragazo committed Oct 27, 2023
1 parent f46d2d2 commit 27808b1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ impl<'gc, C: CustomTypes<S>, S: System<C>> Project<'gc, C, S> {

let mut locals = SymbolTable::default();
for field in fields.iter() {
let value = values.get(field).and_then(|x| SimpleValue::from_netsblox_json(x.clone()).ok().map(|x| Value::from_simple(mc, x))).unwrap_or_else(|| Number::new(0.0).unwrap().into());
let value = values.get(field).map(|x| Value::from_simple(mc, x.clone())).unwrap_or_else(|| Number::new(0.0).unwrap().into());
locals.define_or_redefine(field, value.into());
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,7 @@ pub enum OutgoingMessage<C: CustomTypes<S>, S: System<C>> {
}
pub struct IncomingMessage<C: CustomTypes<S>, S: System<C>> {
pub msg_type: String,
pub values: Vec<(String, Json)>,
pub values: Vec<(String, SimpleValue)>,
pub reply_key: Option<S::InternReplyKey>,
}

Expand Down
11 changes: 6 additions & 5 deletions src/std_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ async fn call_rpc_async<C: CustomTypes<StdSystem<C>>>(context: &Context, client:
let time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis();
let url = format!("{services_url}/{service}/{rpc}?clientId={client_id}&t={time}",
services_url = context.services_url, client_id = context.client_id);
let args: BTreeMap<&str, &Json> = args.iter().copied().collect();
let args = args.iter().copied().collect::<BTreeMap<_,_>>();

let res = match client.post(url).json(&args).send().await {
Ok(x) => x,
Expand All @@ -126,8 +126,8 @@ async fn call_rpc_async<C: CustomTypes<StdSystem<C>>>(context: &Context, client:
Ok(SimpleValue::Image(Image { content: res, center: None }))
} else if content_type.contains("audio/") {
Ok(SimpleValue::Audio(Audio { content: res }))
} else if let Some(x) = parse_json_slice::<Json>(&res).ok().and_then(|x| SimpleValue::from_netsblox_json(x).ok()) {
Ok(x)
} else if let Some(x) = parse_json_slice::<Json>(&res).ok() {
SimpleValue::from_netsblox_json(x).map_err(|e| format!("Received ill-formed success value: {e:?}"))
} else if let Ok(x) = String::from_utf8(res) {
Ok(SimpleValue::String(x))
} else {
Expand Down Expand Up @@ -231,7 +231,8 @@ impl<C: CustomTypes<StdSystem<C>>> StdSystem<C> {
}
false => None,
};
in_sender.send(IncomingMessage { msg_type, values: values.into_iter().collect(), reply_key }).unwrap();
let values = values.into_iter().filter_map(|(k, v)| SimpleValue::from_netsblox_json(v).ok().map(|v| (k, v))).collect();
in_sender.send(IncomingMessage { msg_type, values, reply_key }).unwrap();
}
}
_ => (),
Expand Down Expand Up @@ -359,7 +360,7 @@ impl<C: CustomTypes<StdSystem<C>>> StdSystem<C> {
}

/// Injects a message into the receiving queue as if received over the network.
pub fn inject_message(&self, msg_type: String, values: Vec<(String, Json)>) {
pub fn inject_message(&self, msg_type: String, values: Vec<(String, SimpleValue)>) {
self.message_injector.send(IncomingMessage { msg_type, values, reply_key: None }).unwrap();
}
}
Expand Down

0 comments on commit 27808b1

Please sign in to comment.