Skip to content

Commit

Permalink
Merge pull request #2140 from fzyzcjy/feat/12392
Browse files Browse the repository at this point in the history
Support more attributes ; Add examples ; Fix generated `use` statement pointing to self ; Fix linter warning avoid_return_types_on_setters ; Reduce number of generated files
  • Loading branch information
fzyzcjy authored Jun 22, 2024
2 parents b68eb1d + 1537b49 commit 5a914de
Show file tree
Hide file tree
Showing 502 changed files with 131,828 additions and 107,198 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ jobs:
- frb_example--flutter_via_create
- frb_example--flutter_via_integrate
- frb_example--flutter_package
- frb_example--rust_ui_counter--ui
- frb_example--rust_ui_todo_list--ui
exclude:
- { image: windows-2019, package: frb_example--deliberate_bad }
- { image: macos-11, package: frb_example--deliberate_bad }
Expand Down Expand Up @@ -756,6 +758,8 @@ jobs:
package:
- frb_example--flutter_via_create
- frb_example--flutter_package--example
- frb_example--rust_ui_counter--ui
- frb_example--rust_ui_todo_list--ui
# no `frb_example--flutter_via_integrate` since it is similar to `flutter_via_create`
device:
- "pixel"
Expand Down Expand Up @@ -822,6 +826,8 @@ jobs:
package:
- frb_example--flutter_via_create
- frb_example--flutter_package--example
- frb_example--rust_ui_counter--ui
- frb_example--rust_ui_todo_list--ui
# no `frb_example--flutter_via_integrate` since it is similar to `flutter_via_create`
device:
- "iPad (10th generation) Simulator (17.2)"
Expand Down Expand Up @@ -880,6 +886,26 @@ jobs:
platform: linux
package: frb_example--flutter_package--example

- image: windows-2019
platform: windows
package: frb_example--rust_ui_counter--ui
- image: macos-13
platform: macos
package: frb_example--rust_ui_counter--ui
- image: ubuntu-latest
platform: linux
package: frb_example--rust_ui_counter--ui

- image: windows-2019
platform: windows
package: frb_example--rust_ui_todo_list--ui
- image: macos-13
platform: macos
package: frb_example--rust_ui_todo_list--ui
- image: ubuntu-latest
platform: linux
package: frb_example--rust_ui_todo_list--ui

# only slightly test `flutter_via_integrate` since quite similar to `flutter_via_create`
- image: ubuntu-latest
platform: linux
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ exclude = [
"frb_example/pure_dart/rust",
"frb_example/pure_dart_pde/rust",
"frb_example/integrate_third_party/rust",
"frb_example/rust_ui_counter",
"frb_example/rust_ui_todo_list",
]
resolver = "2"

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,46 @@ impl SseEncode for bool {
}

#[cfg(not(target_family = "wasm"))]
#[path = "frb_generated.io.rs"]
mod io;
mod io {
// This file is automatically generated, so please do not edit it.
// Generated by `flutter_rust_bridge`@ 2.0.0.

// Section: imports

use super::*;
use flutter_rust_bridge::for_generated::byteorder::{
NativeEndian, ReadBytesExt, WriteBytesExt,
};
use flutter_rust_bridge::for_generated::{transform_result_dco, Lifetimeable, Lockable};
use flutter_rust_bridge::{Handler, IntoIntoDart};

// Section: boilerplate

flutter_rust_bridge::frb_generated_boilerplate_io!();
}
#[cfg(not(target_family = "wasm"))]
pub use io::*;

/// cbindgen:ignore
#[cfg(target_family = "wasm")]
#[path = "frb_generated.web.rs"]
mod web;
mod web {
// This file is automatically generated, so please do not edit it.
// Generated by `flutter_rust_bridge`@ 2.0.0.

// Section: imports

use super::*;
use flutter_rust_bridge::for_generated::byteorder::{
NativeEndian, ReadBytesExt, WriteBytesExt,
};
use flutter_rust_bridge::for_generated::wasm_bindgen;
use flutter_rust_bridge::for_generated::wasm_bindgen::prelude::*;
use flutter_rust_bridge::for_generated::{transform_result_dco, Lifetimeable, Lockable};
use flutter_rust_bridge::{Handler, IntoIntoDart};

// Section: boilerplate

flutter_rust_bridge::frb_generated_boilerplate_web!();
}
#[cfg(target_family = "wasm")]
pub use web::*;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use crate::codegen::config::internal_config::ControllerInternalConfig;
use crate::codegen::generator::misc::target::TargetOrCommonMap;
use crate::codegen::MetaConfig;
use std::path::{Path, PathBuf};
use std::path::Path;

