Skip to content

Commit b09a99e

Browse files
committed
Fix context serde instant serialization.
1 parent 61b6cd7 commit b09a99e

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "rustrict"
33
authors = ["Finn Bear"]
4-
version = "0.7.23"
4+
version = "0.7.24"
55
edition = "2021"
66
license = "MIT OR Apache-2.0"
77
repository = "https://github.com/finnbear/rustrict/"

src/context.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,11 @@ mod approx_instant {
620620
{
621621
let system_now = SystemTime::now();
622622
let instant_now = Instant::now();
623-
let approx = system_now - (instant_now - *instant);
623+
let approx = if instant_now > *instant {
624+
system_now - (instant_now - *instant)
625+
} else {
626+
system_now + (*instant - instant_now)
627+
};
624628
let millis = approx
625629
.duration_since(SystemTime::UNIX_EPOCH)
626630
.unwrap_or_default()
@@ -638,8 +642,13 @@ mod approx_instant {
638642
.checked_add(Duration::from_millis(millis))
639643
.unwrap_or(system_now);
640644
let instant_now = Instant::now();
641-
let duration = system_now.duration_since(de).map_err(Error::custom)?;
642-
let approx = instant_now - duration;
645+
let approx = if system_now > de {
646+
let duration = system_now.duration_since(de).map_err(Error::custom)?;
647+
instant_now - duration
648+
} else {
649+
let duration = de.duration_since(system_now).map_err(Error::custom)?;
650+
instant_now + duration
651+
};
643652
Ok(approx)
644653
}
645654
}
@@ -846,8 +855,15 @@ mod tests {
846855
#[test]
847856
#[cfg(feature = "serde")]
848857
fn serde() {
858+
use std::time::SystemTime;
859+
849860
let mut ctx = crate::Context::default();
850861
ctx.process("foo".to_string()).unwrap();
862+
ctx.restrict_for(Duration::from_secs(1000));
851863
println!("{}", serde_json::to_string(&ctx).unwrap());
864+
let json = serde_json::to_value(&ctx).unwrap();
865+
let only_safe_until = &json["only_safe_until"];
866+
let unix = only_safe_until.as_i64().unwrap();
867+
assert!(unix > 1000 + SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_millis() as i64)
852868
}
853869
}

0 commit comments

Comments
 (0)