Skip to content

Commit 1f22aba

Browse files
committed
expand docs to argument and field as well
1 parent b4c96c2 commit 1f22aba

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

codama-attributes/src/codama_directives/argument_directive.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
Attribute, CodamaAttribute, CodamaDirective,
55
};
66
use codama_errors::CodamaError;
7-
use codama_nodes::{Docs, InstructionArgumentNode};
7+
use codama_nodes::InstructionArgumentNode;
88
use codama_syn_helpers::Meta;
99

1010
#[derive(Debug, PartialEq)]
@@ -19,6 +19,7 @@ impl ArgumentDirective {
1919
let consumer = StructFieldMetaConsumer::from_meta(meta)?
2020
.consume_field()?
2121
.consume_argument_default_value()?
22+
.consume_docs()?
2223
.consume_after()?
2324
.assert_fully_consumed()?;
2425

@@ -30,7 +31,7 @@ impl ArgumentDirective {
3031
argument: InstructionArgumentNode {
3132
name: consumer.name.take(meta)?,
3233
r#type: consumer.r#type.take(meta)?,
33-
docs: Docs::default(),
34+
docs: consumer.docs.option().unwrap_or_default(),
3435
default_value,
3536
default_value_strategy,
3637
},
@@ -106,4 +107,21 @@ mod tests {
106107
}
107108
);
108109
}
110+
111+
#[test]
112+
fn with_docs_string() {
113+
let meta: Meta = syn::parse_quote! { argument("cake", number(u8), docs = "The cake") };
114+
let directive = ArgumentDirective::parse(&meta).unwrap();
115+
assert_eq!(directive.argument.docs, vec!["The cake".to_string()].into());
116+
}
117+
118+
#[test]
119+
fn with_docs_array() {
120+
let meta: Meta = syn::parse_quote! { argument("cake", number(u8), docs = ["The cake", "must be a lie"]) };
121+
let directive = ArgumentDirective::parse(&meta).unwrap();
122+
assert_eq!(
123+
directive.argument.docs,
124+
vec!["The cake".to_string(), "must be a lie".to_string()].into()
125+
);
126+
}
109127
}

codama-attributes/src/codama_directives/field_directive.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
Attribute, CodamaAttribute, CodamaDirective,
55
};
66
use codama_errors::CodamaError;
7-
use codama_nodes::{Docs, StructFieldTypeNode};
7+
use codama_nodes::StructFieldTypeNode;
88
use codama_syn_helpers::Meta;
99

1010
#[derive(Debug, PartialEq)]
@@ -19,6 +19,7 @@ impl FieldDirective {
1919
let consumer = StructFieldMetaConsumer::from_meta(meta)?
2020
.consume_field()?
2121
.consume_default_value()?
22+
.consume_docs()?
2223
.consume_after()?
2324
.assert_fully_consumed()?;
2425

@@ -30,7 +31,7 @@ impl FieldDirective {
3031
field: StructFieldTypeNode {
3132
name: consumer.name.take(meta)?,
3233
r#type: consumer.r#type.take(meta)?,
33-
docs: Docs::default(),
34+
docs: consumer.docs.option().unwrap_or_default(),
3435
default_value,
3536
default_value_strategy,
3637
},
@@ -106,4 +107,21 @@ mod tests {
106107
}
107108
);
108109
}
110+
111+
#[test]
112+
fn with_docs_string() {
113+
let meta: Meta = syn::parse_quote! { field("splines", number(u8), docs = "Splines") };
114+
let directive = FieldDirective::parse(&meta).unwrap();
115+
assert_eq!(directive.field.docs, vec!["Splines".to_string()].into());
116+
}
117+
118+
#[test]
119+
fn with_docs_array() {
120+
let meta: Meta = syn::parse_quote! { field("age", number(u8), docs = ["Splines", "Must be pre-reticulated"]) };
121+
let directive = FieldDirective::parse(&meta).unwrap();
122+
assert_eq!(
123+
directive.field.docs,
124+
vec!["Splines".to_string(), "Must be pre-reticulated".to_string()].into()
125+
);
126+
}
109127
}

codama-attributes/src/codama_directives/type_nodes/struct_field_meta_consumer.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
DefaultValueDirective,
44
};
55
use codama_nodes::{
6-
CamelCaseString, DefaultValueStrategy, InstructionInputValueNode, TypeNode, ValueNode,
6+
CamelCaseString, DefaultValueStrategy, Docs, InstructionInputValueNode, TypeNode, ValueNode,
77
};
88
use codama_syn_helpers::{extensions::*, Meta};
99

@@ -13,6 +13,7 @@ pub(crate) struct StructFieldMetaConsumer {
1313
pub r#type: SetOnce<TypeNode>,
1414
pub default_value: SetOnce<DefaultValueDirective>,
1515
pub after: SetOnce<bool>,
16+
pub docs: SetOnce<Docs>,
1617
}
1718

1819
impl MetaConsumer for StructFieldMetaConsumer {
@@ -23,6 +24,7 @@ impl MetaConsumer for StructFieldMetaConsumer {
2324
r#type: SetOnce::new("type"),
2425
default_value: SetOnce::new("default_value"),
2526
after: SetOnce::new("after"),
27+
docs: SetOnce::new("docs"),
2628
}
2729
}
2830

@@ -94,6 +96,16 @@ impl StructFieldMetaConsumer {
9496
})
9597
}
9698

99+
pub fn consume_docs(self) -> syn::Result<Self> {
100+
self.consume_metas(|this, meta| match meta.path_str().as_str() {
101+
"docs" => {
102+
this.docs.set(Docs::from_meta(&meta)?, meta)?;
103+
Ok(None)
104+
}
105+
_ => Ok(Some(meta)),
106+
})
107+
}
108+
97109
pub fn default_value_strategy(&self) -> Option<DefaultValueStrategy> {
98110
self.default_value
99111
.option_ref()

0 commit comments

Comments
 (0)