music21/common/decorators.rs
1//! music21's `common.decorators`, as far as it is not about Python objects.
2
3/// The warning music21's `deprecated` gives the first time a deprecated
4/// function is called: when it was deprecated, when it goes, and what to do
5/// instead, each with music21's wording where it is not given.
6///
7/// ```
8/// use music21::common::decorators::deprecation_message;
9///
10/// assert_eq!(
11/// deprecation_message("hi", None, None, None),
12/// "hi was deprecated and will disappear soon. Find alternative methods."
13/// );
14/// assert_eq!(
15/// deprecation_message("bye", Some("February 1972"), Some("September 2099"), Some("You should be okay.")),
16/// "bye was deprecated on February 1972 and will disappear at or after September 2099. You should be okay."
17/// );
18/// ```
19pub fn deprecation_message(
20 name: &str,
21 start_date: Option<&str>,
22 remove_date: Option<&str>,
23 message: Option<&str>,
24) -> String {
25 let start = start_date.map_or_else(String::new, |date| format!(" on {date}"));
26 let remove =
27 remove_date.map_or_else(|| "soon".to_string(), |date| format!("at or after {date}"));
28 let message = message.unwrap_or("Find alternative methods.");
29 format!("{name} was deprecated{start} and will disappear {remove}. {message}")
30}
31
32#[cfg(test)]
33mod tests {
34 use super::*;
35
36 /// Each part is filled in on its own.
37 #[test]
38 fn missing_parts_take_music21s_wording() {
39 assert_eq!(
40 deprecation_message("A.b", Some("v11"), None, Some("")),
41 "A.b was deprecated on v11 and will disappear soon. "
42 );
43 assert_eq!(
44 deprecation_message("f", None, Some("v12"), None),
45 "f was deprecated and will disappear at or after v12. Find alternative methods."
46 );
47 }
48}