Skip to content

Commit

Permalink
editoast: add more restrictive visibility to ModelV2 internals
Browse files Browse the repository at this point in the history
  • Loading branch information
leovalais committed Apr 18, 2024
1 parent ad7eb82 commit 2c46869
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 57 deletions.
54 changes: 27 additions & 27 deletions editoast/editoast_derive/src/modelv2/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,64 @@ use proc_macro2::Span;
forward_attrs(allow, doc, cfg),
supports(struct_named)
)]
pub struct ModelArgs {
pub table: syn::Path,
pub(super) struct ModelArgs {
pub(super) table: syn::Path,
#[darling(default)]
pub row: GeneratedTypeArgs,
pub(super) row: GeneratedTypeArgs,
#[darling(default)]
pub changeset: GeneratedTypeArgs,
pub(super) changeset: GeneratedTypeArgs,
#[darling(multiple, rename = "identifier")]
pub identifiers: Vec<RawIdentifier>,
pub(super) identifiers: Vec<RawIdentifier>,
#[darling(default)]
pub preferred: Option<RawIdentifier>,
pub data: ast::Data<util::Ignored, ModelFieldArgs>,
pub(super) preferred: Option<RawIdentifier>,
pub(super) data: ast::Data<util::Ignored, ModelFieldArgs>,
}

#[derive(FromMeta, Default, Debug, PartialEq)]
pub struct GeneratedTypeArgs {
pub(super) struct GeneratedTypeArgs {
#[darling(default)]
pub type_name: Option<String>,
pub(super) type_name: Option<String>,
#[darling(default)]
pub derive: PathList,
pub(super) derive: PathList,
#[darling(default)]
pub public: bool,
pub(super) public: bool,
}

#[derive(FromField, Debug)]
#[darling(attributes(model), forward_attrs(allow, doc, cfg))]
pub struct ModelFieldArgs {
pub ident: Option<syn::Ident>,
pub ty: syn::Type,
pub(super) struct ModelFieldArgs {
pub(super) ident: Option<syn::Ident>,
pub(super) ty: syn::Type,
#[darling(default)]
pub builder_fn: Option<syn::Ident>,
pub(super) builder_fn: Option<syn::Ident>,
#[darling(default)]
pub column: Option<String>,
pub(super) column: Option<String>,
#[darling(default)]
pub builder_skip: bool,
pub(super) builder_skip: bool,
#[darling(default)]
pub identifier: bool,
pub(super) identifier: bool,
#[darling(default)]
pub preferred: bool,
pub(super) preferred: bool,
#[darling(default)]
pub primary: bool,
pub(super) primary: bool,
#[darling(default)]
pub json: bool,
pub(super) json: bool,
#[darling(default)]
pub geo: bool,
pub(super) geo: bool,
#[darling(default)]
pub to_string: bool,
pub(super) to_string: bool,
#[darling(default)]
pub to_enum: bool,
pub(super) to_enum: bool,
#[darling(default)]
pub remote: Option<syn::Type>,
pub(super) remote: Option<syn::Type>,
}

impl GeneratedTypeArgs {
pub fn ident(&self) -> syn::Ident {
pub(super) fn ident(&self) -> syn::Ident {
syn::Ident::new(self.type_name.as_ref().unwrap(), Span::call_site())
}

pub fn visibility(&self) -> syn::Visibility {
pub(super) fn visibility(&self) -> syn::Visibility {
if self.public {
syn::Visibility::Public(Default::default())
} else {
Expand Down
54 changes: 27 additions & 27 deletions editoast/editoast_derive/src/modelv2/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,33 @@ use super::{
};

#[derive(Debug, PartialEq)]
pub struct ModelConfig {
pub model: syn::Ident,
pub visibility: syn::Visibility,
pub table: syn::Path,
pub fields: Fields,
pub row: GeneratedTypeArgs,
pub changeset: GeneratedTypeArgs,
pub(crate) struct ModelConfig {
pub(crate) model: syn::Ident,
pub(crate) visibility: syn::Visibility,
pub(crate) table: syn::Path,
pub(crate) fields: Fields,
pub(crate) row: GeneratedTypeArgs,
pub(crate) changeset: GeneratedTypeArgs,
pub(crate) identifiers: HashSet<Identifier>, // identifiers ⊆ fields
pub(crate) preferred_identifier: Identifier, // preferred_identifier ∈ identifiers
pub(crate) primary_identifier: Identifier, // primary_identifier ∈ identifiers
}

#[derive(Debug, PartialEq, Clone)]
pub struct ModelField {
pub ident: syn::Ident,
pub column: String,
pub builder_ident: syn::Ident,
pub ty: syn::Type,
pub builder_skip: bool,
pub identifier: bool,
pub preferred: bool,
pub primary: bool,
pub transform: Option<FieldTransformation>,
pub(crate) struct ModelField {
pub(crate) ident: syn::Ident,
pub(crate) column: String,
pub(crate) builder_ident: syn::Ident,
pub(crate) ty: syn::Type,
pub(crate) builder_skip: bool,
pub(crate) identifier: bool,
pub(crate) preferred: bool,
pub(crate) primary: bool,
pub(crate) transform: Option<FieldTransformation>,
}

#[derive(Debug, PartialEq, Clone)]
pub enum FieldTransformation {
pub(crate) enum FieldTransformation {
Remote(syn::Type),
Json,
Geo,
Expand All @@ -46,7 +46,7 @@ pub enum FieldTransformation {
}

#[derive(Debug, PartialEq)]
pub struct Fields(pub Vec<ModelField>);
pub(crate) struct Fields(pub(crate) Vec<ModelField>);

impl Deref for Fields {
type Target = Vec<ModelField>;
Expand All @@ -63,21 +63,21 @@ impl DerefMut for Fields {
}

impl Fields {
pub fn get(&self, ident: &syn::Ident) -> Option<&ModelField> {
pub(crate) fn get(&self, ident: &syn::Ident) -> Option<&ModelField> {
self.iter().find(|field| &field.ident == ident)
}
}

impl ModelConfig {
pub fn iter_fields(&self) -> impl Iterator<Item = &ModelField> {
pub(crate) fn iter_fields(&self) -> impl Iterator<Item = &ModelField> {
self.fields.iter()
}

pub fn is_primary(&self, field: &ModelField) -> bool {
pub(crate) fn is_primary(&self, field: &ModelField) -> bool {
field.ident == self.get_primary_field_ident()
}

pub fn table_name(&self) -> syn::Ident {
pub(crate) fn table_name(&self) -> syn::Ident {
let table = self
.table
.segments
Expand Down Expand Up @@ -113,7 +113,7 @@ impl ModelConfig {

impl ModelField {
#[allow(clippy::wrong_self_convention)]
pub fn into_transformed(&self, expr: syn::Expr) -> syn::Expr {
pub(crate) fn into_transformed(&self, expr: syn::Expr) -> syn::Expr {
match self.transform {
Some(FieldTransformation::Remote(_)) => parse_quote! { #expr.into() },
Some(FieldTransformation::Json) => parse_quote! { diesel_json::Json(#expr) },
Expand All @@ -127,7 +127,7 @@ impl ModelField {
}

#[allow(clippy::wrong_self_convention)]
pub fn from_transformed(&self, expr: syn::Expr) -> syn::Expr {
pub(crate) fn from_transformed(&self, expr: syn::Expr) -> syn::Expr {
match self.transform {
Some(FieldTransformation::Remote(_)) => parse_quote! { #expr.into() },
Some(FieldTransformation::Json) => parse_quote! { #expr.0 },
Expand All @@ -140,7 +140,7 @@ impl ModelField {
}
}

pub fn transform_type(&self) -> syn::Type {
pub(crate) fn transform_type(&self) -> syn::Type {
let ty = &self.ty;
match self.transform {
Some(FieldTransformation::Remote(ref ty)) => parse_quote! { #ty },
Expand All @@ -152,7 +152,7 @@ impl ModelField {
}
}

pub(super) fn column_ident(&self) -> syn::Ident {
pub(crate) fn column_ident(&self) -> syn::Ident {
syn::Ident::new(&self.column, self.ident.span())
}
}
4 changes: 2 additions & 2 deletions editoast/editoast_derive/src/modelv2/identifier.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::Fields;

#[derive(Debug, Clone, PartialEq, Hash, Eq)]
pub enum RawIdentifier {
pub(crate) enum RawIdentifier {
Field(syn::Ident),
Compound(Vec<syn::Ident>),
}

impl RawIdentifier {
pub fn get_idents(&self) -> Vec<syn::Ident> {
pub(crate) fn get_idents(&self) -> Vec<syn::Ident> {
match self {
Self::Field(ident) => vec![ident.clone()],
Self::Compound(idents) => idents.clone(),
Expand Down
2 changes: 1 addition & 1 deletion editoast/editoast_derive/src/modelv2/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::{
};

impl ModelConfig {
pub fn from_macro_args(
pub(crate) fn from_macro_args(
options: ModelArgs,
model_name: syn::Ident,
visibility: syn::Visibility,
Expand Down

0 comments on commit 2c46869

Please sign in to comment.