1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
use crate::r#type::Type;
/// Defines a struct field.
#[derive(Debug, Clone)]
pub struct Field {
/// Field name
pub name: String,
/// Field type
pub ty: Type,
/// Field documentation
pub documentation: Vec<String>,
/// Field annotation
pub annotation: Vec<String>,
}
impl Field {
/// Return a field definition with the provided name and type.
///
/// # Arguments
///
/// * `name` - The name of the field.
/// * `ty` - The type of the field.
///
/// # Examples
///
/// ```
/// use rust_codegen::Field;
///
/// let count_field = Field::new("count", "i32");
/// ```
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 the field's documentation.
///
/// # Arguments
///
/// * `documentation` - The documentation to set for the field.
///
/// # Examples
///
/// ```
/// use rust_codegen::Field;
///
/// let count_field = Field::new("count", "i32").doc(Vec::from(["The number of Foos"]));
pub fn doc(&mut self, documentation: Vec<&str>) -> &mut Self {
self.documentation = documentation.iter().map(|doc| doc.to_string()).collect();
self
}
/// Set the field's annotation.
///
/// # Arguments
///
/// * `annotation` - The annotation to set for the field.
///
/// # Examples
///
/// ```
/// use rust_codegen::Field;
///
/// let count_field = Field::new("count", "i32").annotation(Vec::from(["serde(rename = \"name\")"]));
pub fn annotation(&mut self, annotation: Vec<&str>) -> &mut Self {
self.annotation = annotation.iter().map(|ann| ann.to_string()).collect();
self
}
}