Skip to content

Commit

Permalink
can parse labels
Browse files Browse the repository at this point in the history
  • Loading branch information
jifalops committed Mar 24, 2024
1 parent 87cd0e3 commit fde0f62
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion example/src/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod tests {
#[test]
fn person() {
assert_eq!(Person::typename(), "Person2");
assert_eq!(Person::labels(), &["Person2, PersonExtraLabel"]);
assert_eq!(Person::labels(), &["Person2", "PersonExtraLabel"]);
assert_eq!(
Person::field_names(),
[
Expand Down
5 changes: 3 additions & 2 deletions lib/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ pub trait FieldSet: TryFrom<Row> {
/// Formatted like `typename() { as_query_fields() }`, or for a fieldless relationship, just `typename()`.
fn to_query_obj(prefix: Option<&str>, mode: StampMode) -> String {
let fields = Self::to_query_fields(prefix, mode);
let labels = Self::labels().join(":");
if fields.is_empty() {
return Self::labels().join(":").to_owned();
return labels;
}
format!("{} {{ {} }}", Self::typename(), fields)
format!("{} {{ {} }}", labels, fields)
}
}

Expand Down
40 changes: 24 additions & 16 deletions macros/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ pub use node::Node;
pub use relation::Relation;

use quote::{__private::TokenStream, quote, ToTokens};
use syn::{Attribute, LitStr, Meta};
use syn::{
parse::{Parse, ParseStream},
Attribute, LitStr, Meta, Result, Token,
};

#[cfg(feature = "serde")]
pub fn derive_serde() -> TokenStream {
Expand Down Expand Up @@ -67,22 +70,10 @@ pub fn parse_labels_meta(meta: &Meta) -> Option<Vec<String>> {
// Parse #[labels("Foo", "Bar")].
Meta::List(list) => {
if list.path.is_ident("labels") {
let mut labels = Vec::new();
if let Err(e) = list.parse_nested_meta(|meta| {
let label = syn::parse2::<LitStr>(meta.path.into_token_stream())
.map(|lit| lit.value())
.unwrap();

labels.push(label);
// match meta.path.get_ident() {
// Some(ident) => labels.push(ident.to_string()),
// None => panic!("Expected a label."),
// };
Ok(())
}) {
panic!("Expected a list of labels: {}", e);
match list.parse_args::<Labels>() {
Ok(labels) => Some(labels.0),
Err(e) => panic!("Expected a list of labels: {}", e),
}
Some(labels)
} else {
None
}
Expand All @@ -91,6 +82,23 @@ pub fn parse_labels_meta(meta: &Meta) -> Option<Vec<String>> {
}
}

struct Labels(Vec<String>);

impl Parse for Labels {
fn parse(input: ParseStream) -> syn::Result<Labels> {
let mut values = Vec::new();
while !input.is_empty() {
let lit_str = input.parse::<LitStr>()?;
values.push(lit_str.value());
if input.is_empty() {
break;
}
input.parse::<Token![,]>()?;
}
Ok(Labels(values))
}
}

#[cfg(test)]
mod tests {
use syn::parse_quote;
Expand Down

0 comments on commit fde0f62

Please sign in to comment.