Skip to content

Commit

Permalink
formatted code
Browse files Browse the repository at this point in the history
  • Loading branch information
chavic committed Aug 21, 2024
1 parent 9ec8e66 commit 4f85c67
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 64 deletions.
2 changes: 1 addition & 1 deletion fixtures/futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,5 @@ pub async fn broken_sleep(ms: u16, fail_after: u16) {
)
.await;
}

uniffi::include_scaffolding!("api");
2 changes: 1 addition & 1 deletion fixtures/large_enum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub fn new_public_key_value(value: Vec<i32>) -> Value {

// #[uniffi::export]
// pub fn new_map(value: HashMap<String, Value>) -> MapEntry {
// todo!("Not done")
// todo!("Not done")
// }

#[uniffi::export]
Expand Down
1 change: 0 additions & 1 deletion src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ pub fn generate_scaffolding(udl_file: &Utf8Path) -> Result<()> {
)?;
Ok(())
}

1 change: 0 additions & 1 deletion src/gen/compounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,3 @@ impl_code_type_for_compound!(SequenceCodeType, "List<{}>", "Sequence{}");

impl_renderable_for_compound!(OptionalCodeType, "{}?", "FfiConverterOptional{}");
impl_renderable_for_compound!(SequenceCodeType, "FfiConverterSequence{}");

1 change: 0 additions & 1 deletion src/gen/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,3 @@ pub fn generate_enum(obj: &Enum, type_helper: &dyn TypeHelperRenderer) -> dart::
}
}
}

1 change: 0 additions & 1 deletion src/gen/functions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use genco::prelude::*;
use uniffi_bindgen::backend::Type;
use uniffi_bindgen::interface::{AsType, Callable, ExternalKind, Function};
Expand Down
9 changes: 5 additions & 4 deletions src/gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use genco::fmt;
use genco::prelude::*;
use serde::{Deserialize, Serialize};
// use uniffi_bindgen::MergeWith;
use crate::gen::oracle::DartCodeOracle;
use uniffi_bindgen::{BindingGenerator, BindingsConfig, ComponentInterface};
use self::render::Renderer;
use self::types::TypeHelpersRenderer;
use crate::gen::oracle::DartCodeOracle;
use uniffi_bindgen::{BindingGenerator, BindingsConfig, ComponentInterface};

mod compounds;
mod enums;
Expand Down Expand Up @@ -135,8 +135,9 @@ impl<'a> DartWrapper<'a> {
native_args.append(
quote!($(DartCodeOracle::ffi_native_type_label(Some(&arg.type_()))),),
);
dart_args
.append(quote!($(DartCodeOracle::ffi_dart_type_label(Some(&arg.type_()))),));
dart_args.append(
quote!($(DartCodeOracle::ffi_dart_type_label(Some(&arg.type_()))),),
);
}

if fun.has_rust_call_status_arg() {
Expand Down
7 changes: 3 additions & 4 deletions src/gen/objects.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

use genco::prelude::*;
use uniffi_bindgen::backend::{CodeType, Literal};
use uniffi_bindgen::interface::{AsType, Method, Object};

use crate::gen::oracle::{DartCodeOracle, AsCodeType};
use crate::gen::oracle::{AsCodeType, DartCodeOracle};
use crate::gen::render::AsRenderable;
use crate::gen::render::{Renderable, TypeHelperRenderer};

Expand Down Expand Up @@ -82,7 +81,7 @@ pub fn generate_object(obj: &Object, type_helper: &dyn TypeHelperRenderer) -> da
}

#[allow(unused_variables)]
pub fn generate_method(func: &Method, type_helper: &dyn TypeHelperRenderer) -> dart::Tokens {
pub fn generate_method(func: &Method, type_helper: &dyn TypeHelperRenderer) -> dart::Tokens {
// let api = "_api";
// let ffi = fun.ffi_func();
// let fn_name = fn_name(fun.name());
Expand Down Expand Up @@ -166,4 +165,4 @@ pub fn generate_method(func: &Method, type_helper: &dyn TypeHelperRenderer) -> d
}
)
}
}
}
112 changes: 86 additions & 26 deletions src/gen/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ impl DartCodeOracle {
panic!("unsupported type for error: {type_:?}")
}


