faker_rust/default/
file.rs1use crate::base::sample;
4use crate::locale::{fetch_locale_with_context, sample_with_resolve};
5
6pub fn extension() -> String {
8 fetch_locale_with_context("file.extension", "en", Some("file"))
9 .map(|v| sample_with_resolve(&v, Some("file")))
10 .unwrap_or_else(|| sample(FALLBACK_EXTENSIONS).to_string())
11}
12
13pub fn mime_type() -> String {
15 fetch_locale_with_context("file.mime_type", "en", Some("file"))
16 .map(|v| sample_with_resolve(&v, Some("file")))
17 .unwrap_or_else(|| sample(FALLBACK_MIME_TYPES).to_string())
18}
19
20pub fn file_name(name: Option<&str>, extension: Option<&str>) -> String {
22 let name = name
23 .map(|s| s.to_string())
24 .unwrap_or_else(crate::lorem::word);
25 let extension = extension
26 .map(|s| s.to_string())
27 .unwrap_or_else(self::extension);
28 format!("{}.{}", name, extension)
29}
30
31const FALLBACK_EXTENSIONS: &[&str] = &["jpg", "png", "pdf", "docx", "zip", "mp3"];
33const FALLBACK_MIME_TYPES: &[&str] = &[
34 "image/jpeg",
35 "image/png",
36 "application/pdf",
37 "application/zip",
38 "audio/mpeg",
39];
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44
45 #[test]
46 fn test_extension() {
47 assert!(!extension().is_empty());
48 }
49
50 #[test]
51 fn test_file_name() {
52 let name = file_name(None, None);
53 assert!(name.contains('.'));
54 }
55}