-
Notifications
You must be signed in to change notification settings - Fork 65
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
Issue #3 and #4: Add field documentation and annotation #6
Merged
andrewleverette
merged 13 commits into
carllerche:master
from
nbigaouette:issue-3-and-4-field-documentation-annotation
May 9, 2020
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
411c8e6
Let `Field` be public
nbigaouette 7d187d1
Add `Struct::push_field()` to push a field to a struct with named fields
nbigaouette 4084261
Add `Field::new()` impl
nbigaouette 6093af4
Split `Fields::named()` into `named()` and `push_named()`
nbigaouette 911dbc8
Add test adding `Field` to `Struct`
nbigaouette 3981c19
Add a `documentation` field to `Field` and `with_documentation()` fn
nbigaouette 3e44fb6
Add a `annotation` field to `Field` and `with_annotation()` fn
nbigaouette 2d8d4b0
Review: Rename `with_documentation()` to `doc()`
nbigaouette 5aafce7
Review: Rename `with_annotation()` to `annotation()`
nbigaouette 3d032ea
Review: Fix tests
nbigaouette 95b6673
Review: Delete useless semicolon
nbigaouette 6e17dc2
Let `Field.annotation` be a `Vec<String>` instead of `String` for mul…
nbigaouette 2528d99
Let `Field.documentation` be a `Vec<String>` instead of `String` for …
nbigaouette File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,12 +128,18 @@ enum Fields { | |
|
||
/// Defines a struct field. | ||
#[derive(Debug, Clone)] | ||
struct Field { | ||
pub struct Field { | ||
/// Field name | ||
name: String, | ||
|
||
/// Field type | ||
ty: Type, | ||
|
||
/// Field documentation | ||
documentation: String, | ||
|
||
/// Field annotation | ||
annotation: String, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this probably should be a |
||
} | ||
|
||
/// Defines an associated type. | ||
|
@@ -582,6 +588,16 @@ impl Struct { | |
self | ||
} | ||
|
||
/// Push a named field to the struct. | ||
/// | ||
/// A struct can either set named fields with this function or tuple fields | ||
/// with `push_tuple_field`, but not both. | ||
pub fn push_field(&mut self, field: Field) -> &mut Self | ||
{ | ||
self.fields.push_named(field); | ||
self | ||
} | ||
|
||
/// Add a named field to the struct. | ||
/// | ||
/// A struct can either set named fields with this function or tuple fields | ||
|
@@ -1073,31 +1089,63 @@ impl AssociatedType { | |
} | ||
} | ||
|
||
// ===== impl Field ===== | ||
|
||
impl Field { | ||
/// Return a field definition with the provided name and type | ||
pub fn new<T>(name: &str, ty: T) -> Self | ||
where T: Into<Type>, | ||
{ | ||
Field { | ||
name: name.into(), | ||
ty: ty.into(), | ||
documentation: String::new(), | ||
annotation: String::new(), | ||
} | ||
} | ||
|
||
/// Set field's documentation. | ||
pub fn doc(&mut self, documentation: &str) -> &mut Self { | ||
self.documentation = documentation.into(); | ||
self | ||
} | ||
|
||
/// Set field's annotation. | ||
pub fn annotation(&mut self, annotation: &str) -> &mut Self { | ||
self.annotation = annotation.into(); | ||
self | ||
} | ||
} | ||
|
||
// ===== impl Fields ===== | ||
|
||
impl Fields { | ||
fn named<T>(&mut self, name: &str, ty: T) -> &mut Self | ||
where T: Into<Type>, | ||
fn push_named(&mut self, field: Field) -> &mut Self | ||
{ | ||
match *self { | ||
Fields::Empty => { | ||
*self = Fields::Named(vec![Field { | ||
name: name.to_string(), | ||
ty: ty.into(), | ||
}]); | ||
*self = Fields::Named(vec![field]); | ||
} | ||
Fields::Named(ref mut fields) => { | ||
fields.push(Field { | ||
name: name.to_string(), | ||
ty: ty.into(), | ||
}); | ||
fields.push(field); | ||
} | ||
_ => panic!("field list is named"), | ||
} | ||
|
||
self | ||
} | ||
|
||
fn named<T>(&mut self, name: &str, ty: T) -> &mut Self | ||
where T: Into<Type>, | ||
{ | ||
self.push_named(Field { | ||
name: name.to_string(), | ||
ty: ty.into(), | ||
documentation: String::new(), | ||
annotation: String::new(), | ||
}) | ||
} | ||
|
||
fn tuple<T>(&mut self, ty: T) -> &mut Self | ||
where T: Into<Type>, | ||
{ | ||
|
@@ -1121,6 +1169,12 @@ impl Fields { | |
|
||
fmt.block(|fmt| { | ||
for f in fields { | ||
if !f.documentation.is_empty() { | ||
write!(fmt, "/// {}\n", f.documentation)?; | ||
} | ||
if !f.annotation.is_empty() { | ||
write!(fmt, "{}\n", f.annotation)?; | ||
} | ||
write!(fmt, "{}: ", f.name)?; | ||
f.ty.fmt(fmt)?; | ||
write!(fmt, ",\n")?; | ||
|
@@ -1196,6 +1250,8 @@ impl Impl { | |
self.assoc_tys.push(Field { | ||
name: name.to_string(), | ||
ty: ty.into(), | ||
documentation: String::new(), | ||
annotation: String::new(), | ||
}); | ||
|
||
self | ||
|
@@ -1340,6 +1396,11 @@ impl Function { | |
self.args.push(Field { | ||
name: name.to_string(), | ||
ty: ty.into(), | ||
// While a `Field` is used here, both `documentation` | ||
// and `annotation` does not make sense for function arguments. | ||
// Simply use empty strings. | ||
documentation: String::new(), | ||
annotation: String::new(), | ||
}); | ||
|
||
self | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation strings are tricky. Reading the diff, it looks like only a single doc string line is supported. I'm not sure what the best option would be to support multi lines. Either "\n" could be detected in the documentation string and then it all gets formatted, or
documentation
could be aVec<String>
.Thoughts"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about different functions? Have
doc()
take aVec<String>
to stay the same asannotation()
and add something likedoc_with_newlines()
which takes aString
.