Skip to content
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

Added support for visibility for fields #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub struct Field {

/// Field annotation
pub annotation: Vec<String>,

/// The visibility of the field
pub visibility: Option<String>,
}

impl Field {
Expand All @@ -27,6 +30,7 @@ impl Field {
ty: ty.into(),
documentation: Vec::new(),
annotation: Vec::new(),
visibility: None,
}
}

Expand All @@ -41,4 +45,10 @@ impl Field {
self.annotation = annotation.iter().map(|ann| ann.to_string()).collect();
self
}

/// Set the visibility of the field
pub fn vis(&mut self, visibility: &str) -> &mut Self {
self.visibility = Some(visibility.to_string());
self
}
}
17 changes: 17 additions & 0 deletions src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,23 @@ impl Fields {
ty: ty.into(),
documentation: Vec::new(),
annotation: Vec::new(),
visibility: None,
})
}

pub fn new_named<T>(&mut self, name: &str, ty: T) -> &mut Field
where
T: Into<Type>,
{
self.named(name, ty);
if let Fields::Named(ref mut fields) = *self {
fields.last_mut().unwrap()
}
else {
unreachable!()
}
}

pub fn tuple<T>(&mut self, ty: T) -> &mut Self
where
T: Into<Type>,
Expand Down Expand Up @@ -74,6 +88,9 @@ impl Fields {
write!(fmt, "{}\n", ann)?;
}
}
if let Some(visibility) = &f.visibility {
write!(fmt, "{} ", visibility)?;
}
write!(fmt, "{}: ", f.name)?;
f.ty.fmt(fmt)?;
write!(fmt, ",\n")?;
Expand Down
3 changes: 2 additions & 1 deletion src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ impl Function {
self.args.push(Field {
name: name.to_string(),
ty: ty.into(),
// While a `Field` is used here, both `documentation`
// While a `Field` is used here, both `documentation`, `visibility`
// and `annotation` does not make sense for function arguments.
// Simply use empty strings.
documentation: Vec::new(),
annotation: Vec::new(),
visibility: None,
});

self
Expand Down
1 change: 1 addition & 0 deletions src/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl Impl {
ty: ty.into(),
documentation: Vec::new(),
annotation: Vec::new(),
visibility: None,
});

self
Expand Down
11 changes: 11 additions & 0 deletions src/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ impl Struct {
self
}

/// Create a named field for the struct.
///
/// A struct can either set named fields with this function or tuple fields
/// with `tuple_field`, but not both.
pub fn new_field<T>(&mut self, name: &str, ty: T) -> &mut Field
where
T: Into<Type>,
{
self.fields.new_named(name, ty)
}

/// Add a tuple field to the struct.
///
/// A struct can either set tuple fields with this function or named fields
Expand Down
21 changes: 21 additions & 0 deletions tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,5 +603,26 @@ enum IpAddrKind {
V6,
}"#;

assert_eq!(scope.to_string(), &expect[1..]);
}

#[test]
fn struct_with_member_visibility() {
let mut scope = Scope::new();

let struct_description = scope.new_struct("Foo");

let mut bar = Field::new("bar", "usize");
bar.vis("pub");

struct_description.push_field(bar);
struct_description.new_field("baz", "i16").vis("pub(crate)");

let expect = r#"
struct Foo {
pub bar: usize,
pub(crate) baz: i16,
}"#;

assert_eq!(scope.to_string(), &expect[1..]);
}