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

Field decorator parsing rework #118

Merged
merged 5 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions core/src/language/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ pub enum SupportedLanguage {
TypeScript,
}

impl SupportedLanguage {
/// Returns an iterator over all supported language variants.
pub fn all_languages() -> impl Iterator<Item = Self> {
use SupportedLanguage::*;
[Go, Kotlin, Scala, Swift, TypeScript].into_iter()
}
}

impl FromStr for SupportedLanguage {
type Err = ParseError;

Expand Down
4 changes: 2 additions & 2 deletions core/src/language/typescript.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use joinery::JoinableIterator;

use crate::rust_types::{RustType, RustTypeFormatError, SpecialRustType};
use crate::rust_types::{FieldDecorator, RustType, RustTypeFormatError, SpecialRustType};
use crate::{
language::{Language, SupportedLanguage},
rust_types::{RustEnum, RustEnumVariant, RustField, RustStruct, RustTypeAlias},
Expand Down Expand Up @@ -239,7 +239,7 @@ impl TypeScript {
let is_readonly = field
.decorators
.get(&SupportedLanguage::TypeScript)
.filter(|v| v.contains(&"readonly".to_string()))
.filter(|v| v.contains(&FieldDecorator::Word("readonly".to_string())))
.is_some();
writeln!(
w,
Expand Down
32 changes: 18 additions & 14 deletions core/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::rust_types::FieldDecorator;
use crate::{
language::SupportedLanguage,
rename::RenameExt,
Expand All @@ -7,6 +8,7 @@ use crate::{
},
};
use proc_macro2::{Ident, Span};
use std::collections::BTreeSet;
use std::{
collections::{HashMap, HashSet},
convert::TryFrom,
Expand Down Expand Up @@ -560,17 +562,12 @@ fn get_field_type_override(attrs: &[syn::Attribute]) -> Option<String> {
}

/// Checks the struct or enum for decorators like `#[typeshare(typescript(readonly)]`
/// Takes a slice of `syn::Attribute`, returns a `HashMap<language, HashSet<decoration_words>>`, where `language` is `SupportedLanguage` and `decoration_words` is `String`
fn get_field_decorators(attrs: &[syn::Attribute]) -> HashMap<SupportedLanguage, HashSet<String>> {
let languages: HashSet<SupportedLanguage> = [
SupportedLanguage::Go,
SupportedLanguage::TypeScript,
SupportedLanguage::Kotlin,
SupportedLanguage::Swift,
]
.iter()
.cloned()
.collect();
/// Takes a slice of `syn::Attribute`, returns a `HashMap<language, BTreeSet<decorator>>`, where `language` is `SupportedLanguage`
/// and `decorator` is `FieldDecorator`. Field decorators are ordered in a `BTreeSet` for consistent code generation.
fn get_field_decorators(
attrs: &[Attribute],
) -> HashMap<SupportedLanguage, BTreeSet<FieldDecorator>> {
let languages: HashSet<SupportedLanguage> = SupportedLanguage::all_languages().collect();

attrs
.iter()
Expand All @@ -593,11 +590,18 @@ fn get_field_decorators(attrs: &[syn::Attribute]) -> HashMap<SupportedLanguage,
language,
list.iter()
.flat_map(|nested| match nested {
snowsignal marked this conversation as resolved.
Show resolved Hide resolved
NestedMeta::Meta(Meta::Path(path)) => path.get_ident(),
NestedMeta::Meta(Meta::Path(path)) => {
snowsignal marked this conversation as resolved.
Show resolved Hide resolved
Some(FieldDecorator::Word(path.get_ident()?.to_string()))
}
NestedMeta::Meta(Meta::NameValue(name_value)) => {
Some(FieldDecorator::NameValue(
name_value.path.get_ident()?.to_string(),
literal_as_string(name_value.lit.clone())?,
))
}
_ => None,
snowsignal marked this conversation as resolved.
Show resolved Hide resolved
})
.map(|ident| ident.to_string())
.collect::<HashSet<String>>(),
.collect::<BTreeSet<_>>(),
)
})
.fold(HashMap::new(), |mut acc, (language, decorators)| {
Expand Down
15 changes: 12 additions & 3 deletions core/src/rust_types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use quote::ToTokens;
use std::collections::HashSet;
use std::collections::BTreeSet;
use std::str::FromStr;
use std::{collections::HashMap, convert::TryFrom};
use syn::{Expr, ExprLit, Lit, TypeArray};
Expand Down Expand Up @@ -75,8 +75,17 @@ pub struct RustField {
/// for the languages we generate code for.
pub has_default: bool,
/// Language-specific decorators assigned to a given field.
/// The keys are language names (e.g. SupportedLanguage::TypeScript), the values are decorators (e.g. readonly)
pub decorators: HashMap<SupportedLanguage, HashSet<String>>,
/// The keys are language names (e.g. SupportedLanguage::TypeScript), the values are field decorators (e.g. readonly)
pub decorators: HashMap<SupportedLanguage, BTreeSet<FieldDecorator>>,
}

/// A single decorator on a field in Rust code.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum FieldDecorator {
/// A boolean flag enabled by its existence as a decorator: for example, `readonly`.
Word(String),
/// A key-value pair, such as `type = "any"`.
NameValue(String, String),
snowsignal marked this conversation as resolved.
Show resolved Hide resolved
}

/// A Rust type.
Expand Down