Skip to content

Commit

Permalink
Merge pull request #2391 from fzyzcjy/feat/12629
Browse files Browse the repository at this point in the history
  • Loading branch information
fzyzcjy authored Nov 5, 2024
2 parents 648c239 + 0dd8486 commit 57eb253
Show file tree
Hide file tree
Showing 49 changed files with 227,260 additions and 226,628 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<'a> EnumRefApiDartGenerator<'a> {
self.mir.ident.0.name,
variant.name.dart_style(),
args,
variant.wrapper_name.rust_style(),
variant.wrapper_name.rust_style(true),
)
}

Expand Down Expand Up @@ -130,7 +130,7 @@ impl<'a> EnumRefApiDartGenerator<'a> {

fn generate_implements_exception(&self, variant: &MirEnumVariant) -> &str {
let has_backtrace = matches!(&variant.kind,
MirVariantKind::Struct(MirStruct {is_fields_named: true, fields, ..}) if fields.iter().any(|field| field.name.rust_style() == BACKTRACE_IDENT));
MirVariantKind::Struct(MirStruct {is_fields_named: true, fields, ..}) if fields.iter().any(|field| field.name.rust_style(true) == BACKTRACE_IDENT));
if self.mir.is_exception && has_backtrace {
"@Implements<FrbBacktracedException>()"
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<'a> EnumRefApiDartGenerator<'a> {
let variant_name = if self.context.config.dart_enums_style {
dart_keywords::escape(variant.name.dart_style())
} else {
variant.name.rust_style().to_string()
variant.name.rust_style(true).to_string()
};

format!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub(crate) fn generate(
let header = generate_header(func, context)?;

Ok(ApiDartGeneratedFunction {
namespace: func.name.namespace.clone(),
namespace: func.namespace.clone(),
header,
func_comments,
func_expr,
Expand Down Expand Up @@ -194,7 +194,7 @@ fn generate_header(
) -> anyhow::Result<DartHeaderCode> {
Ok(DartHeaderCode {
import: generate_imports_which_types_and_funcs_use(
&func.name.namespace.clone(),
&func.namespace.clone(),
&None,
&Some(&vec![func]),
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(crate) fn generate(
.dump("api_dart.json", &generate_dump_info(&cache, context))?;

let funcs_with_impl = mir_pack.funcs_with_impl();
let grouped_funcs = (funcs_with_impl.iter()).into_group_map_by(|x| x.name.namespace.clone());
let grouped_funcs = (funcs_with_impl.iter()).into_group_map_by(|x| x.namespace.clone());
let grouped_namespaced_types = (cache.distinct_types.iter())
.filter(|x| x.self_namespace().is_some())
.into_group_map_by(|x| x.self_namespace().unwrap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<'a> CodecSseTyTrait for EnumRefCodecSseTy<'a> {
.map(|field| {
format!(
"{};\n",
lang.call_encode(&field.ty, &field.name.style(lang))
lang.call_encode(&field.ty, &field.name.style(lang, false))
)
})
.join("");
Expand Down Expand Up @@ -133,7 +133,7 @@ fn pattern_match_enum_variant(lang: &Lang, variant: &MirEnumVariant) -> String {
}
Lang::RustLang(_) => {
let pattern = (st.fields.iter())
.map(|field| field.name.rust_style().to_owned())
.map(|field| field.name.rust_style(true).to_owned())
.join(",");
let (left, right) = st.brackets_pair();
format!("{left}{pattern}{right}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<'a> GeneralizedStructGenerator<'a> {
self.context.as_api_dart_context(),
),
&(self.st.fields.iter())
.map(|x| x.name.style(lang))
.map(|x| x.name.style(lang, false))
.collect_vec(),
&(self.st.fields.iter())
.map(|x| format!("var_{}", x.name.dart_style().clone()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl StructOrRecord {
Lang::RustLang(_) => match self {
StructOrRecord::Struct => {
if is_field_named {
field.name.rust_style().to_owned()
field.name.rust_style(false).to_owned()
} else {
format!("{}", index)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'a> EnumRefWireDartCodecCstGenerator<'a> {
.map(|field| {
format!(
"var pre_{} = cst_encode_{}(apiObj.{});",
field.name.rust_style(),
field.name.rust_style(true),
field.ty.safe_ident(),
field.name.dart_style()
)
Expand All @@ -72,7 +72,10 @@ impl<'a> EnumRefWireDartCodecCstGenerator<'a> {
.fields
.iter()
.map(|field| {
format!("{r}.{name} = pre_{name};", name = field.name.rust_style())
format!(
"{r}.{name} = pre_{name};",
name = field.name.rust_style(true)
)
})
.join("\n");

Expand Down Expand Up @@ -108,6 +111,6 @@ fn generate_encode_body_variant(index: usize, variant: &MirEnumVariant) -> Strin
"if (raw is {variant}) {{
return [{index} {fields}].jsify()!;
}}",
variant = variant.wrapper_name.rust_style(),
variant = variant.wrapper_name.rust_style(true),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn generate_companion_field(func: &MirFunc, const_meta_field_name: &str) -> Stri
argNames: [{}],
);
"#,
func.name.name,
func.name,
func.inputs
.iter()
.map(|input| format!("\"{}\"", input.inner.name.dart_style()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl<'a> EnumRefWireRustCodecCstGenerator<'a> {
WireRustCodecCstGenerator::new(field.ty.clone(), self.context);
format!(
"{}: {}{},",
field.name.rust_style(),
field.name.rust_style(true),
field_generator.rust_wire_modifier(Target::Io),
field_generator.rust_wire_type(Target::Io)
)
Expand Down Expand Up @@ -165,7 +165,7 @@ fn generate_impl_cst_decode_body_variant(
.iter()
.enumerate()
.map(|(idx, field)| {
let field_name = field.name.rust_style();
let field_name = field.name.rust_style(true);
let field_ = if st.is_fields_named {
format!("{field_name}: ")
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl<'a> WireRustCodecCstGeneratorDecoderTrait for RecordWireRustCodecCstGenerat
.enumerate()
.map(|(idx, field)| Acc {
web: format!("self_.get({idx}).cst_decode()"),
io: format!("self.{}.cst_decode()", field.name.rust_style()),
io: format!("self.{}.cst_decode()", field.name.rust_style(true)),
..Default::default()
})
.collect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<'a> WireRustCodecCstGeneratorDecoderTrait for StructRefWireRustCodecCstGene
WireRustCodecCstGenerator::new(field.ty.clone(), self.context);
format!(
"{}: {}{}",
field.name.rust_style(),
field.name.rust_style(false),
field_generator.rust_wire_modifier(Target::Io),
field_generator.rust_wire_type(Target::Io)
)
Expand All @@ -39,7 +39,7 @@ impl<'a> WireRustCodecCstGeneratorDecoderTrait for StructRefWireRustCodecCstGene
.iter()
.enumerate()
.map(|(idx, field)| {
let field_name = field.name.rust_style();
let field_name = field.name.rust_style(false);
let field_ = if api_struct.is_fields_named {
format!("{field_name}: ")
} else {
Expand Down Expand Up @@ -85,7 +85,7 @@ impl<'a> WireRustCodecCstGeneratorDecoderTrait for StructRefWireRustCodecCstGene
.map(|field| {
format!(
"{}: {},",
field.name.rust_style(),
field.name.rust_style(false),
if WireRustCodecCstGenerator::new(field.ty.clone(), self.context)
.rust_wire_is_pointer(Target::Io)
|| matches!(field.ty, MirType::DartOpaque(_))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl WireRustCodecEntrypointTrait<'_> for CstWireRustCodecEntrypoint {
.inputs
.iter()
.map(|field| {
let name = field.inner.name.rust_style().to_owned();
let name = field.inner.name.rust_style(false).to_owned();
Acc::new(|target| match target {
TargetOrCommon::Common => ExternFuncParam {
name: name.clone(),
Expand Down Expand Up @@ -94,13 +94,14 @@ impl WireRustCodecEntrypointTrait<'_> for CstWireRustCodecEntrypoint {
.map(|field| {
let gen = WireRustGenerator::new(field.inner.ty.clone(), context);

let name = field.inner.name.rust_style();
let mut expr = format!("{name}.cst_decode()");
let name_non_stripped = field.inner.name.rust_style(false);
let name_stripped = field.inner.name.rust_style(true);
let mut expr = format!("{name_non_stripped}.cst_decode()");
if let Some(wrapper) = gen.generate_wire_func_call_decode_wrapper() {
expr = format!("{wrapper}({expr})");
}

format!("let api_{name} = {expr};")
format!("let api_{name_stripped} = {expr};")
})
.join("")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ impl<'a> WireRustCodecDcoGeneratorEncoderTrait for EnumRefWireRustCodecDcoGenera
let tag = format!("{idx}.into_dart()");
let fields = (Some(tag).into_iter())
.chain(variant.kind.fields().iter().map(|field| {
format!("{}.into_into_dart().into_dart()", field.name.rust_style())
format!(
"{}.into_into_dart().into_dart()",
field.name.rust_style(false)
)
}))
.join(",\n");
format!("[{fields}].into_dart()")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<'a> WireRustCodecDcoGeneratorEncoderTrait for StructRefWireRustCodecDcoGene
.enumerate()
.map(|(i, field)| {
let field_ref = if src.is_fields_named {
field.name.rust_style().to_string()
field.name.rust_style(false).to_string()
} else {
i.to_string()
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl WireRustCodecEntrypointTrait<'_> for SseWireRustCodecEntrypoint {
.map(|field| {
let gen = WireRustGenerator::new(field.inner.ty.clone(), context);

let name = field.inner.name.rust_style();
let name = field.inner.name.rust_style(true);
let effective_rust_api_type = (gen.generate_wire_func_call_decode_type())
.unwrap_or_else(|| field.inner.ty.rust_api_type());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn is_interest_field(field: &MirFuncInput) -> bool {
}

fn get_variable_name(field: &MirFuncInput) -> String {
field.inner.name.rust_style()
field.inner.name.rust_style(true)
}

pub(crate) fn generate_inner_func_arg(raw: &str, field: &MirFuncInput) -> String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn generate_decode_statement(
}

fn get_variable_name(field: &MirFuncInput) -> String {
field.inner.name.rust_style()
field.inner.name.rust_style(true)
}

fn filter_interest_fields(func: &MirFunc) -> Vec<FieldInfo> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn generate_inner_func_args(func: &MirFunc) -> Vec<String> {
let ownership = lockable::generate_inner_func_arg_ownership(field);
let ans = format!(
"{ownership}api_{name}",
name = field.inner.name.rust_style()
name = field.inner.name.rust_style(true)
);
let ans = lockable::generate_inner_func_arg(&ans, field);

Expand All @@ -93,7 +93,7 @@ fn generate_inner_func_args(func: &MirFunc) -> Vec<String> {
fn generate_wrap_info_obj(func: &MirFunc) -> String {
format!(
"flutter_rust_bridge::for_generated::TaskInfo{{ debug_name: \"{name}\", port: {port}, mode: flutter_rust_bridge::for_generated::FfiCallMode::{mode} }}",
name = func.name.name,
name = func.name,
port = if has_port_argument(func.mode) {
"Some(port_)"
} else {
Expand All @@ -111,7 +111,11 @@ fn generate_code_call_inner_func_result(func: &MirFunc, inner_func_args: Vec<Str
let mut ans = (func.rust_call_code.clone()).unwrap_or_else(|| {
match &func.owner {
MirFuncOwnerInfo::Function => {
format!("{}({})", func.name.rust_style(), inner_func_args.join(", "))
format!(
"{}({})",
func.namespaced_name_rust_style(false),
inner_func_args.join(", ")
)
}
MirFuncOwnerInfo::Method(method) => {
let owner_ty_name = method.owner_ty_name().unwrap().rust_style();
Expand Down Expand Up @@ -254,8 +258,11 @@ fn generate_redirect_body(func: &MirFunc, params: &[ExternFuncParam]) -> String
}

pub(crate) fn wire_func_name(func: &MirFunc) -> String {
let name = &func.name;
format!("wire__{}__{}", name.namespace.safe_ident(), name.name)
format!(
"wire__{}__{}",
func.namespace.safe_ident(),
&func.name.rust_style(true)
)
}

fn ffi_call_mode(mode: MirFuncMode) -> &'static str {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ fn generate_content_hash(mir_pack: &MirPack) -> i32 {
let mut hasher = Sha1::new();
hasher.update(
(mir_pack.funcs_with_impl().iter())
.map(|func| func.name.rust_style())
.map(|func| func.namespaced_name_rust_style(true))
.sorted()
.join("\n")
.as_bytes(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl<'a> WireRustGeneratorMiscTrait for EnumRefWireRustGenerator<'a> {
let pattern = s
.fields
.iter()
.map(|field| field.name.rust_style().to_owned())
.map(|field| field.name.rust_style(true).to_owned())
.collect_vec();
let pattern = if s.is_fields_named {
format!(
Expand All @@ -50,7 +50,7 @@ impl<'a> WireRustGeneratorMiscTrait for EnumRefWireRustGenerator<'a> {
format!(
"let _: {} = {};\n",
field.ty.rust_api_type(),
field.name.rust_style(),
field.name.rust_style(true),
)
})
.collect_vec();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<'a> WireRustGeneratorMiscTrait for StructRefWireRustGenerator<'a> {
.enumerate()
.map(|(i, field)| {
let field_access = if src.is_fields_named {
field.name.rust_style().to_owned()
field.name.rust_style(true).to_owned()
} else {
i.to_string()
};
Expand Down
21 changes: 15 additions & 6 deletions frb_codegen/src/library/codegen/ir/mir/func.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use crate::codegen::generator::codec::structs::CodecModePack;
use crate::codegen::ir::mir::comment::MirComment;
use crate::codegen::ir::mir::field::MirField;
use crate::codegen::ir::mir::ident::MirIdent;
use crate::codegen::ir::mir::ty::delegate::{
MirTypeDelegate, MirTypeDelegatePrimitiveEnum, MirTypeDelegateProxyVariant,
};
use crate::codegen::ir::mir::ty::primitive::MirTypePrimitive;
use crate::codegen::ir::mir::ty::trait_def::MirTypeTraitDef;
use crate::codegen::ir::mir::ty::{MirContext, MirType, MirTypeTrait};
use crate::if_then_some;
use crate::utils::namespace::NamespacedName;
use crate::utils::namespace::{Namespace, NamespacedName};
use convert_case::{Case, Casing};
use itertools::Itertools;

crate::mir! {
pub struct MirFunc {
pub name: NamespacedName,
pub dart_name: Option<String>,
pub namespace: Namespace,
pub name: MirIdent,
pub id: Option<i32>,
pub inputs: Vec<MirFuncInput>,
pub output: MirFuncOutput,
Expand Down Expand Up @@ -178,15 +179,23 @@ impl MirFunc {
// }
// }

pub(crate) fn namespaced_name_rust_style(&self, strip_raw_identifier: bool) -> String {
NamespacedName::new(
self.namespace.clone(),
self.name.rust_style(strip_raw_identifier),
)
.rust_style()
}

pub(crate) fn name_dart_api(&self) -> String {
(self.dart_name.clone()).unwrap_or_else(|| self.name.name.to_owned().to_case(Case::Camel))
self.name.dart_style()
}

pub(crate) fn name_dart_wire(&self) -> String {
let raw = format!(
"{}_{}",
self.name.namespace.path().into_iter().join("_"),
self.name.name
self.namespace.path().into_iter().join("_"),
self.name.rust_style(true)
);
raw.to_case(Case::Camel)
}
Expand Down
Loading

0 comments on commit 57eb253

Please sign in to comment.