1
- use std:: fs:: { create_dir_all, File } ;
1
+ use std:: fs:: { create_dir_all, read_to_string , File } ;
2
2
use std:: io:: prelude:: * ;
3
- use std:: path:: PathBuf ;
3
+ use std:: path:: Path ;
4
4
5
5
use rustc_feature:: UnstableFeatures ;
6
6
use rustc_span:: edition:: Edition ;
7
7
use rustc_span:: source_map:: DUMMY_SP ;
8
8
9
9
use crate :: config:: { Options , RenderOptions } ;
10
- use crate :: externalfiles:: { load_string, LoadStringError } ;
11
10
use crate :: html:: escape:: Escape ;
12
11
use crate :: html:: markdown;
13
12
use crate :: html:: markdown:: { find_testable_code, ErrorCodes , IdMap , Markdown , MarkdownWithToc } ;
@@ -34,17 +33,16 @@ fn extract_leading_metadata(s: &str) -> (Vec<&str>, &str) {
34
33
35
34
/// Render `input` (e.g., "foo.md") into an HTML file in `output`
36
35
/// (e.g., output = "bar" => "bar/foo.html").
37
- pub fn render (
38
- input : PathBuf ,
36
+ pub fn render < P : AsRef < Path > > (
37
+ input : P ,
39
38
options : RenderOptions ,
40
- diag : & rustc_errors:: Handler ,
41
39
edition : Edition ,
42
- ) -> i32 {
40
+ ) -> Result < ( ) , String > {
43
41
if let Err ( e) = create_dir_all ( & options. output ) {
44
- diag. struct_err ( & format ! ( "{}: {}" , options. output. display( ) , e) ) . emit ( ) ;
45
- return 4 ;
42
+ return Err ( format ! ( "{}: {}" , options. output. display( ) , e) ) ;
46
43
}
47
44
45
+ let input = input. as_ref ( ) ;
48
46
let mut output = options. output ;
49
47
output. push ( input. file_name ( ) . unwrap ( ) ) ;
50
48
output. set_extension ( "html" ) ;
@@ -55,26 +53,15 @@ pub fn render(
55
53
css. push_str ( & s)
56
54
}
57
55
58
- let input_str = match load_string ( & input, diag) {
59
- Ok ( s) => s,
60
- Err ( LoadStringError :: ReadFail ) => return 1 ,
61
- Err ( LoadStringError :: BadUtf8 ) => return 2 ,
62
- } ;
56
+ let input_str = read_to_string ( input) . map_err ( |err| format ! ( "{}: {}" , input. display( ) , err) ) ?;
63
57
let playground_url = options. markdown_playground_url . or ( options. playground_url ) ;
64
58
let playground = playground_url. map ( |url| markdown:: Playground { crate_name : None , url } ) ;
65
59
66
- let mut out = match File :: create ( & output) {
67
- Err ( e) => {
68
- diag. struct_err ( & format ! ( "{}: {}" , output. display( ) , e) ) . emit ( ) ;
69
- return 4 ;
70
- }
71
- Ok ( f) => f,
72
- } ;
60
+ let mut out = File :: create ( & output) . map_err ( |e| format ! ( "{}: {}" , output. display( ) , e) ) ?;
73
61
74
62
let ( metadata, text) = extract_leading_metadata ( & input_str) ;
75
63
if metadata. is_empty ( ) {
76
- diag. struct_err ( "invalid markdown file: no initial lines starting with `# ` or `%`" ) . emit ( ) ;
77
- return 5 ;
64
+ return Err ( "invalid markdown file: no initial lines starting with `# ` or `%`" . to_owned ( ) ) ;
78
65
}
79
66
let title = metadata[ 0 ] ;
80
67
@@ -122,22 +109,15 @@ pub fn render(
122
109
) ;
123
110
124
111
match err {
125
- Err ( e) => {
126
- diag. struct_err ( & format ! ( "cannot write to `{}`: {}" , output. display( ) , e) ) . emit ( ) ;
127
- 6
128
- }
129
- Ok ( _) => 0 ,
112
+ Err ( e) => Err ( format ! ( "cannot write to `{}`: {}" , output. display( ) , e) ) ,
113
+ Ok ( _) => Ok ( ( ) ) ,
130
114
}
131
115
}
132
116
133
117
/// Runs any tests/code examples in the markdown file `input`.
134
- pub fn test ( mut options : Options , diag : & rustc_errors:: Handler ) -> i32 {
135
- let input_str = match load_string ( & options. input , diag) {
136
- Ok ( s) => s,
137
- Err ( LoadStringError :: ReadFail ) => return 1 ,
138
- Err ( LoadStringError :: BadUtf8 ) => return 2 ,
139
- } ;
140
-
118
+ pub fn test ( mut options : Options ) -> Result < ( ) , String > {
119
+ let input_str = read_to_string ( & options. input )
120
+ . map_err ( |err| format ! ( "{}: {}" , options. input. display( ) , err) ) ?;
141
121
let mut opts = TestOptions :: default ( ) ;
142
122
opts. no_crate_inject = true ;
143
123
opts. display_warnings = options. display_warnings ;
@@ -161,5 +141,5 @@ pub fn test(mut options: Options, diag: &rustc_errors::Handler) -> i32 {
161
141
collector. tests ,
162
142
Some ( testing:: Options :: new ( ) . display_output ( options. display_warnings ) ) ,
163
143
) ;
164
- 0
144
+ Ok ( ( ) )
165
145
}
0 commit comments