Skip to content

Commit

Permalink
TimeResponse::Now is struct-like
Browse files Browse the repository at this point in the history
  • Loading branch information
adwhit committed Nov 4, 2024
1 parent cb86c69 commit 0e58b02
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
11 changes: 8 additions & 3 deletions crux_time/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn get_timer_id() -> TimerId {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum TimeResponse {
Now(Instant),
Now { instant: Instant },
InstantArrived { id: TimerId },
DurationElapsed { id: TimerId },
Cleared { id: TimerId },
Expand Down Expand Up @@ -231,10 +231,15 @@ mod test {

#[test]
fn test_serializing_the_response_types_as_json() {
let now = TimeResponse::Now(Instant::new(1, 2).expect("valid instant"));
let now = TimeResponse::Now {
instant: Instant::new(1, 2).expect("valid instant"),
};

let serialized = serde_json::to_string(&now).unwrap();
assert_eq!(&serialized, r#"{"now":{"seconds":1,"nanos":2}}"#);
assert_eq!(
&serialized,
r#"{"now":{"instant":{"seconds":1,"nanos":2}}}"#
);

let deserialized: TimeResponse = serde_json::from_str(&serialized).unwrap();
assert_eq!(now, deserialized);
Expand Down
12 changes: 7 additions & 5 deletions crux_time/tests/time_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ mod shared {
}
}),
Event::Set(time) => {
if let TimeResponse::Now(time) = time {
let time: DateTime<Utc> = time.try_into().unwrap();
if let TimeResponse::Now { instant } = time {
let time: DateTime<Utc> = instant.try_into().unwrap();
model.time = time.to_rfc3339();
caps.render.render()
}
Expand Down Expand Up @@ -151,8 +151,8 @@ mod shell {

let effs = match msg {
Some(CoreMessage::Event(m)) => core.process_event(m),
Some(CoreMessage::Response(Outcome::Time(mut request, result))) => {
core.resolve(&mut request, TimeResponse::Now(result))
Some(CoreMessage::Response(Outcome::Time(mut request, instant))) => {
core.resolve(&mut request, TimeResponse::Now { instant })
}
_ => vec![],
};
Expand Down Expand Up @@ -201,7 +201,9 @@ mod tests {
.expect_time();

let now: DateTime<Utc> = "2022-12-01T01:47:12.746202562+00:00".parse().unwrap();
let response = TimeResponse::Now(now.try_into().unwrap());
let response = TimeResponse::Now {
instant: now.try_into().unwrap(),
};
let _update = app.resolve_to_event_then_update(request, response, &mut model);

assert_eq!(app.view(&model).time, "2022-12-01T01:47:12.746202562+00:00");
Expand Down

0 comments on commit 0e58b02

Please sign in to comment.