1 /// Look for a directive in a comment string.
2 /// The directive is of the form "foo:" and should follow the leading `;` in the comment:
3 ///
4 /// ; dominates: block3 block4
5 ///
6 /// Return the comment text following the directive.
match_directive<'a>(comment: &'a str, directive: &str) -> Option<&'a str>7 pub fn match_directive<'a>(comment: &'a str, directive: &str) -> Option<&'a str> {
8 assert!(
9 directive.ends_with(':'),
10 "Directive must include trailing colon"
11 );
12 let text = comment.trim_start_matches(';').trim_start();
13 text.strip_prefix(directive).map(|s| s.trim())
14 }
15
16 #[test]
test_match_directive()17 fn test_match_directive() {
18 assert_eq!(match_directive("; foo: bar ", "foo:"), Some("bar"));
19 assert_eq!(match_directive(" foo:bar", "foo:"), Some("bar"));
20 assert_eq!(match_directive("foo:bar", "foo:"), Some("bar"));
21 assert_eq!(match_directive(";x foo: bar", "foo:"), None);
22 assert_eq!(match_directive(";;; foo: bar", "foo:"), Some("bar"));
23 }
24