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
mod json;
mod text;
#[macro_use]
mod ser;
use std::collections::HashMap;
use crate::crates::{Dependency, DependencyKind, Version};
pub(crate) use self::{
json::{Format, RenderAsJson},
text::RenderAsText,
};
pub(crate) type Dependencies = HashMap<DependencyKind, Vec<Dependency>>;
#[derive(serde::Serialize, Debug)]
pub struct SimpleModel<'a> {
pub name: &'a str,
pub version: &'a str,
pub yanked: bool,
}
impl<'a> SimpleModel<'a> {
#[inline]
pub fn from_version(version: &'a Version) -> Self {
Self {
name: &version.crate_,
version: &version.num,
yanked: version.yanked,
}
}
}
#[derive(Debug)]
pub struct FeaturesModel<'a> {
pub simple: SimpleModel<'a>,
pub version: &'a Version,
}
impl<'a> FeaturesModel<'a> {
#[inline]
pub fn from_version(version: &'a Version) -> Self {
Self {
simple: SimpleModel::from_version(&version),
version,
}
}
}
#[derive(Debug)]
pub struct DependencyModel<'a> {
pub simple: SimpleModel<'a>,
pub dependencies: Dependencies,
}
impl<'a> DependencyModel<'a> {
pub fn from_vers_deps(
version: &'a Version,
deps: impl IntoIterator<Item = Dependency>,
) -> Self {
Self {
simple: SimpleModel::from_version(&version),
dependencies: deps
.into_iter()
.fold(Dependencies::default(), |mut map, dep| {
map.entry(dep.kind).or_default().push(dep);
map
}),
}
}
}
#[derive(Debug)]
pub struct CompositeModel<'a> {
pub simple: SimpleModel<'a>,
pub features: FeaturesModel<'a>,
pub dependencies: DependencyModel<'a>,
}
impl<'a> CompositeModel<'a> {
pub fn from_vers_deps(
version: &'a Version,
deps: impl IntoIterator<Item = Dependency>,
) -> Self {
Self {
simple: SimpleModel::from_version(&version),
features: FeaturesModel::from_version(&version),
dependencies: DependencyModel::from_vers_deps(version, deps),
}
}
}
#[derive(Debug)]
pub struct SimpleListModel<'a> {
pub simple_list: Vec<SimpleModel<'a>>,
}
impl<'a> SimpleListModel<'a> {
#[inline]
pub fn new(list: Vec<SimpleModel<'a>>) -> Self {
Self { simple_list: list }
}
}
impl<'a> std::iter::FromIterator<SimpleModel<'a>> for SimpleListModel<'a> {
fn from_iter<T: IntoIterator<Item = SimpleModel<'a>>>(iter: T) -> Self {
Self {
simple_list: iter.into_iter().collect(),
}
}
}
#[derive(Debug)]
pub struct FeaturesListModel<'a> {
pub features_list: Vec<FeaturesModel<'a>>,
}
impl<'a> FeaturesListModel<'a> {
#[inline]
pub fn new(list: Vec<FeaturesModel<'a>>) -> Self {
Self {
features_list: list,
}
}
}
impl<'a> std::iter::FromIterator<FeaturesModel<'a>> for FeaturesListModel<'a> {
fn from_iter<T: IntoIterator<Item = FeaturesModel<'a>>>(iter: T) -> Self {
Self {
features_list: iter.into_iter().collect(),
}
}
}
#[derive(Debug)]
pub struct Output<'a, W> {
writer: &'a mut W,
}
impl<'a, W: std::io::Write> Output<'a, W> {
pub fn new(writer: &'a mut W) -> Self {
Self { writer }
}
pub fn write_json(&mut self, data: &impl serde::Serialize) {
data.render(self.writer, Format::Compact).unwrap();
}
pub fn write_text(&mut self, data: &impl RenderAsText) {
data.render(self.writer).unwrap()
}
}
pub fn render_as_json(
item: &impl serde::Serialize,
writer: &mut impl std::io::Write,
) -> std::io::Result<()> {
RenderAsJson::render(item, writer, Format::Compact)
}
pub fn render_as_text(
item: &impl RenderAsText,
writer: &mut impl std::io::Write,
) -> std::io::Result<()> {
RenderAsText::render(item, writer)
}