| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
pub fn format_cents(cents: i64) -> String { |
| 5 |
if cents == 0 { |
| 6 |
"$0".to_string() |
| 7 |
} else { |
| 8 |
format!("${}.{:02}", cents / 100, cents.abs() % 100) |
| 9 |
} |
| 10 |
} |
| 11 |
|
| 12 |
|
| 13 |
pub fn format_price(cents: i32) -> String { |
| 14 |
if cents == 0 { |
| 15 |
"Free".to_string() |
| 16 |
} else { |
| 17 |
format!("${}.{:02}", cents / 100, cents % 100) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
pub fn format_tier(tier: &str) -> &str { |
| 23 |
match tier { |
| 24 |
"basic" => "Basic", |
| 25 |
"small_files" => "Small Files", |
| 26 |
"big_files" => "Big Files", |
| 27 |
"streaming" => "Streaming", |
| 28 |
_ => tier, |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
pub fn format_project_type(pt: &str) -> &str { |
| 34 |
match pt { |
| 35 |
"software" => "Software", |
| 36 |
"music" => "Music", |
| 37 |
"blog" => "Blog", |
| 38 |
"video" => "Video", |
| 39 |
"audio" => "Audio", |
| 40 |
"art" => "Art", |
| 41 |
"writing" => "Writing", |
| 42 |
"education" => "Education", |
| 43 |
other => other, |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
pub fn format_item_type(it: &str) -> &str { |
| 49 |
match it { |
| 50 |
"audio" => "Audio", |
| 51 |
"text" => "Text", |
| 52 |
"video" => "Video", |
| 53 |
"image" => "Image", |
| 54 |
"plugin" => "Plugin", |
| 55 |
"preset" => "Preset", |
| 56 |
"sample" => "Sample", |
| 57 |
"course" => "Course", |
| 58 |
"template" => "Template", |
| 59 |
"digital" => "Digital", |
| 60 |
"bundle" => "Bundle", |
| 61 |
other => other, |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
#[cfg(test)] |
| 66 |
mod tests { |
| 67 |
use super::*; |
| 68 |
|
| 69 |
#[test] |
| 70 |
fn format_cents_zero() { |
| 71 |
assert_eq!(format_cents(0), "$0"); |
| 72 |
} |
| 73 |
|
| 74 |
#[test] |
| 75 |
fn format_cents_positive() { |
| 76 |
assert_eq!(format_cents(999), "$9.99"); |
| 77 |
assert_eq!(format_cents(100), "$1.00"); |
| 78 |
assert_eq!(format_cents(1050), "$10.50"); |
| 79 |
} |
| 80 |
|
| 81 |
#[test] |
| 82 |
fn format_price_free() { |
| 83 |
assert_eq!(format_price(0), "Free"); |
| 84 |
} |
| 85 |
|
| 86 |
#[test] |
| 87 |
fn format_price_nonzero() { |
| 88 |
assert_eq!(format_price(550), "$5.50"); |
| 89 |
assert_eq!(format_price(1299), "$12.99"); |
| 90 |
} |
| 91 |
|
| 92 |
#[test] |
| 93 |
fn format_tier_known() { |
| 94 |
assert_eq!(format_tier("basic"), "Basic"); |
| 95 |
assert_eq!(format_tier("small_files"), "Small Files"); |
| 96 |
assert_eq!(format_tier("streaming"), "Streaming"); |
| 97 |
} |
| 98 |
|
| 99 |
#[test] |
| 100 |
fn format_tier_unknown() { |
| 101 |
assert_eq!(format_tier("custom"), "custom"); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
fn format_project_type_known() { |
| 106 |
assert_eq!(format_project_type("software"), "Software"); |
| 107 |
assert_eq!(format_project_type("music"), "Music"); |
| 108 |
} |
| 109 |
|
| 110 |
#[test] |
| 111 |
fn format_item_type_known() { |
| 112 |
assert_eq!(format_item_type("audio"), "Audio"); |
| 113 |
assert_eq!(format_item_type("plugin"), "Plugin"); |
| 114 |
} |
| 115 |
|
| 116 |
#[test] |
| 117 |
fn format_item_type_unknown() { |
| 118 |
assert_eq!(format_item_type("other_thing"), "other_thing"); |
| 119 |
} |
| 120 |
} |
| 121 |
|