guppy_summaries/
report.rs

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) The cargo-guppy Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::{
    diff::{changed_sort_key, PackageDiff, SummaryDiff, SummaryDiffStatus},
    SummaryId,
};
use std::fmt;

/// A report of a diff between two summaries.
///
/// This report can be generated or written to a file through `fmt::Display`.
#[derive(Clone, Debug)]
pub struct SummaryReport<'a, 'b> {
    diff: &'b SummaryDiff<'a>,
    sorted_target: Vec<(&'a SummaryId, &'b SummaryDiffStatus<'a>)>,
    sorted_host: Vec<(&'a SummaryId, &'b SummaryDiffStatus<'a>)>,
}

impl<'a, 'b> SummaryReport<'a, 'b> {
    /// Creates a new `SummaryReport` that can be displayed.
    pub fn new(diff: &'b SummaryDiff<'a>) -> Self {
        let sorted_target = Self::make_sorted(&diff.target_packages);
        let sorted_host = Self::make_sorted(&diff.host_packages);

        Self {
            diff,
            sorted_target,
            sorted_host,
        }
    }

    fn make_sorted(
        packages: &'b PackageDiff<'a>,
    ) -> Vec<(&'a SummaryId, &'b SummaryDiffStatus<'a>)> {
        let mut v: Vec<_> = packages
            .changed
            .iter()
            .map(|(summary_id, status)| (*summary_id, status))
            .collect();
        v.sort_by_key(|(summary_id, status)| changed_sort_key(summary_id, status));

        v
    }
}

impl<'a, 'b> fmt::Display for SummaryReport<'a, 'b> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if !self.diff.target_packages.is_unchanged() {
            writeln!(
                f,
                "target packages:\n{}",
                PackageReport::new(&self.diff.target_packages, &self.sorted_target)
            )?;
        }
        if !self.diff.host_packages.is_unchanged() {
            writeln!(
                f,
                "host packages:\n{}",
                PackageReport::new(&self.diff.host_packages, &self.sorted_host)
            )?;
        }

        Ok(())
    }
}

// Collapse the lifetime params into one because three is too annoying, all the params here are
// covariant anyway, and this is an internal struct.
struct PackageReport<'x> {
    package_diff: &'x PackageDiff<'x>,
    sorted: &'x [(&'x SummaryId, &'x SummaryDiffStatus<'x>)],
}

impl<'x> PackageReport<'x> {
    fn new(
        package_diff: &'x PackageDiff<'x>,
        sorted: &'x [(&'x SummaryId, &'x SummaryDiffStatus<'x>)],
    ) -> Self {
        Self {
            package_diff,
            sorted,
        }
    }
}

impl<'x> fmt::Display for PackageReport<'x> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for (summary_id, status) in self.sorted {
            write!(
                f,
                "  {} {} {} ({}, {})",
                status.tag(),
                summary_id.name,
                summary_id.version,
                status.latest_status(),
                summary_id.source
            )?;

            // Print out other versions if available.
            if let Some(unchanged_list) = self.package_diff.unchanged.get(summary_id.name.as_str())
            {
                write!(f, " (other versions: ")?;
                display_list(f, unchanged_list.iter().map(|(version, _, _)| *version))?;
                write!(f, ")")?;
            }

            writeln!(f)?;

            match status {
                SummaryDiffStatus::Added { info } => {
                    write!(f, "    * features: ")?;
                    display_list(f, &info.features)?;
                    writeln!(f)?;
                }
                SummaryDiffStatus::Removed { old_info } => {
                    write!(f, "    * (old features: ")?;
                    display_list(f, &old_info.features)?;
                    writeln!(f, ")")?;
                }
                SummaryDiffStatus::Modified {
                    old_version,
                    old_source,
                    old_status,
                    // The new status is printed in the package header.
                    new_status: _,
                    added_features,
                    removed_features,
                    unchanged_features,
                    added_optional_deps,
                    removed_optional_deps,
                    unchanged_optional_deps,
                } => {
                    if let Some(old_version) = old_version {
                        let change_str = if summary_id.version > **old_version {
                            "upgraded"
                        } else {
                            "DOWNGRADED"
                        };
                        writeln!(f, "    * version {} from {}", change_str, old_version)?;
                    }
                    if let Some(old_source) = old_source {
                        writeln!(f, "    * source changed from {}", old_source)?;
                    }
                    if let Some(old_status) = old_status {
                        writeln!(f, "    * status changed from {}", old_status)?;
                    }

                    // ---

                    if !added_features.is_empty() {
                        write!(f, "    * added features: ")?;
                        display_list(f, added_features.iter().copied())?;
                        writeln!(f)?;
                    }
                    if !removed_features.is_empty() {
                        write!(f, "    * removed features: ")?;
                        display_list(f, removed_features.iter().copied())?;
                        writeln!(f)?;
                    }
                    write!(f, "    * (unchanged features: ")?;
                    display_list(f, unchanged_features.iter().copied())?;
                    writeln!(f, ")")?;

                    // ---

                    if !added_optional_deps.is_empty() {
                        write!(f, "    * added optional dependencies: ")?;
                        display_list(f, added_optional_deps.iter().copied())?;
                        writeln!(f)?;
                    }
                    if !removed_optional_deps.is_empty() {
                        write!(f, "    * removed optional dependencies: ")?;
                        display_list(f, removed_optional_deps.iter().copied())?;
                        writeln!(f)?;
                    }
                    write!(f, "    * (unchanged optional dependencies: ")?;
                    display_list(f, unchanged_optional_deps.iter().copied())?;
                    writeln!(f, ")")?;
                }
            }
        }

        Ok(())
    }
}

fn display_list<I>(f: &mut fmt::Formatter, items: I) -> fmt::Result
where
    I: IntoIterator,
    I::Item: fmt::Display,
    I::IntoIter: ExactSizeIterator,
{
    let items = items.into_iter();
    let len = items.len();
    if len == 0 {
        write!(f, "[none]")?;
    }

    for (idx, item) in items.enumerate() {
        write!(f, "{}", item)?;
        // Add a comma for all items except the last one.
        if idx + 1 < len {
            write!(f, ", ")?;
        }
    }

    Ok(())
}