#[non_exhaustive]pub struct UnitRate {
pub unit: UsageUnit,
pub micro_usd: u64,
pub per: u64,
pub granularity: u64,
pub minimum_per_request: u64,
}Expand description
A price per unit of usage: micro_usd for every per units of unit.
granularity is the billing step: usage is billed in whole multiples of it, rounded
down, as a vendor billing “whole seconds, rounded down” bills audio milliseconds in
steps of 1,000. minimum_per_request is the least a request that uses the unit at all
is billed for, as a vendor billing “at least 50 frames per video request” does. The cost
of the billed amount rounds up to a whole micro-USD.
§Example
use adk_core::{UnitRate, UsageUnit};
// $2.50 per 1,000 images.
let rate = UnitRate::new(UsageUnit::Images, 2_500_000, 1_000);
assert_eq!(rate.cost_micro_usd(3), 7_500);
assert_eq!(rate.cost_micro_usd(0), 0);
// $0.20 per 1,000 frames, at least 50 frames per request.
let frames = UnitRate::new(UsageUnit::Frames, 200_000, 1_000).with_minimum_per_request(50);
assert_eq!(frames.cost_micro_usd(12), 10_000);
assert_eq!(frames.cost_micro_usd(900), 180_000);Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.unit: UsageUnitThe unit priced.
micro_usd: u64Micro-USD charged for every per units.
per: u64How many units micro_usd buys. Never zero.
granularity: u64Usage is billed in whole multiples of this many units, rounded down. Never zero.
minimum_per_request: u64The fewest units a request that uses the unit is billed for; zero for no minimum.
Implementations§
Source§impl UnitRate
impl UnitRate
Sourcepub const fn new(unit: UsageUnit, micro_usd: u64, per: u64) -> Self
pub const fn new(unit: UsageUnit, micro_usd: u64, per: u64) -> Self
micro_usd per per units of unit, every unit billed. A zero per is read as 1.
Sourcepub const fn rounded_down_to(self, granularity: u64) -> Self
pub const fn rounded_down_to(self, granularity: u64) -> Self
Bills usage in whole multiples of granularity units, rounded down. Zero is read as 1.
Sourcepub const fn with_minimum_per_request(self, minimum: u64) -> Self
pub const fn with_minimum_per_request(self, minimum: u64) -> Self
Bills every request that uses the unit for at least minimum units.
Sourcepub fn cost_micro_usd(&self, amount: u64) -> u64
pub fn cost_micro_usd(&self, amount: u64) -> u64
The cost in micro-USD of one request that used amount units, saturating at
u64::MAX.
The amount is rounded down to the billing granularity, then raised to the per-request minimum; a request that used none of the unit costs nothing.