Skip to content

Commit

Permalink
feat: ty
Browse files Browse the repository at this point in the history
  • Loading branch information
fzyzcjy committed Jun 18, 2024
1 parent 64a86af commit 7c27105
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@ use crate::if_then_some;
use anyhow::{bail, Context};
use itertools::Itertools;
use syn::{parse_str, Type};
use crate::codegen::parser::mir::parser::ty::ty_or_skip::MirTypeOrSkip;

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<MirType>> {
) -> anyhow::Result<Option<MirTypeOrSkip>> {
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 {
Ok(Some(MirTypeOrSkip::Type(match last_segment {
("Self", []) => self.parse_type_self()?,

("Duration", []) if check_prefix("chrono") => Delegate(MirTypeDelegate::Time(MirTypeDelegateTime::Duration)),
Expand Down Expand Up @@ -96,7 +97,7 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
})),

_ => return Ok(None),
}))
})))
}

fn parse_type_self(&mut self) -> anyhow::Result<MirType> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::codegen::ir::mir::ty::delegate::{MirTypeDelegate, MirTypeDelegateCustomSerDes};
use crate::codegen::ir::mir::ty::MirType;
use crate::codegen::parser::mir::parser::ty::ty_or_skip::MirTypeOrSkip;
use crate::codegen::parser::mir::parser::ty::unencodable::SplayedSegment;
use crate::codegen::parser::mir::parser::ty::TypeParserWithContext;
use crate::library::codegen::ir::mir::ty::MirTypeTrait;
Expand All @@ -8,7 +9,7 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
pub(crate) fn parse_type_path_data_custom_ser_des(
&mut self,
last_segment: &SplayedSegment,
) -> anyhow::Result<Option<MirType>> {
) -> anyhow::Result<Option<MirTypeOrSkip>> {
// use HashMap etc later if too slow; here we use filter to remain flexibility of filtering strategy
Ok((self.inner.custom_ser_des_infos.iter())
.find(|info| info.rust_api_type.rust_api_type() == last_segment.0)
Expand Down
8 changes: 6 additions & 2 deletions frb_codegen/src/library/codegen/parser/mir/parser/ty/path.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use crate::codegen::ir::mir::ty::MirType;
use crate::codegen::parser::mir::parser::ty::path_data::extract_path_data;
use crate::codegen::parser::mir::parser::ty::ty_or_skip::MirTypeOrSkip;
use crate::codegen::parser::mir::parser::ty::unencodable::splay_segments;
use crate::codegen::parser::mir::parser::ty::TypeParserWithContext;
use anyhow::bail;
use quote::ToTokens;
use syn::{Path, QSelf, TypePath};

impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
pub(crate) fn parse_type_path(&mut self, type_path: &TypePath) -> anyhow::Result<MirType> {
pub(crate) fn parse_type_path(
&mut self,
type_path: &TypePath,
) -> anyhow::Result<MirTypeOrSkip> {
match &type_path {
TypePath { qself: None, path } => self.parse_type_path_core(type_path, path),
// This branch simply halts the generator with an error message, so we do not test it
Expand All @@ -28,7 +32,7 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
&mut self,
type_path: &TypePath,
path: &Path,
) -> anyhow::Result<MirType> {
) -> anyhow::Result<MirTypeOrSkip> {
let segments = extract_path_data(path)?;
let splayed_segments = splay_segments(&segments);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use crate::codegen::ir::mir::ty::MirType;
use crate::codegen::ir::mir::ty::MirType::Primitive;
use crate::codegen::parser::mir::parser::ty::unencodable::SplayedSegment;
use crate::codegen::parser::mir::parser::ty::{TypeParserParsingContext, TypeParserWithContext};
use crate::codegen::parser::mir::parser::ty::ty_or_skip::MirTypeOrSkip;

impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
pub(crate) fn parse_type_path_data_primitive(
&mut self,
last_segment: &SplayedSegment,
) -> anyhow::Result<Option<MirType>> {
) -> anyhow::Result<Option<MirTypeOrSkip>> {
Ok(Some(match last_segment {
// TODO: change to "if let guard" https://github.com/rust-lang/rust/issues/51114
(name, []) if matches!(parse_primitive(name, self.context), Some(..)) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ use crate::utils::namespace::{Namespace, NamespacedName};
use anyhow::bail;
use std::collections::HashMap;
use syn::{Field, Fields, FieldsNamed, FieldsUnnamed, ItemStruct, Type, Visibility};
use crate::codegen::parser::mir::parser::ty::ty_or_skip::MirTypeOrSkip;

impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
pub(crate) fn parse_type_path_data_struct(
&mut self,
path: &syn::Path,
last_segment: &SplayedSegment,
override_opaque: Option<bool>,
) -> anyhow::Result<Option<MirType>> {
) -> anyhow::Result<Option<MirTypeOrSkip>> {
EnumOrStructParserStruct(self).parse(path, last_segment, override_opaque)
}

Expand Down
3 changes: 2 additions & 1 deletion frb_codegen/src/library/codegen/parser/mir/parser/ty/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::codegen::parser::mir::parser::ty::{TypeParserParsingContext, TypePars
use crate::utils::syn_utils::ty_to_string;
use anyhow::Context;
use syn::Type;
use crate::codegen::parser::mir::parser::ty::ty_or_skip::MirTypeOrSkip;

impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
pub(crate) fn parse_type(&mut self, ty: &Type) -> anyhow::Result<MirType> {
Expand All @@ -29,7 +30,7 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
self_with_context.parse_type(ty)
}

fn parse_type_inner(&mut self, ty: &Type) -> anyhow::Result<MirType> {
fn parse_type_inner(&mut self, ty: &Type) -> anyhow::Result<MirTypeOrSkip> {
Ok(match ty.clone() {
Type::Path(x) => self.parse_type_path(&x)?,
Type::Array(x) => self.parse_type_array(&x)?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ impl MirTypeOrSkip {
}
}
}

impl From<MirType> for MirTypeOrSkip {
fn from(value: MirType) -> Self {
Self::Type(value)
}
}

0 comments on commit 7c27105

Please sign in to comment.