esp_generate/lib.rs
1pub mod cargo;
2pub mod config;
3pub mod template;
4
5/// This turns a list of strings into a sentence, and appends it to the base string.
6///
7/// # Example
8///
9/// ```rust
10/// # use esp_generate::append_list_as_sentence;
11/// let list = &["foo", "bar", "baz"];
12/// let sentence = append_list_as_sentence("Here is a sentence.", "My elements are", list);
13/// assert_eq!(sentence, "Here is a sentence. My elements are `foo`, `bar` and `baz`.");
14///
15/// let list = &["foo", "bar", "baz"];
16/// let sentence = append_list_as_sentence("The following list is problematic:", "", list);
17/// assert_eq!(sentence, "The following list is problematic: `foo`, `bar` and `baz`.");
18/// ```
19pub fn append_list_as_sentence<S: AsRef<str>>(base: &str, word: &str, els: &[S]) -> String {
20 if !els.is_empty() {
21 let mut requires = String::new();
22
23 if !base.is_empty() {
24 requires.push_str(base);
25 requires.push(' ');
26 }
27
28 for (i, r) in els.iter().enumerate() {
29 if i == 0 {
30 if !word.is_empty() {
31 requires.push_str(word);
32 requires.push(' ');
33 }
34 } else if i == els.len() - 1 {
35 requires.push_str(" and ");
36 } else {
37 requires.push_str(", ");
38 };
39
40 requires.push('`');
41 requires.push_str(r.as_ref());
42 requires.push('`');
43 }
44 requires.push('.');
45
46 requires
47 } else {
48 base.to_string()
49 }
50}