Skip to content

Commit

Permalink
Merge pull request #1782 from fzyzcjy/feat/1768
Browse files Browse the repository at this point in the history
Auto understand when user structs have name conflict with builtin types
  • Loading branch information
fzyzcjy authored Feb 26, 2024
2 parents 73293cf + 23d16a2 commit 5b0b200
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
16 changes: 12 additions & 4 deletions frb_codegen/src/library/codegen/parser/type_parser/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,29 @@ use crate::codegen::parser::type_parser::unencodable::{splay_segments, SplayedSe
use crate::codegen::parser::type_parser::TypeParserWithContext;
use crate::if_then_some;
use anyhow::bail;
use itertools::Itertools;
use syn::{parse_str, Type};

impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
pub(crate) fn parse_type_path_data_concrete(
&mut self,
last_segment: &SplayedSegment,
splayed_segments: &[SplayedSegment],
) -> anyhow::Result<Option<IrType>> {
let non_last_segments = (splayed_segments.split_last().unwrap().1.iter())
.map(|segment| segment.0)
.join("::");
let check_prefix =
|matcher: &str| non_last_segments == matcher || non_last_segments.is_empty();

Ok(Some(match last_segment {
("Self", []) => self.parse_type_self()?,

("Duration", []) => Delegate(IrTypeDelegate::Time(IrTypeDelegateTime::Duration)),
("NaiveDateTime", []) => Delegate(IrTypeDelegate::Time(IrTypeDelegateTime::Naive)),
("DateTime", args) => self.parse_datetime(args)?,
("Duration", []) if check_prefix("chrono") => Delegate(IrTypeDelegate::Time(IrTypeDelegateTime::Duration)),
("NaiveDateTime", []) if check_prefix("chrono") => Delegate(IrTypeDelegate::Time(IrTypeDelegateTime::Naive)),
("DateTime", args) if check_prefix("chrono") => self.parse_datetime(args)?,

("Uuid", []) => Delegate(IrTypeDelegate::Uuid),
("Uuid", []) if check_prefix("uuid") => Delegate(IrTypeDelegate::Uuid),
("String", []) => Delegate(IrTypeDelegate::String),
("Backtrace", []) => Delegate(IrTypeDelegate::Backtrace),

Expand Down
8 changes: 5 additions & 3 deletions frb_codegen/src/library/codegen/parser/type_parser/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
if let Some(ans) = self.parse_type_path_data_primitive(last_segment)? {
return Ok(ans);
}
if let Some(ans) =
self.parse_type_path_data_concrete(last_segment, &splayed_segments)?
{
return Ok(ans);
}
if let Some(ans) = self.parse_type_path_data_struct(type_path, last_segment)? {
return Ok(ans);
}
Expand All @@ -44,9 +49,6 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
if let Some(ans) = self.parse_type_path_data_rust_opaque(last_segment)? {
return Ok(ans);
}
if let Some(ans) = self.parse_type_path_data_concrete(last_segment)? {
return Ok(ans);
}
if let Some(ans) = self.parse_type_path_data_optional(type_path, last_segment)? {
return Ok(ans);
}
Expand Down

0 comments on commit 5b0b200

Please sign in to comment.