summaryrefslogtreecommitdiff
path: root/src/bin/sp-meta.rs
blob: cc4a7bd221e5aebd3dd05b64ec005d4bb814a901 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use anyhow::Result;
use std::convert::TryFrom;
use std::path::{Path, PathBuf};

use structopt::StructOpt;

use subplot::Document;

#[derive(Debug, StructOpt)]
#[structopt(name = "sp-meta", about = "Show Subplot document metadata.")]
struct Opt {
    // Input filename.
    #[structopt(parse(from_os_str))]
    filename: PathBuf,
}

struct Metadata {
    sources: Vec<String>,
    title: String,
    binding_files: Vec<String>,
    function_files: Vec<String>,
    bibliographies: Vec<String>,
    scenarios: Vec<String>,
}

impl TryFrom<&mut Document> for Metadata {
    type Error = subplot::SubplotError;
    fn try_from(doc: &mut Document) -> std::result::Result<Self, Self::Error> {
        let sources: Vec<_> = doc
            .sources()
            .into_iter()
            .map(|p| filename(Some(&p)))
            .collect();
        let title = doc.meta().title().to_owned();
        let binding_files = doc
            .meta()
            .bindings_filenames()
            .into_iter()
            .map(|p| filename(Some(&p)))
            .collect();
        let function_files = doc
            .meta()
            .functions_filenames()
            .into_iter()
            .map(|p| filename(Some(&p)))
            .collect();
        let bibliographies = doc
            .meta()
            .bibliographies()
            .into_iter()
            .map(|p| filename(Some(&p)))
            .collect();
        let scenarios = doc
            .scenarios()?
            .into_iter()
            .map(|s| s.title().to_owned())
            .collect();
        Ok(Self {
            sources,
            title,
            binding_files,
            function_files,
            bibliographies,
            scenarios,
        })
    }
}

impl Metadata {
    fn write_list(v: &[String], prefix: &str) {
        v.iter().for_each(|entry| println!("{}: {}", prefix, entry))
    }

    fn write_out(&self) {
        Self::write_list(&self.sources, "source");
        println!("title: {}", self.title);
        Self::write_list(&self.binding_files, "bindings");
        Self::write_list(&self.function_files, "functions");
        Self::write_list(&self.bibliographies, "bibliography");
        Self::write_list(&self.scenarios, "scenario");
    }
}

fn main() -> Result<()> {
    let opt = Opt::from_args();
    let basedir = subplot::get_basedir_from(&opt.filename)?;
    let mut doc = Document::from_file(&basedir, &opt.filename)?;
    let meta = Metadata::try_from(&mut doc)?;

    meta.write_out();

    Ok(())
}

fn filename(name: Option<&Path>) -> String {
    let path = match name {
        None => return "".to_string(),
        Some(x) => x,
    };
    match path.to_str() {
        None => "non-UTF8 filename".to_string(),
        Some(x) => x.to_string(),
    }
}