-
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
Changes from all commits
411c8e6
7d187d1
4084261
6093af4
911dbc8
3981c19
3e44fb6
2d8d4b0
5aafce7
3d032ea
95b6673
6e17dc2
2528d99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: Vec<String>, | ||
|
||
/// Field annotation | ||
annotation: Vec<String>, | ||
} | ||
|
||
/// 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: Vec::new(), | ||
annotation: Vec::new(), | ||
} | ||
} | ||
|
||
/// Set field's documentation. | ||
pub fn doc(&mut self, documentation: Vec<&str>) -> &mut Self { | ||
self.documentation = documentation.iter().map(|doc| doc.to_string()).collect(); | ||
self | ||
} | ||
|
||
/// Set field's annotation. | ||
pub fn annotation(&mut self, annotation: Vec<&str>) -> &mut Self { | ||
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. Maybe 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'm not sure I follow. Do you mean using a It was changed to use a 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 meant a method to add one annotation line per call, like you do for import(...) - each call results in a new import - instead of passing the whole Vec with all lines :) |
||
self.annotation = annotation.iter().map(|ann| ann.to_string()).collect(); | ||
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: Vec::new(), | ||
annotation: Vec::new(), | ||
}) | ||
} | ||
|
||
fn tuple<T>(&mut self, ty: T) -> &mut Self | ||
where T: Into<Type>, | ||
{ | ||
|
@@ -1121,6 +1169,16 @@ impl Fields { | |
|
||
fmt.block(|fmt| { | ||
for f in fields { | ||
if !f.documentation.is_empty() { | ||
for doc in &f.documentation { | ||
write!(fmt, "/// {}\n", doc)?; | ||
} | ||
} | ||
if !f.annotation.is_empty() { | ||
for ann in &f.annotation { | ||
write!(fmt, "{}\n", ann)?; | ||
} | ||
} | ||
write!(fmt, "{}: ", f.name)?; | ||
f.ty.fmt(fmt)?; | ||
write!(fmt, ",\n")?; | ||
|
@@ -1196,6 +1254,8 @@ impl Impl { | |
self.assoc_tys.push(Field { | ||
name: name.to_string(), | ||
ty: ty.into(), | ||
documentation: Vec::new(), | ||
annotation: Vec::new(), | ||
}); | ||
|
||
self | ||
|
@@ -1340,6 +1400,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: Vec::new(), | ||
annotation: Vec::new(), | ||
}); | ||
|
||
self | ||
|
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.
Maybe
doc(&mut self, documentation: &str)
likeimport(&mut self, path: &str, ty: &str)
?