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
use dev_prefix::*;
use super::types::*;

/// format `ArtNames` in a reasonable way
pub fn fmt_names(names: &[ArtNameRc]) -> String {
    if names.is_empty() {
        return "".to_string();
    }
    names.iter().map(|n| &n.raw).cloned().collect::<Vec<_>>().join(", ")
}

/// use several configuration options and pieces of data to represent
/// how the artifact should be formatted
pub fn fmt_artifact(name: &ArtNameRc,
                    artifacts: &Artifacts,
                    fmtset: &FmtSettings,
                    recurse: u8,
                    displayed: &mut ArtNames)
                    -> FmtArtifact {
    let artifact = artifacts.get(name).unwrap();
    let mut out = FmtArtifact::default();
    out.long = fmtset.long;
    if fmtset.path {
        out.path = Some(artifact.path.clone());
    }
    if fmtset.parts {
        let mut parts: Vec<FmtArtifact> = Vec::new();
        for p in &artifact.parts {
            let mut part;
            if recurse == 0 || displayed.contains(p) {
                part = FmtArtifact::default();
                part.name = p.clone();
            } else {
                part = fmt_artifact(p, artifacts, fmtset, recurse - 1, displayed);
                displayed.insert(p.clone());
            }
            parts.push(part);
        }
        parts.sort_by_key(|p| p.name.clone()); // TODO: get around clone here
        out.parts = Some(parts);
    }
    if fmtset.partof {
        let mut partof = artifact.partof.iter().cloned().collect::<Vec<ArtNameRc>>();
        partof.sort();
        let partof = partof.drain(0..)
            .map(|n| FmtArtifact { name: n, ..FmtArtifact::default() })
            .collect();
        out.partof = Some(partof);
    }
    if fmtset.loc_path {
        out.done = match artifact.done {
            Done::Code(ref l) => Some(l.to_string()),
            Done::Defined(ref d) => Some(d.clone()),
            Done::NotDone => Some("".to_string()),
        };
    }
    if fmtset.text {
        if fmtset.long {
            out.text = Some(artifact.text.clone());
        } else {
            // return only the first "line" according to markdown
            let mut s = String::new();
            for l in artifact.text.lines() {
                let l = l.trim();
                if l == "" {
                    break;
                }
                s.write_str(l).unwrap();
                s.push(' ');
            }
            out.text = Some(s);
        }
    }
    out.name = name.clone();
    out
}