/// Sanitize a Dart identifier, appending an underscore if it's a reserved keyword.
pub fn sanitize_identifier(id: &str) -> String {
if Self::is_reserved_identifier(id) {
Expand Down Expand Up @@ -63,7 +62,10 @@ impl DartCodeOracle {

/// Get the idiomatic Dart rendering of an FFI callback function name
fn ffi_callback_name(nm: &str) -> String {
format!("Pointer<NativeFunction<Uniffi{}>>", nm.to_upper_camel_case())
format!(
"Pointer<NativeFunction<Uniffi{}>>",
nm.to_upper_camel_case()
)
}

/// Get the idiomatic Dart rendering of an exception name
Expand All @@ -85,30 +87,30 @@ impl DartCodeOracle {
return quote!(void);
};
match ret_type {
FfiType::UInt8 |
FfiType::UInt16 |
FfiType::UInt32 |
FfiType::UInt64 |
FfiType::Int8 |
FfiType::Int16 |
FfiType::Int32 |
FfiType::Handle |
FfiType::Int64 => quote!(int),
FfiType::UInt8
| FfiType::UInt16
| FfiType::UInt32
| FfiType::UInt64
| FfiType::Int8
| FfiType::Int16
| FfiType::Int32
| FfiType::Handle
| FfiType::Int64 => quote!(int),
FfiType::Float32 | FfiType::Float64 => quote!(double),
FfiType::RustBuffer(ref inner) => match inner {
Some(i) => quote!($i),
_ => quote!(RustBuffer),
},
FfiType::ForeignBytes => quote!(ForeignBytes),
FfiType::RustArcPtr(_) => quote!(Pointer<Void>),
FfiType::Callback (name) => quote!($(Self::ffi_callback_name(name))),
FfiType::Callback(name) => quote!($(Self::ffi_callback_name(name))),
_ => todo!("FfiType::{:?}", ret_type),
}
}

pub fn ffi_native_type_label(ffi_ret_type: Option<&FfiType>) -> dart::Tokens {
let Some(ret_type) = ffi_ret_type else {
return quote!(Void)
return quote!(Void);
};
match ret_type {
FfiType::UInt8 => quote!(Uint8),
Expand All @@ -128,7 +130,7 @@ impl DartCodeOracle {
},
FfiType::ForeignBytes => quote!(ForeignBytes),
FfiType::RustArcPtr(_) => quote!(Pointer<Void>),
FfiType::Callback (name) => quote!($(Self::ffi_callback_name(name))),
FfiType::Callback(name) => quote!($(Self::ffi_callback_name(name))),
_ => todo!("FfiType::{:?}", ret_type),
}
}
Expand Down Expand Up @@ -169,7 +171,9 @@ impl DartCodeOracle {
| Type::Object { .. }
| Type::Enum { .. }
| Type::Record { .. }
| Type::Optional { .. } => quote!($(ty.as_codetype().ffi_converter_name()).lift($inner)),
| Type::Optional { .. } => {
quote!($(ty.as_codetype().ffi_converter_name()).lift($inner))
}
_ => todo!("lift Type::{:?}", ty),
}
}
Expand All @@ -193,7 +197,9 @@ impl DartCodeOracle {
| Type::Enum { .. }
| Type::Optional { .. }
| Type::Record { .. }
| Type::Sequence { .. } => quote!($(ty.as_codetype().ffi_converter_name()).lower($inner)),
| Type::Sequence { .. } => {
quote!($(ty.as_codetype().ffi_converter_name()).lower($inner))
}
_ => todo!("lower Type::{:?}", ty),
}
}
Expand Down Expand Up @@ -225,16 +231,71 @@ impl DartCodeOracle {
}
}


// https://dart.dev/guides/language/language-tour#keywords
pub static RESERVED_IDENTIFIERS: [&str; 63] = [
"abstract", "as", "assert", "async", "await", "break", "case", "catch", "class", "const",
"continue", "covariant", "default", "deferred", "do", "dynamic", "else", "enum", "export",
"extends", "extension", "external", "factory", "false", "final", "finally", "for", "Function",
"get", "hide", "if", "implements", "import", "in", "interface", "is", "late", "library",
"mixin", "new", "null", "on", "operator", "part", "required", "rethrow", "return", "set",
"show", "static", "super", "switch", "sync", "this", "throw", "true", "try", "typedef",
"var", "void", "while", "with", "yield",
"abstract",
"as",
"assert",
"async",
"await",
"break",
"case",
"catch",
"class",
"const",
"continue",
"covariant",
"default",
"deferred",
"do",
"dynamic",
"else",
"enum",
"export",
"extends",
"extension",
"external",
"factory",
"false",
"final",
"finally",
"for",
"Function",
"get",
"hide",
"if",
"implements",
"import",
"in",
"interface",
"is",
"late",
"library",
"mixin",
"new",
"null",
"on",
"operator",
"part",
"required",
"rethrow",
"return",
"set",
"show",
"static",
"super",
"switch",
"sync",
"this",
"throw",
"true",
"try",
"typedef",
"var",
"void",
"while",
"with",
"yield",
];

