Skip to content

Commit

Permalink
Fixed usage of Self in a model's fields
Browse files Browse the repository at this point in the history
Fixes #66
  • Loading branch information
gammelalf committed Sep 6, 2024
1 parent 78546df commit 18ef1c0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
5 changes: 1 addition & 4 deletions rorm-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ proc-macro = true

[dependencies]
# syn builds rust syntax trees from strings or tokenstream
syn = { version = "~1" }
syn = { version = "~1", features = ["full", "visit-mut"] }
# quote provides a macro to write rust code with template variables which then produces a tokenstream
quote = { version = "~1" }
# a higher level wrapper for rust's proc-macro which is used by syn and quote
Expand All @@ -28,8 +28,5 @@ darling = { version = "~0.14" }
rustc_version = "0.4.0"

[features]
default = [
"syn/full"
]
# requires nightly rust
unstable = []
14 changes: 13 additions & 1 deletion rorm-macro/src/analyze/model.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use proc_macro2::Ident;
use quote::format_ident;
use syn::visit_mut::VisitMut;
use syn::{LitInt, LitStr, Type, Visibility};

use crate::analyze::vis_to_display;
Expand Down Expand Up @@ -39,7 +40,7 @@ pub fn analyze_model(parsed: ParsedModel) -> darling::Result<AnalyzedModel> {
let ParsedField {
vis,
ident,
ty,
mut ty,
annos:
ModelFieldAnnotations {
auto_create_time,
Expand Down Expand Up @@ -86,6 +87,17 @@ pub fn analyze_model(parsed: ParsedModel) -> darling::Result<AnalyzedModel> {
auto_increment = true;
}

// Replace `Self` in the field's type to the model's identifier
struct ReplaceSelf<'a>(&'a Ident);
impl<'a> VisitMut for ReplaceSelf<'a> {
fn visit_ident_mut(&mut self, i: &mut Ident) {
if i == "Self" {
*i = self.0.clone();
}
}
}
ReplaceSelf(model_ident).visit_type_mut(&mut ty);

analyzed_fields.push(AnalyzedField {
vis,
unit: format_ident!("__{}_{}", model_ident, ident),
Expand Down

0 comments on commit 18ef1c0

Please sign in to comment.