Skip to content

Commit 978dc59

Browse files
committed
Add variant docs and non_exhaustive option
1 parent 899501d commit 978dc59

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# unreleased ()
2+
3+
### Added
4+
- Documentation comments for enum variants
5+
- Marking an enum `non_exhaustive`
6+
17
# 0.1.3 (May 9, 2020)
28

39
### Added

src/enum.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ impl Enum {
8686
self
8787
}
8888

89+
/// Mark this enum with `#[non_exhaustive]`.
90+
pub fn non_exhaustive(&mut self) -> &mut Self {
91+
self.type_def.r#macro("#[non_exhaustive]");
92+
self
93+
}
94+
8995
/// Formats the enum using the given formatter.
9096
pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
9197
self.type_def.fmt_head("enum", &[], fmt)?;

src/variant.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ use fields::Fields;
44
use formatter::Formatter;
55

66
use r#type::Type;
7+
use docs::Docs;
78

89

910
/// Defines an enum variant.
1011
#[derive(Debug, Clone)]
1112
pub struct Variant {
1213
name: String,
1314
fields: Fields,
15+
16+
/// Variant documentation
17+
docs: Option<Docs>,
1418
}
1519

1620

@@ -20,6 +24,7 @@ impl Variant {
2024
Variant {
2125
name: name.to_string(),
2226
fields: Fields::Empty,
27+
docs: None,
2328
}
2429
}
2530

@@ -38,8 +43,17 @@ impl Variant {
3843
self
3944
}
4045

46+
/// Set the variant documentation.
47+
pub fn doc(&mut self, docs: &str) -> &mut Self {
48+
self.docs = Some(Docs::new(docs));
49+
self
50+
}
51+
4152
/// Formats the variant using the given formatter.
4253
pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
54+
if let Some(ref docs) = self.docs {
55+
docs.fmt(fmt)?;
56+
}
4357
write!(fmt, "{}", self.name)?;
4458
self.fields.fmt(fmt)?;
4559
write!(fmt, ",\n")?;

tests/codegen.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,47 @@ enum IpAddrKind {
405405
assert_eq!(scope.to_string(), &expect[1..]);
406406
}
407407

408+
#[test]
409+
fn enum_with_non_exhaustive() {
410+
let mut scope = Scope::new();
411+
412+
scope.new_enum("IpAddrKind")
413+
.non_exhaustive()
414+
.push_variant(Variant::new("V4"))
415+
.push_variant(Variant::new("V6"))
416+
;
417+
418+
let expect = r#"
419+
#[non_exhaustive]
420+
enum IpAddrKind {
421+
V4,
422+
V6,
423+
}"#;
424+
425+
assert_eq!(scope.to_string(), &expect[1..]);
426+
}
427+
428+
#[test]
429+
fn enum_with_variant_doc() {
430+
let mut scope = Scope::new();
431+
432+
let mut v = Variant::new("V4");
433+
v.doc("best");
434+
scope.new_enum("IpAddrKind")
435+
.push_variant(v)
436+
.push_variant(Variant::new("V6"))
437+
;
438+
439+
let expect = r#"
440+
enum IpAddrKind {
441+
/// best
442+
V4,
443+
V6,
444+
}"#;
445+
446+
assert_eq!(scope.to_string(), &expect[1..]);
447+
}
448+
408449
#[test]
409450
fn scoped_imports() {
410451
let mut scope = Scope::new();

0 commit comments

Comments
 (0)