]> git.proxmox.com Git - rustc.git/blame - src/librustdoc/externalfiles.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustdoc / externalfiles.rs
CommitLineData
1a4d82fc
JJ
1// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
c34b1796
AL
11use std::fs::File;
12use std::io::prelude::*;
13use std::io;
14use std::path::{PathBuf, Path};
15use std::str;
1a4d82fc
JJ
16
17#[derive(Clone)]
18pub struct ExternalHtml{
19 pub in_header: String,
20 pub before_content: String,
21 pub after_content: String
22}
23
24impl ExternalHtml {
25 pub fn load(in_header: &[String], before_content: &[String], after_content: &[String])
26 -> Option<ExternalHtml> {
27 match (load_external_files(in_header),
28 load_external_files(before_content),
29 load_external_files(after_content)) {
30 (Some(ih), Some(bc), Some(ac)) => Some(ExternalHtml {
31 in_header: ih,
32 before_content: bc,
33 after_content: ac
34 }),
35 _ => None
36 }
37 }
38}
39
c34b1796 40pub fn load_string(input: &Path) -> io::Result<Option<String>> {
54a0048b 41 let mut f = File::open(input)?;
c34b1796 42 let mut d = Vec::new();
54a0048b 43 f.read_to_end(&mut d)?;
85aaf69f 44 Ok(str::from_utf8(&d).map(|s| s.to_string()).ok())
1a4d82fc
JJ
45}
46
47macro_rules! load_or_return {
48 ($input: expr, $cant_read: expr, $not_utf8: expr) => {
49 {
c34b1796 50 let input = PathBuf::from(&$input[..]);
1a4d82fc
JJ
51 match ::externalfiles::load_string(&input) {
52 Err(e) => {
c34b1796 53 let _ = writeln!(&mut io::stderr(),
1a4d82fc
JJ
54 "error reading `{}`: {}", input.display(), e);
55 return $cant_read;
56 }
57 Ok(None) => {
c34b1796 58 let _ = writeln!(&mut io::stderr(),
1a4d82fc
JJ
59 "error reading `{}`: not UTF-8", input.display());
60 return $not_utf8;
61 }
62 Ok(Some(s)) => s
63 }
64 }
65 }
66}
67
68pub fn load_external_files(names: &[String]) -> Option<String> {
69 let mut out = String::new();
85aaf69f
SL
70 for name in names {
71 out.push_str(&*load_or_return!(&name, None, None));
1a4d82fc
JJ
72 out.push('\n');
73 }
74 Some(out)
75}