-
Notifications
You must be signed in to change notification settings - Fork 4
WIP: doc comments #2
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -34,7 +34,7 @@ pub fn impl_bson_schema_fields_extra( | |
| impl_bson_schema_named_fields(attrs, fields.named, extra) | ||
| }, | ||
| Fields::Unnamed(fields) => { | ||
| impl_bson_schema_indexed_fields(fields.unnamed, extra) | ||
| impl_bson_schema_indexed_fields(attrs, fields.unnamed, extra) | ||
| }, | ||
| Fields::Unit => { | ||
| assert!(extra.is_none(), "internally-tagged unit should've been handled"); | ||
|
|
@@ -51,10 +51,17 @@ fn impl_bson_schema_named_fields( | |
| ) -> Result<TokenStream> { | ||
| let properties = &field_names(attrs, &fields)?; | ||
| let defs: Vec<_> = fields.iter().map(field_def).collect::<Result<_>>()?; | ||
| let doc = doc_meta(&attrs).and_then(|doc| meta_value_as_str(&doc).ok()); | ||
| let doc = if doc.is_some() { | ||
| quote! { "description": #doc.trim_left(), } | ||
| } else { | ||
| quote! {} | ||
|
Owner
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 would be better served by Also, the trailing comma should be at the use site for better readability.
Author
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. hm, but if #doc is empty it would error out for extra comma.
Owner
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. oh, that is quite right. I didn't notice that. Nevermind, then. :)
Owner
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. However, that should also be true for the other similar places, then. For example, when |
||
| }; | ||
| let tokens = if let Some(TagExtra { tag, variant }) = extra { | ||
| quote! { | ||
| doc! { | ||
| "type": "object", | ||
| #doc | ||
|
Owner
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 mean, add a comma here…) |
||
| "additionalProperties": false, | ||
| "required": [ #tag, #(#properties,)* ], | ||
| "properties": { | ||
|
|
@@ -67,6 +74,7 @@ fn impl_bson_schema_named_fields( | |
| quote! { | ||
| doc! { | ||
| "type": "object", | ||
| #doc | ||
|
Owner
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. (…and here.) |
||
| "additionalProperties": false, | ||
| "required": [ #(#properties,)* ], | ||
| "properties": { | ||
|
|
@@ -90,15 +98,17 @@ fn field_def(field: &Field) -> Result<TokenStream> { | |
| let max_excl = magnet_meta_name_value(&field.attrs, "max_excl")?; | ||
| let lower = bounds_from_meta(min_incl, min_excl)?; | ||
| let upper = bounds_from_meta(max_incl, max_excl)?; | ||
| let doc = doc_meta(&field.attrs).and_then(|doc| meta_value_as_str(&doc).ok()).unwrap_or_else(String::new); | ||
|
Owner
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. Please propagate the error here as well, instead of using
Owner
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. Also, |
||
|
|
||
| Ok(quote! { | ||
| ::magnet_schema::support::extend_schema_with_bounds( | ||
| <#ty as ::magnet_schema::BsonSchema>::bson_schema(), | ||
| ::magnet_schema::support::Bounds { | ||
| lower: #lower, | ||
| upper: #upper, | ||
| }, | ||
| ) | ||
| ::magnet_schema::support::extend_schema_with_doc( | ||
|
Owner
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. Instead of wrapping each kind of extension into its own function, and increasing nesting, let's create a single extension context object and an |
||
| ::magnet_schema::support::extend_schema_with_bounds( | ||
| <#ty as ::magnet_schema::BsonSchema>::bson_schema(), | ||
| ::magnet_schema::support::Bounds { | ||
| lower: #lower, | ||
| upper: #upper, | ||
| }, | ||
| ), #doc) | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -163,6 +173,7 @@ fn field_names(attrs: &[Attribute], fields: &Punctuated<Field, Comma>) -> Result | |
| /// Implements `BsonSchema` for a tuple `struct` or variant, | ||
| /// with unnamed (numbered/indexed) fields. | ||
| fn impl_bson_schema_indexed_fields( | ||
| attrs: &[Attribute], | ||
| mut fields: Punctuated<Field, Comma>, | ||
| extra: Option<TagExtra>, | ||
| ) -> Result<TokenStream> { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,21 @@ use std::f64; | |
| use syn::{ Attribute, Meta, NestedMeta, MetaNameValue, Lit }; | ||
| use error::{ Error, Result }; | ||
|
|
||
| pub fn doc_meta(attrs: &[Attribute]) -> Option<MetaNameValue> { | ||
|
Owner
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. Please add doc comments to all items (especially public ones). Running |
||
| let mut value = attrs.iter().filter_map(|attr| { | ||
| if let Some(meta) = attr.interpret_meta() { | ||
|
Owner
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. This can be simplified to: attr.interpret_meta().and_then(|meta| match meta {
Meta::NameValue(ref val) if val.ident == "doc" => Some(val.clone()),
_ => None,
}) |
||
| match meta { | ||
| Meta::NameValue(ref val) if val.ident == "doc" => { | ||
| return Some(val.clone()) | ||
| } | ||
| _ => {}, | ||
| }; | ||
| } | ||
| None | ||
| }); | ||
| value.next() | ||
| } | ||
|
|
||
| /// Returns the inner, `...` part of the first `#[name(...)]` attribute | ||
| /// with the specified name (like `#[magnet(key ( = "value")?)]`). | ||
| /// TODO(H2CO3): check for duplicate arguments and bail out with an error | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,14 @@ pub fn extend_schema_with_bounds(mut schema: Document, bounds: Bounds) -> Docume | |
| schema | ||
| } | ||
|
|
||
| #[doc(hidden)] | ||
|
Owner
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. (Therefore, this function will not be necessary, its body will be incorporated into |
||
| pub fn extend_schema_with_doc(mut schema: Document, doc: &str) -> Document { | ||
| if !doc.is_empty() { | ||
| schema.insert("description", doc.trim_left()); | ||
| } | ||
| schema | ||
| } | ||
|
|
||
|
Owner
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 here emptiness should be tested after trimming the doc-string. |
||
| /// This function should not be used directly; calls to it are only generated by | ||
| /// `magnet_derive` when emitting code for internally-tagged newtype variants. | ||
| /// | ||
|
|
||
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.
Please do not use
Result::ok()here – attribute parsing errors are meant to be propagated upward.