bobashare_web

Function clamp_expiry

Source
pub fn clamp_expiry(
    max_expiry: Option<Duration>,
    other: Option<Duration>,
) -> Option<Duration>
Expand description

Take the requested expiry, and make sure it’s within the maximum expiry.

§Meaning of None

If the maximum expiry (max_expiry) is None, then any expiry will be allowed, including no expiry. If the requested expiry (other) is set to None, then it will return the maximum allowed expiry.

§Examples

Requesting no expiry with no maximum expiry:

let max_expiry = None;
assert_eq!(bobashare_web::clamp_expiry(max_expiry, None), None);

Requesting no expiry but a maximum expiry is set (gives the maximum allowed expiry):

let max_expiry = Some(Duration::days(7));
assert_eq!(bobashare_web::clamp_expiry(max_expiry, None), max_expiry);

Requesting an expiry with no maximum expiry:

let max_expiry = None;
assert_eq!(
    bobashare_web::clamp_expiry(max_expiry, Some(Duration::days(3))),
    Some(Duration::days(3)),
);

Requesting an expiry that’s within the maximum expiry:

let max_expiry = Some(Duration::days(7));
assert_eq!(
    bobashare_web::clamp_expiry(max_expiry, Some(Duration::days(3))),
    Some(Duration::days(3)),
);

Requesting an expiry that’s outside of the maximum expiry (clamps to the maximum expiry):

let max_expiry = Some(Duration::days(7));
assert_eq!(
    bobashare_web::clamp_expiry(max_expiry, Some(Duration::days(30))),
    max_expiry,
);