Skip to content

Commit aa54e7b

Browse files
committed
Remove DocData & Item
Instead, build the Documentation up itself. This means that we now have to have Documentation own its data, which feels like adding more allocations, but the reason we need to do this is that item itself was allocating.
1 parent ee6786d commit aa54e7b

File tree

4 files changed

+97
-171
lines changed

4 files changed

+97
-171
lines changed

src/item.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/json.rs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ const DATA_SIZE: usize = 1;
5252
/// - `data`: The top level crate information and documentation
5353
/// - `included`: The rest of the crate information and documentation
5454
#[derive(Serialize, Debug)]
55-
pub struct Documentation<'a> {
56-
data: Option<Document<'a>>,
57-
included: Option<Vec<Document<'a>>>,
55+
pub struct Documentation {
56+
data: Option<Document>,
57+
included: Option<Vec<Document>>,
5858
}
5959

6060
/// A sub type of the `Documentation` struct. It contains the majority of the data. It can be used
@@ -68,12 +68,12 @@ pub struct Documentation<'a> {
6868
/// - `relationships`: An optional field used to show the relationship between the crate to the
6969
/// otheritems in the crate
7070
#[derive(Serialize, Debug)]
71-
pub struct Document<'a> {
71+
pub struct Document {
7272
#[serde(rename = "type")]
73-
ty: &'a str,
74-
id: &'a str,
75-
attributes: HashMap<&'a str, &'a str>,
76-
relationships: Option<HashMap<&'a str, HashMap<&'a str, Vec<Data<'a>>>>>,
73+
ty: String,
74+
id: String,
75+
attributes: HashMap<String, String>,
76+
relationships: Option<HashMap<String, HashMap<String, Vec<Data>>>>,
7777
}
7878

7979
/// Used to populate the `relationships` `data` field in the serialized JSON
@@ -83,13 +83,13 @@ pub struct Document<'a> {
8383
/// - `ty`: The type of the the item (e.g. "crate", "function", "enum", etc.)
8484
/// - `id`: The unique identifier associated with this item
8585
#[derive(Serialize, Debug)]
86-
pub struct Data<'a> {
86+
pub struct Data {
8787
#[serde(rename = "type")]
88-
ty: &'a str,
89-
id: &'a str,
88+
ty: String,
89+
id: String,
9090
}
9191

92-
impl<'a> Documentation<'a> {
92+
impl Documentation {
9393
/// Create an empty `Documentation` struct
9494
pub fn new() -> Self {
9595
Self {
@@ -100,56 +100,56 @@ impl<'a> Documentation<'a> {
100100

101101
/// Set the `data` field of a `Documentation` struct to the value passed into the
102102
/// `data` argument
103-
pub fn data(mut self, data: Document<'a>) -> Self {
103+
pub fn data(mut self, data: Document) -> Self {
104104
self.data = Some(data);
105105
self
106106
}
107107

108108
/// Set the `included` field of a `Documentation` struct to the value passed into the
109109
/// `included` argument
110-
pub fn included(mut self, included: Vec<Document<'a>>) -> Self {
110+
pub fn included(mut self, included: Vec<Document>) -> Self {
111111
self.included = Some(included);
112112
self
113113
}
114114
}
115115

116-
impl<'a> Document<'a> {
116+
impl Document {
117117
/// Create an empty `Document` struct
118118
pub fn new() -> Self {
119119
Self {
120-
ty: "",
121-
id: "",
120+
ty: String::new(),
121+
id: String::new(),
122122
attributes: HashMap::new(),
123123
relationships: None,
124124
}
125125
}
126126

127127
/// Set the `ty` field of a `Document` struct to the value passed into the
128128
/// `t` argument
129-
pub fn ty(mut self, t: &'a str) -> Self {
129+
pub fn ty(mut self, t: String) -> Self {
130130
self.ty = t;
131131
self
132132
}
133133

134134
/// Set the `id` field of a `Document` struct to the value passed into the
135135
/// `id` argument
136-
pub fn id(mut self, id: &'a str) -> Self {
136+
pub fn id(mut self, id: String) -> Self {
137137
self.id = id;
138138
self
139139
}
140140

141141
/// Insert an attribute for the `attribute` field of a `Document` struct. If the current
142142
/// `attribute` exists it'll be overwritten with the given value, otherwise it'll just be
143143
/// created for the first time.
144-
pub fn attributes(mut self, attribute: &'a str, value: &'a str) -> Self {
144+
pub fn attributes(mut self, attribute: String, value: String) -> Self {
145145
self.attributes.insert(attribute, value);
146146
self
147147
}
148148

149149
/// Insert a relationship for the `relationships` field of a `Document` struct. If the current
150150
/// `ty` exists it'll be overwritten with the given `data` value, otherwise it'll just be
151151
/// created for the first time.
152-
pub fn relationships(&mut self, ty: &'a str, data: Vec<Data<'a>>) {
152+
pub fn relationships(&mut self, ty: String, data: Vec<Data>) {
153153
match self.relationships {
154154
// The code below allows us to handle with ease the fact we have data structured like:
155155
//
@@ -181,7 +181,7 @@ impl<'a> Document<'a> {
181181
// it to the `Document`.
182182
None => {
183183
let mut data_map = HashMap::with_capacity(DATA_SIZE);
184-
data_map.insert("data", data);
184+
data_map.insert(String::from("data"), data);
185185

186186
let mut relationships = HashMap::with_capacity(METADATA_SIZE);
187187
relationships.insert(ty, data_map);
@@ -191,28 +191,31 @@ impl<'a> Document<'a> {
191191
// type, etc.) and the data.
192192
Some(ref mut relationships) => {
193193
let mut data_map = HashMap::with_capacity(DATA_SIZE);
194-
data_map.insert("data", data);
194+
data_map.insert(String::from("data"), data);
195195

196196
relationships.insert(ty, data_map);
197197
}
198198
}
199199
}
200200
}
201201

202-
impl<'a> Data<'a> {
202+
impl Data {
203203
/// Create a new empty `Data` struct
204204
pub fn new() -> Self {
205-
Self { ty: "", id: "" }
205+
Self {
206+
ty: String::new(),
207+
id: String::new(),
208+
}
206209
}
207210

208211
/// Set the `ty` field of `Data` to the value passed to the `t` argument
209-
pub fn ty(mut self, t: &'a str) -> Self {
212+
pub fn ty(mut self, t: String) -> Self {
210213
self.ty = t;
211214
self
212215
}
213216

214217
/// Set the `id` field of `Data` to the value passed to the `id` argument
215-
pub fn id(mut self, id: &'a str) -> Self {
218+
pub fn id(mut self, id: String) -> Self {
216219
self.id = id;
217220
self
218221
}

0 commit comments

Comments
 (0)