1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
//! Module containing various utility functions. use reqwest::mime::Mime; /// App name and version to use with User-Agent request header. pub static USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); lazy_static! { /// Custom media type for the `Accept` header to get [projects](https://developer.github.com/v3/projects/) from the GH API. pub static ref GITHUB_PROJECTS_ACCEPT_MIMETYPE: Mime = "application/vnd.github.inertia-preview+json".parse().unwrap(); } /// Uppercase the first character of the supplied string. /// /// Based on http://stackoverflow.com/a/38406885/2851815 /// /// # Examples /// /// ``` /// # use dumplingh::util::uppercase_first; /// assert_eq!(&uppercase_first("abolish"), "Abolish"); /// ``` pub fn uppercase_first(s: &str) -> String { let mut c = s.chars(); match c.next() { None => String::new(), Some(f) => f.to_uppercase().collect::<String>() + c.as_str(), } } /// Get how many characters wide the string representation of a number is. /// /// In other words, the max padding for filenames to make sense. /// /// # Examples /// /// ``` /// # use dumplingh::util::width_for; /// assert_eq!(width_for(7), 1); /// assert_eq!(width_for(10), 2); /// assert_eq!(width_for(579), 3); /// ``` pub fn width_for(len: usize) -> usize { if len == 0 { 1 } else { (len as f64 + 1f64).log10().ceil() as usize } }