pub(super) fn parse(
meta_config: &MetaConfig,
rust_crate_dir: &Path,
rust_output_path: &TargetOrCommonMap<PathBuf>,
rust_output_path: &Path,
) -> anyhow::Result<ControllerInternalConfig> {
let watching_paths = vec![
// The whole crate needs to be watched, because e.g. when a struct definition changes
// in a non-input file, it may still cause the generated code to change.
rust_crate_dir.join("src"),
];
let exclude_paths = rust_output_path.clone().into_vec();
let exclude_paths = vec![rust_output_path.to_owned()];

Ok(ControllerInternalConfig {
watch: meta_config.watch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::codegen::config::internal_config::{
use crate::codegen::config::internal_config_parser::dart_path_parser::DartOutputPathPack;
use crate::codegen::generator::api_dart::internal_config::GeneratorApiDartInternalConfig;
use crate::codegen::generator::codec::structs::CodecMode;
use crate::codegen::generator::misc::target::TargetOrCommonMap;
use crate::codegen::generator::wire::c::internal_config::GeneratorWireCInternalConfig;
use crate::codegen::generator::wire::dart::internal_config::{
DartOutputClassNamePack, GeneratorWireDartDefaultExternalLibraryLoaderInternalConfig,
Expand All @@ -29,7 +28,7 @@ pub(super) struct Args<'a> {
pub rust_crate_dir: &'a Path,
pub dart_output_path_pack: &'a DartOutputPathPack,
pub dart_output_class_name_pack: &'a DartOutputClassNamePack,
pub rust_output_path: &'a TargetOrCommonMap<PathBuf>,
pub rust_output_path: &'a Path,
pub default_stream_sink_codec: CodecMode,
pub default_rust_opaque_codec: RustOpaqueCodecMode,
pub c_output_path: &'a Option<PathBuf>,
Expand Down Expand Up @@ -90,7 +89,7 @@ pub(super) fn parse(args: Args) -> anyhow::Result<GeneratorInternalConfig> {
rust: GeneratorWireRustInternalConfig {
rust_crate_dir: rust_crate_dir.to_owned(),
web_enabled,
rust_output_path: rust_output_path.clone(),
rust_output_path: rust_output_path.to_owned(),
c_symbol_prefix: c_symbol_prefix.clone(),
has_ffigen: full_dep,
default_stream_sink_codec,
Expand All @@ -100,7 +99,7 @@ pub(super) fn parse(args: Args) -> anyhow::Result<GeneratorInternalConfig> {
c: GeneratorWireCInternalConfig {
enable: full_dep,
rust_crate_dir: rust_crate_dir.to_owned(),
rust_output_path: rust_output_path.clone(),
rust_output_path: rust_output_path.to_owned(),
c_output_path: c_output_path.clone(),
c_symbol_prefix,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,9 @@ mod tests {

assert!(result.is_err());
let error = result.err().unwrap();
assert!(error.to_string().contains("rust_output: is wrong:"));
assert!(error
.source()
.unwrap()
.to_string()
.contains("A path for input/output needs to include the file name"));
.contains("Rust output path needs to include the file name."));
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
use crate::codegen::config::internal_config_parser::dart_path_parser::compute_path_map;
use crate::codegen::config::internal_config_parser::rust_path_migrator::ConfigRustRootAndRustInput;
use crate::codegen::generator::misc::target::TargetOrCommonMap;
use crate::codegen::parser::mir::internal_config::RustInputNamespacePack;
use crate::utils::crate_name::CrateName;
use crate::utils::namespace::Namespace;
use crate::utils::path_utils::canonicalize_with_error_message;
use anyhow::Context;
use anyhow::ensure;
use itertools::Itertools;
use std::path::{Path, PathBuf};

pub(super) struct RustInputInfo {
pub rust_crate_dir: PathBuf,
pub third_party_crate_names: Vec<CrateName>,
pub rust_input_namespace_pack: RustInputNamespacePack,
pub rust_output_path: TargetOrCommonMap<PathBuf>,
pub rust_output_path: PathBuf,
}

pub(super) fn compute_rust_path_info(
Expand All @@ -28,7 +26,7 @@ pub(super) fn compute_rust_path_info(
let rust_output_path = compute_rust_output_path(config_rust_output, base_dir, &rust_crate_dir)?;

let rust_output_path_namespace =
Namespace::new_from_rust_crate_path(&rust_output_path.common, &rust_crate_dir)?;
Namespace::new_from_rust_crate_path(&rust_output_path, &rust_crate_dir)?;

Ok(RustInputInfo {
rust_crate_dir,
Expand Down Expand Up @@ -79,12 +77,21 @@ fn compute_rust_output_path(
config_rust_output: &Option<String>,
base_dir: &Path,
rust_crate_dir: &Path,
) -> anyhow::Result<TargetOrCommonMap<PathBuf>> {
let path_common = base_dir.join(
) -> anyhow::Result<PathBuf> {
let ans = base_dir.join(
(config_rust_output.clone().map(PathBuf::from))
.unwrap_or_else(|| fallback_rust_output_path(rust_crate_dir)),
);
compute_path_map(&path_common).context("rust_output: is wrong: ")

// We do not care about codecov for this, since it is just a sanity check warning
// frb-coverage:ignore-start
ensure!(
ans.extension().is_some(),
"Rust output path needs to include the file name."
);
// frb-coverage:ignore-end

Ok(ans)
}

fn fallback_rust_output_path(rust_crate_dir: &Path) -> PathBuf {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,14 @@ fn generate_signature(
) -> String {
let is_static_method = method_info.mode == MirFuncOwnerInfoMethodMode::Static;
let maybe_static = if is_static_method { "static" } else { "" };
let return_type = &api_dart_func.func_return_type;
let (func_params, maybe_accessor) = match func.accessor {
Some(MirFuncAccessorMode::Getter) => ("".to_owned(), "get"),
let (return_type, func_params, maybe_accessor) = match func.accessor {
Some(MirFuncAccessorMode::Getter) => (
api_dart_func.func_return_type.as_ref(),
"".to_owned(),
"get",
),
Some(MirFuncAccessorMode::Setter) => (
"",
// TODO: merge with below
format!(
"({})",
Expand All @@ -245,6 +249,7 @@ fn generate_signature(
"set",
),
None => (
api_dart_func.func_return_type.as_ref(),
format!("({})", compute_params_str(func_params, func.arg_mode)),
"",
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::codegen::generator::misc::target::TargetOrCommonMap;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub(crate) struct GeneratorWireCInternalConfig {
pub(crate) enable: bool,
pub(crate) rust_crate_dir: PathBuf,
pub(crate) rust_output_path: TargetOrCommonMap<PathBuf>,
pub(crate) rust_output_path: PathBuf,
pub(crate) c_output_path: Option<PathBuf>,
pub(crate) c_symbol_prefix: String,
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::codegen::generator::wire::dart::spec_generator::base::{
};
use crate::codegen::generator::wire::dart::spec_generator::output_code::WireDartOutputCode;
use crate::codegen::generator::wire::rust::spec_generator::extern_func::ExternFunc;
use crate::codegen::ir::mir::func::MirFuncMode;
use crate::codegen::ir::mir::pack::MirPackComputedCache;
use crate::codegen::misc::GeneratorProgressBarPack;
use crate::library::codegen::generator::wire::dart::spec_generator::misc::ty::WireDartGeneratorMiscTrait;
Expand All @@ -29,6 +30,7 @@ pub(crate) struct WireDartOutputSpecMisc {
pub(crate) boilerplate: Acc<Vec<WireDartOutputCode>>,
pub(crate) api_impl_normal_functions: Vec<WireDartOutputCode>,
pub(crate) extra_functions: Acc<Vec<WireDartOutputCode>>,
pub(crate) extra_from_parser: Acc<Vec<WireDartOutputCode>>,
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -63,6 +65,11 @@ pub(crate) fn generate(
extra_functions: (cache.distinct_types.iter())
.flat_map(|ty| WireDartGenerator::new(ty.clone(), context).generate_extra_functions())
.collect(),
extra_from_parser: Acc::new_common(vec![WireDartOutputCode {
header: context.mir_pack.extra_dart_output_code.header.clone(),
body: context.mir_pack.extra_dart_output_code.body.clone(),
..Default::default()
}]),
})
}

Expand Down Expand Up @@ -102,7 +109,17 @@ fn generate_boilerplate(

let execute_rust_initializers = (context.mir_pack.funcs_with_impl().iter())
.filter(|f| f.initializer)
.map(|f| format!("await api.{}();\n", f.name_dart_wire()))
.map(|f| {
format!(
"{maybe_await}api.{name}();\n",
maybe_await = if f.mode == MirFuncMode::Normal {
"await "
} else {
""
},
name = f.name_dart_wire()
)
})
.join("");

let codegen_version = env!("CARGO_PKG_VERSION");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fn generate_merged_code(spec: &WireDartOutputSpec) -> Acc<WireDartOutputCode> {
);
// add("wire_delegate_functions", &spec.misc.wire_delegate_functions);
add("extra_functions", &spec.misc.extra_functions);
add("extra_from_parser", &spec.misc.extra_from_parser);
add("wire_class", &spec.misc.wire_class);
add("rust2dart", &spec.rust2dart.inner);
add("dart2rust", &spec.dart2rust.inner);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::codegen::generator::codec::structs::CodecMode;
use crate::codegen::generator::misc::target::TargetOrCommonMap;
use crate::codegen::ir::mir::ty::rust_opaque::RustOpaqueCodecMode;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
Expand All @@ -8,7 +7,7 @@ use std::path::PathBuf;
pub(crate) struct GeneratorWireRustInternalConfig {
pub rust_crate_dir: PathBuf,
pub web_enabled: bool,
pub rust_output_path: TargetOrCommonMap<PathBuf>,
pub rust_output_path: PathBuf,
pub c_symbol_prefix: String,
pub has_ffigen: bool,
pub default_stream_sink_codec: CodecMode,
Expand Down
Loading

0 comments on commit 5a914de

Please sign in to comment.