pub trait AsCodeType {
Expand Down Expand Up @@ -290,4 +351,3 @@ impl<T: AsType> AsCodeType for T {
}
}
}

1 change: 0 additions & 1 deletion src/gen/primitives/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,3 @@ impl Renderable for BooleanCodeType {
}
}
}

1 change: 0 additions & 1 deletion src/gen/primitives/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,3 @@ impl Renderable for DurationCodeType {
}
}
}

8 changes: 4 additions & 4 deletions src/gen/primitives/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[macro_use]
mod macros;
mod boolean;
mod string;
mod duration;
mod string;

use crate::gen::render::{Renderable, TypeHelperRenderer};
use genco::prelude::*;
Expand All @@ -11,8 +11,8 @@ use uniffi_bindgen::backend::Literal;
use uniffi_bindgen::interface::{Radix, Type};

pub use boolean::BooleanCodeType;
pub use string::StringCodeType;
pub use duration::DurationCodeType;
pub use string::StringCodeType;

fn render_literal(literal: &Literal) -> String {
fn typed_number(type_: &Type, num_str: String) -> String {
Expand All @@ -33,7 +33,7 @@ fn render_literal(literal: &Literal) -> String {

match literal {
Literal::Boolean(v) => format!("{}", v),
Literal::String(s) => format!("'{}'", s),
Literal::String(s) => format!("'{}'", s),
Literal::Int(i, radix, type_) => typed_number(
type_,
match radix {
Expand Down Expand Up @@ -78,4 +78,4 @@ impl_renderable_for_primitive!(UInt16CodeType, "int", "UInt16", 2);
impl_renderable_for_primitive!(UInt32CodeType, "int", "UInt32", 4);
impl_renderable_for_primitive!(UInt64CodeType, "int", "UInt64", 8);
impl_renderable_for_primitive!(Float32CodeType, "double", "Double32", 4);
impl_renderable_for_primitive!(Float64CodeType, "double", "Double64", 8);
impl_renderable_for_primitive!(Float64CodeType, "double", "Double64", 8);
1 change: 0 additions & 1 deletion src/gen/primitives/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,3 @@ impl Renderable for StringCodeType {
}
}
}

1 change: 0 additions & 1 deletion src/gen/records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,3 @@ pub fn generate_record(obj: &Record, type_helper: &dyn TypeHelperRenderer) -> da
}
}
}

22 changes: 17 additions & 5 deletions src/gen/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,28 @@ pub trait Renderable {

fn render_type(&self, ty: &Type, type_helper: &dyn TypeHelperRenderer) -> dart::Tokens {
let type_name = match ty {
Type::UInt8 | Type::Int8 | Type::UInt16 | Type::Int16 |
Type::UInt32 | Type::Int32 | Type::UInt64 | Type::Int64 => quote!(int),
Type::UInt8
| Type::Int8
| Type::UInt16
| Type::Int16
| Type::UInt32
| Type::Int32
| Type::UInt64
| Type::Int64 => quote!(int),
Type::Float32 | Type::Float64 => quote!(double),
Type::String => quote!(String),
Type::Boolean => quote!(bool),
Type::Object { name, .. } => quote!($name),
Type::Optional { inner_type } => quote!($(&self.render_type(inner_type, type_helper))?),
Type::Sequence { inner_type } => quote!(List<$(&self.render_type(inner_type, type_helper))>),
Type::Map { key_type, value_type } => quote!(Map<$(&self.render_type(key_type, type_helper)), $(&self.render_type(value_type, type_helper))>),
Type::Sequence { inner_type } => {
quote!(List<$(&self.render_type(inner_type, type_helper))>)
}
Type::Map {
key_type,
value_type,
} => {
quote!(Map<$(&self.render_type(key_type, type_helper)), $(&self.render_type(value_type, type_helper))>)
}
Type::Enum { name, .. } => quote!($name),
Type::Record { name, .. } => quote!($name),
Type::Duration => quote!(Duration),
Expand Down Expand Up @@ -90,4 +103,3 @@ impl<T: AsType> AsRenderable for T {
}
}
}

Loading

0 comments on commit 4f85c67

Please sign in to comment.