Skip to content

Commit a17b042

Browse files
committed
rustdoc: refactor code block language info into a struct.
Fields have names, unlike an anonymous tuple.
1 parent af622a4 commit a17b042

File tree

1 file changed

+68
-42
lines changed

1 file changed

+68
-42
lines changed

src/librustdoc/html/markdown.rs

+68-42
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
174174
slice::raw::buf_as_slice((*lang).data,
175175
(*lang).size as uint, |rlang| {
176176
let rlang = str::from_utf8(rlang).unwrap();
177-
let (_,_,_,notrust) = parse_lang_string(rlang);
178-
if notrust {
177+
if LangString::parse(rlang).notrust {
179178
(my_opaque.dfltblk)(ob, &buf, lang,
180179
opaque as *mut libc::c_void);
181180
true
@@ -309,16 +308,16 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
309308
lang: *hoedown_buffer, opaque: *mut libc::c_void) {
310309
unsafe {
311310
if text.is_null() { return }
312-
let (should_fail, no_run, ignore, notrust) = if lang.is_null() {
313-
(false, false, false, false)
311+
let block_info = if lang.is_null() {
312+
LangString::all_false()
314313
} else {
315314
slice::raw::buf_as_slice((*lang).data,
316315
(*lang).size as uint, |lang| {
317316
let s = str::from_utf8(lang).unwrap();
318-
parse_lang_string(s)
317+
LangString::parse(s)
319318
})
320319
};
321-
if notrust { return }
320+
if block_info.notrust { return }
322321
slice::raw::buf_as_slice((*text).data, (*text).size as uint, |text| {
323322
let opaque = opaque as *mut hoedown_html_renderer_state;
324323
let tests = &mut *((*opaque).opaque as *mut ::test::Collector);
@@ -327,7 +326,9 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
327326
stripped_filtered_line(l).unwrap_or(l)
328327
});
329328
let text = lines.collect::<Vec<&str>>().connect("\n");
330-
tests.add_test(text.to_string(), should_fail, no_run, ignore);
329+
tests.add_test(text.to_string(),
330+
block_info.should_fail, block_info.no_run,
331+
block_info.ignore);
331332
})
332333
}
333334
}
@@ -365,33 +366,49 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
365366
}
366367
}
367368

368-
fn parse_lang_string(string: &str) -> (bool,bool,bool,bool) {
369-
let mut seen_rust_tags = false;
370-
let mut seen_other_tags = false;
371-
let mut should_fail = false;
372-
let mut no_run = false;
373-
let mut ignore = false;
374-
let mut notrust = false;
375-
376-
let mut tokens = string.as_slice().split(|c: char|
377-
!(c == '_' || c == '-' || c.is_alphanumeric())
378-
);
379-
380-
for token in tokens {
381-
match token {
382-
"" => {},
383-
"should_fail" => { should_fail = true; seen_rust_tags = true; },
384-
"no_run" => { no_run = true; seen_rust_tags = true; },
385-
"ignore" => { ignore = true; seen_rust_tags = true; },
386-
"notrust" => { notrust = true; seen_rust_tags = true; },
387-
"rust" => { notrust = false; seen_rust_tags = true; },
388-
_ => { seen_other_tags = true }
369+
#[deriving(Eq, PartialEq, Clone, Show)]
370+
struct LangString {
371+
should_fail: bool,
372+
no_run: bool,
373+
ignore: bool,
374+
notrust: bool,
375+
}
376+
377+
impl LangString {
378+
fn all_false() -> LangString {
379+
LangString {
380+
should_fail: false,
381+
no_run: false,
382+
ignore: false,
383+
notrust: false,
389384
}
390385
}
391386

392-
let notrust = notrust || (seen_other_tags && !seen_rust_tags);
387+
fn parse(string: &str) -> LangString {
388+
let mut seen_rust_tags = false;
389+
let mut seen_other_tags = false;
390+
let mut data = LangString::all_false();
391+
392+
let mut tokens = string.as_slice().split(|c: char|
393+
!(c == '_' || c == '-' || c.is_alphanumeric())
394+
);
395+
396+
for token in tokens {
397+
match token {
398+
"" => {},
399+
"should_fail" => { data.should_fail = true; seen_rust_tags = true; },
400+
"no_run" => { data.no_run = true; seen_rust_tags = true; },
401+
"ignore" => { data.ignore = true; seen_rust_tags = true; },
402+
"notrust" => { data.notrust = true; seen_rust_tags = true; },
403+
"rust" => { data.notrust = false; seen_rust_tags = true; },
404+
_ => { seen_other_tags = true }
405+
}
406+
}
407+
408+
data.notrust |= seen_other_tags && !seen_rust_tags;
393409

394-
(should_fail, no_run, ignore, notrust)
410+
data
411+
}
395412
}
396413

397414
/// By default this markdown renderer generates anchors for each header in the
@@ -425,19 +442,28 @@ impl<'a> fmt::Show for MarkdownWithToc<'a> {
425442

426443
#[cfg(test)]
427444
mod tests {
428-
use super::parse_lang_string;
445+
use super::LangString;
429446

430447
#[test]
431-
fn test_parse_lang_string() {
432-
assert_eq!(parse_lang_string(""), (false,false,false,false))
433-
assert_eq!(parse_lang_string("rust"), (false,false,false,false))
434-
assert_eq!(parse_lang_string("sh"), (false,false,false,true))
435-
assert_eq!(parse_lang_string("notrust"), (false,false,false,true))
436-
assert_eq!(parse_lang_string("ignore"), (false,false,true,false))
437-
assert_eq!(parse_lang_string("should_fail"), (true,false,false,false))
438-
assert_eq!(parse_lang_string("no_run"), (false,true,false,false))
439-
assert_eq!(parse_lang_string("{.no_run .example}"), (false,true,false,false))
440-
assert_eq!(parse_lang_string("{.sh .should_fail}"), (true,false,false,false))
441-
assert_eq!(parse_lang_string("{.example .rust}"), (false,false,false,false))
448+
fn test_lang_string_parse() {
449+
fn t(s: &str, should_fail: bool, no_run: bool, ignore: bool, notrust: bool) {
450+
assert_eq!(LangString::parse(s), LangString {
451+
should_fail: should_fail,
452+
no_run: no_run,
453+
ignore: ignore,
454+
notrust: notrust,
455+
})
456+
}
457+
458+
t("", false,false,false,false);
459+
t("rust", false,false,false,false);
460+
t("sh", false,false,false,true);
461+
t("notrust", false,false,false,true);
462+
t("ignore", false,false,true,false);
463+
t("should_fail", true,false,false,false);
464+
t("no_run", false,true,false,false);
465+
t("{.no_run .example}", false,true,false,false);
466+
t("{.sh .should_fail}", true,false,false,false);
467+
t("{.example .rust}", false,false,false,false);
442468
}
443469
}

0 commit comments

Comments
 (0)