Skip to content

Commit 7a5742d

Browse files
authored
refactor(definitions)!: conversion functions (#47)
* refactor!(definitions): conversion functions * feat(definitions): add is_variant to `Definition`
1 parent 353d659 commit 7a5742d

11 files changed

+112
-27
lines changed

Cargo.lock

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ pedantic = { level = "warn", priority = -1 }
2121
anyhow = "1.0.95"
2222
bon = "3.3.2"
2323
clap = { version = "4.5.29", features = ["derive"] }
24-
derive_more = { version = "2.0.1", features = ["from", "display"] }
24+
derive_more = { version = "2.0.1", features = [
25+
"from",
26+
"display",
27+
"is_variant",
28+
] }
2529
dotenvy = "0.15.7"
2630
dunce = "1.0.5"
2731
figment = { version = "0.10.19", features = ["env", "toml"] }

src/definitions/constructor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ mod tests {
126126
.next()
127127
.unwrap();
128128
let def = ConstructorDefinition::extract(m).unwrap();
129-
def.as_constructor().unwrap()
129+
def.to_constructor().unwrap()
130130
}
131131

132132
#[test]

src/definitions/enumeration.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ mod tests {
140140
let cursor = output.create_tree_cursor();
141141
let m = cursor.query(vec![EnumDefinition::query()]).next().unwrap();
142142
let def = EnumDefinition::extract(m).unwrap();
143-
def.as_enum().unwrap()
143+
def.to_enum().unwrap()
144144
}
145145

146146
#[test]

src/definitions/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ mod tests {
124124
let cursor = output.create_tree_cursor();
125125
let m = cursor.query(vec![ErrorDefinition::query()]).next().unwrap();
126126
let def = ErrorDefinition::extract(m).unwrap();
127-
def.as_error().unwrap()
127+
def.to_error().unwrap()
128128
}
129129

130130
#[test]

src/definitions/event.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ mod tests {
124124
let cursor = output.create_tree_cursor();
125125
let m = cursor.query(vec![EventDefinition::query()]).next().unwrap();
126126
let def = EventDefinition::extract(m).unwrap();
127-
def.as_event().unwrap()
127+
def.to_event().unwrap()
128128
}
129129

130130
#[test]

src/definitions/function.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ mod tests {
204204
.next()
205205
.unwrap();
206206
let def = FunctionDefinition::extract(m).unwrap();
207-
def.as_function().unwrap()
207+
def.to_function().unwrap()
208208
}
209209

210210
#[test]

src/definitions/mod.rs

+89-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! definitions contained within.
77
//! Some helper functions allow to extract useful information from [`Cursor`]s.
88
use constructor::ConstructorDefinition;
9-
use derive_more::{Display, From};
9+
use derive_more::{Display, From, IsVariant};
1010
use enumeration::EnumDefinition;
1111
use error::ErrorDefinition;
1212
use event::EventDefinition;
@@ -169,7 +169,7 @@ pub enum Parent {
169169
}
170170

171171
/// A source item's definition
172-
#[derive(Debug, From)]
172+
#[derive(Debug, From, IsVariant)]
173173
pub enum Definition {
174174
Constructor(ConstructorDefinition),
175175
Enumeration(EnumDefinition),
@@ -238,72 +238,143 @@ impl Definition {
238238
}
239239
}
240240

241-
/// Retrieve the inner constructor definition
241+
/// Convert to the inner constructor definition
242242
#[must_use]
243-
pub fn as_constructor(self) -> Option<ConstructorDefinition> {
243+
pub fn to_constructor(self) -> Option<ConstructorDefinition> {
244244
match self {
245245
Definition::Constructor(def) => Some(def),
246246
_ => None,
247247
}
248248
}
249249

250-
/// Retrieve the inner enum definition
250+
/// Convert to the inner enum definition
251251
#[must_use]
252-
pub fn as_enum(self) -> Option<EnumDefinition> {
252+
pub fn to_enum(self) -> Option<EnumDefinition> {
253253
match self {
254254
Definition::Enumeration(def) => Some(def),
255255
_ => None,
256256
}
257257
}
258258

259-
/// Retrieve the inner error definition
259+
/// Convert to the inner error definition
260260
#[must_use]
261-
pub fn as_error(self) -> Option<ErrorDefinition> {
261+
pub fn to_error(self) -> Option<ErrorDefinition> {
262262
match self {
263263
Definition::Error(def) => Some(def),
264264
_ => None,
265265
}
266266
}
267267

268-
/// Retrieve the inner event definition
268+
/// Convert to the inner event definition
269269
#[must_use]
270-
pub fn as_event(self) -> Option<EventDefinition> {
270+
pub fn to_event(self) -> Option<EventDefinition> {
271271
match self {
272272
Definition::Event(def) => Some(def),
273273
_ => None,
274274
}
275275
}
276276

277-
/// Retrieve the inner function definition
277+
/// Convert to the inner function definition
278278
#[must_use]
279-
pub fn as_function(self) -> Option<FunctionDefinition> {
279+
pub fn to_function(self) -> Option<FunctionDefinition> {
280280
match self {
281281
Definition::Function(def) => Some(def),
282282
_ => None,
283283
}
284284
}
285285

286-
/// Retrieve the inner modifier definition
286+
/// Convert to the inner modifier definition
287287
#[must_use]
288-
pub fn as_modifier(self) -> Option<ModifierDefinition> {
288+
pub fn to_modifier(self) -> Option<ModifierDefinition> {
289289
match self {
290290
Definition::Modifier(def) => Some(def),
291291
_ => None,
292292
}
293293
}
294294

295-
/// Retrieve the inner struct definition
295+
/// Convert to the inner struct definition
296296
#[must_use]
297-
pub fn as_struct(self) -> Option<StructDefinition> {
297+
pub fn to_struct(self) -> Option<StructDefinition> {
298298
match self {
299299
Definition::Struct(def) => Some(def),
300300
_ => None,
301301
}
302302
}
303303

304-
/// Retrieve the inner variable declaration
304+
/// Convert to the inner variable declaration
305305
#[must_use]
306-
pub fn as_variable(self) -> Option<VariableDeclaration> {
306+
pub fn to_variable(self) -> Option<VariableDeclaration> {
307+
match self {
308+
Definition::Variable(def) => Some(def),
309+
_ => None,
310+
}
311+
}
312+
/// Reference to the inner constructor definition
313+
#[must_use]
314+
pub fn as_constructor(&self) -> Option<&ConstructorDefinition> {
315+
match self {
316+
Definition::Constructor(def) => Some(def),
317+
_ => None,
318+
}
319+
}
320+
321+
/// Reference to the inner enum definition
322+
#[must_use]
323+
pub fn as_enum(&self) -> Option<&EnumDefinition> {
324+
match self {
325+
Definition::Enumeration(def) => Some(def),
326+
_ => None,
327+
}
328+
}
329+
330+
/// Reference to the inner error definition
331+
#[must_use]
332+
pub fn as_error(&self) -> Option<&ErrorDefinition> {
333+
match self {
334+
Definition::Error(def) => Some(def),
335+
_ => None,
336+
}
337+
}
338+
339+
/// Reference to the inner event definition
340+
#[must_use]
341+
pub fn as_event(&self) -> Option<&EventDefinition> {
342+
match self {
343+
Definition::Event(def) => Some(def),
344+
_ => None,
345+
}
346+
}
347+
348+
/// Reference to the inner function definition
349+
#[must_use]
350+
pub fn as_function(&self) -> Option<&FunctionDefinition> {
351+
match self {
352+
Definition::Function(def) => Some(def),
353+
_ => None,
354+
}
355+
}
356+
357+
/// Reference to the inner modifier definition
358+
#[must_use]
359+
pub fn as_modifier(&self) -> Option<&ModifierDefinition> {
360+
match self {
361+
Definition::Modifier(def) => Some(def),
362+
_ => None,
363+
}
364+
}
365+
366+
/// Reference to the inner struct definition
367+
#[must_use]
368+
pub fn as_struct(&self) -> Option<&StructDefinition> {
369+
match self {
370+
Definition::Struct(def) => Some(def),
371+
_ => None,
372+
}
373+
}
374+
375+
/// Reference to the inner variable declaration
376+
#[must_use]
377+
pub fn as_variable(&self) -> Option<&VariableDeclaration> {
307378
match self {
308379
Definition::Variable(def) => Some(def),
309380
_ => None,

src/definitions/modifier.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ mod tests {
178178
.next()
179179
.unwrap();
180180
let def = ModifierDefinition::extract(m).unwrap();
181-
def.as_modifier().unwrap()
181+
def.to_modifier().unwrap()
182182
}
183183

184184
#[test]

src/definitions/structure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ mod tests {
150150
.next()
151151
.unwrap();
152152
let def = StructDefinition::extract(m).unwrap();
153-
def.as_struct().unwrap()
153+
def.to_struct().unwrap()
154154
}
155155

156156
#[test]

src/definitions/variable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ mod tests {
155155
.next()
156156
.unwrap();
157157
let def = VariableDeclaration::extract(m).unwrap();
158-
def.as_variable().unwrap()
158+
def.to_variable().unwrap()
159159
}
160160

161161
#[test]

0 commit comments

Comments
 (0)