Skip to content

Add variant docs and non_exhaustive option #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# unreleased ()

### Added
- Documentation comments for enum variants
- Marking an enum `non_exhaustive`

# 0.1.3 (May 9, 2020)

### Added
Expand Down
6 changes: 6 additions & 0 deletions src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ impl Enum {
self
}

/// Mark this enum with `#[non_exhaustive]`.
pub fn non_exhaustive(&mut self) -> &mut Self {
self.type_def.r#macro("#[non_exhaustive]");
self
}

/// Formats the enum using the given formatter.
pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
self.type_def.fmt_head("enum", &[], fmt)?;
Expand Down
14 changes: 14 additions & 0 deletions src/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ use fields::Fields;
use formatter::Formatter;

use r#type::Type;
use docs::Docs;


/// Defines an enum variant.
#[derive(Debug, Clone)]
pub struct Variant {
name: String,
fields: Fields,

/// Variant documentation
docs: Option<Docs>,
}


Expand All @@ -20,6 +24,7 @@ impl Variant {
Variant {
name: name.to_string(),
fields: Fields::Empty,
docs: None,
}
}

Expand All @@ -38,8 +43,17 @@ impl Variant {
self
}

/// Set the variant documentation.
pub fn doc(&mut self, docs: &str) -> &mut Self {
self.docs = Some(Docs::new(docs));
self
}

/// Formats the variant using the given formatter.
pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
if let Some(ref docs) = self.docs {
docs.fmt(fmt)?;
}
write!(fmt, "{}", self.name)?;
self.fields.fmt(fmt)?;
write!(fmt, ",\n")?;
Expand Down
41 changes: 41 additions & 0 deletions tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,47 @@ enum IpAddrKind {
assert_eq!(scope.to_string(), &expect[1..]);
}

#[test]
fn enum_with_non_exhaustive() {
let mut scope = Scope::new();

scope.new_enum("IpAddrKind")
.non_exhaustive()
.push_variant(Variant::new("V4"))
.push_variant(Variant::new("V6"))
;

let expect = r#"
#[non_exhaustive]
enum IpAddrKind {
V4,
V6,
}"#;

assert_eq!(scope.to_string(), &expect[1..]);
}

#[test]
fn enum_with_variant_doc() {
let mut scope = Scope::new();

let mut v = Variant::new("V4");
v.doc("best");
scope.new_enum("IpAddrKind")
.push_variant(v)
.push_variant(Variant::new("V6"))
;

let expect = r#"
enum IpAddrKind {
/// best
V4,
V6,
}"#;

assert_eq!(scope.to_string(), &expect[1..]);
}

#[test]
fn scoped_imports() {
let mut scope = Scope::new();
Expand Down