Skip to main content

clamp_volume_with

Function clamp_volume_with 

Source
pub fn clamp_volume_with(policy: VolumePolicy, current: u8, requested: u8) -> u8
Expand description

Returns the volume level to apply for requested while current is in force, using policy.

Both values are first limited to the protocol’s 0-100 range, so the result is always a level the server accepts. A request at or below VolumePolicy::threshold is applied as asked, whether it raises or lowers the level. Above the threshold, one request may raise the level by at most VolumePolicy::max_step; a lower request is applied as asked, because the limit bounds raises only. When VolumePolicy::threshold_floor is set, such a request may also rise to the threshold itself, so a player at 1 reaches 40 in one command instead of 21.

A policy whose fields exceed 100 is not rejected here, because the configuration validation rejects it before a connection exists; the values are limited to 100 so this function still returns a level the server accepts.

let policy = VolumePolicy {
    threshold: 40,
    max_step: 20,
    threshold_floor: true,
};
assert_eq!(clamp_volume_with(policy, 1, 100), 40);
assert_eq!(clamp_volume_with(policy, 40, 100), 60);
assert_eq!(clamp_volume_with(policy, 100, 90), 90);