-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: more key/val hash_map types for field access (#24)
Added a few more types that HashMap field access now supports. Namely key + val can now be different types (before that wasn't properly implemented and the key type applied to the value as well). The String type is now supported for keys (not yet for values). rid::model structs are now supported as value types. * test: testing hash_map key/val int primitives * rid-macro: parsing hash_map includes key + val types - previously the key type was used for both key and value * rid-macro: handles mixed primitive key/val types for hash_maps * rid-macro: extracting collection access tokens from vec access method - needed to make it easier useable for hash maps - named collection_access_tokens for lack of better idea of what to name it for now * rid-macro: merged render vec access into VecAccess impl module * rid-macro: rename collection item access tokens * rid-macro: rid::export for hash_map contains_key * rid-macro: rid::export to get hash_map item * rid-macro: rid::export for hash_map len * rid-macro: generalized render resolved dart ffi arg - to ease reuse when we specify the arg by name instead via slot * rid-macro: including proper key arg conversion for hash_map * rid-macro: hash_map access fns include structs/enums info with rendered export * rid-macro: pulled collection item conversion out of vec module * rid-macro: HashMap<String, struct> implementation * test: adding tests for hash_map with string key
- Loading branch information
Showing
15 changed files
with
620 additions
and
306 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
rid-macro-impl/src/accesses/collection_item_access_tokens.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::Ident; | ||
|
||
use crate::{ | ||
parse::rust_type::RustType, | ||
render_common::PointerTypeAlias, | ||
render_rust::{render_return_type, RenderedReturnType}, | ||
}; | ||
|
||
use super::AccessKind; | ||
|
||
pub struct CollectionItemAccessTokens { | ||
/// The tokens for the type that should be returned by the get item wrapper method | ||
pub item_return_type: TokenStream, | ||
|
||
/// The tokens to convert the type of the wrapped method into the one the wrapper returns | ||
pub into_return_type: TokenStream, | ||
|
||
/// Type aliases used as a part of the return type | ||
pub type_alias: Option<PointerTypeAlias>, | ||
} | ||
|
||
/// Helper function to figure out the return type and into return type tokens for | ||
/// a specific collection type. Used for `Vec` get by index and `HashMap` get by key. | ||
pub fn collection_item_access_tokens( | ||
ptr_ident: Ident, | ||
item_type: &RustType, | ||
access_kind: &AccessKind, | ||
) -> CollectionItemAccessTokens { | ||
// CString and str aren't FFI safe so we send the item content as a *char. | ||
// For consistency we do the same for Strings. | ||
let (item_return_type, into_return_type, type_alias) = if item_type | ||
.is_string_like() | ||
{ | ||
let item_return_type = quote! { *const ::std::os::raw::c_char }; | ||
let into_return_type = if item_type.is_string() { | ||
quote! { | ||
let s: &String = unsafe { | ||
assert!(!#ptr_ident.is_null()); | ||
let #ptr_ident: *mut String = #ptr_ident as *mut String; | ||
#ptr_ident.as_mut().expect("resolve ptr from collection item as_mut failed") | ||
}; | ||
let cstring = ::std::ffi::CString::new(s.as_str()).unwrap(); | ||
cstring.into_raw() | ||
} | ||
} else if item_type.is_cstring() { | ||
quote! { | ||
let cstring: &::std::ffi::CString = unsafe { | ||
assert!(!#ptr_ident.is_null()); | ||
let #ptr_ident: *mut ::std::ffi::CString = #ptr_ident as *mut ::std::ffi::CString; | ||
#ptr_ident.as_mut().expect("resolve_ptr.as_mut failed") | ||
}; | ||
cstring.clone().into_raw() | ||
} | ||
} else if item_type.is_str() { | ||
quote! { | ||
let s: &str = unsafe { | ||
assert!(!#ptr_ident.is_null()); | ||
let #ptr_ident: *mut str = #ptr_ident as *mut str; | ||
#ptr_ident.as_mut() | ||
.expect("resolve ptr from collection item as_mut failed") | ||
}; | ||
let cstring = ::std::ffi::CString::new(s).unwrap(); | ||
cstring.into_raw() | ||
} | ||
} else { | ||
panic!("Should have covered all string-like cases") | ||
}; | ||
(item_return_type, into_return_type, None) | ||
} else { | ||
let RenderedReturnType { | ||
tokens, type_alias, .. | ||
} = render_return_type(&item_type, access_kind); | ||
(tokens, quote! { #ptr_ident }, type_alias) | ||
}; | ||
CollectionItemAccessTokens { | ||
item_return_type, | ||
into_return_type, | ||
type_alias, | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
rid-macro-impl/src/accesses/collection_item_conversions.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use crate::{ | ||
attrs::TypeInfoMap, | ||
parse::{dart_type::DartType, rust_type::RustType}, | ||
}; | ||
|
||
pub fn resolved_dart_item_type_string( | ||
item_type: &RustType, | ||
type_infos: &TypeInfoMap, | ||
) -> String { | ||
if item_type.is_struct() { | ||
item_type.rust_ident().to_string() | ||
} else if item_type.is_string_like() { | ||
"String".to_string() | ||
} else { | ||
DartType::from(&item_type, type_infos).render_type(false) | ||
} | ||
} | ||
|
||
pub fn map_to_dart_string( | ||
item_type: &RustType, | ||
dart_item_type: &str, | ||
) -> String { | ||
if item_type.is_struct() { | ||
format!(".map((raw) => raw.toDart())") | ||
} else if item_type.is_enum() { | ||
format!( | ||
".map((x) => {enum_type}.values[x])", | ||
enum_type = dart_item_type | ||
) | ||
} else { | ||
"".to_string() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
mod access; | ||
mod collection_item_access_tokens; | ||
mod collection_item_conversions; | ||
mod hash_map; | ||
mod render_collection_accesses; | ||
mod vec; | ||
|
||
pub use access::*; | ||
pub use collection_item_access_tokens::*; | ||
pub use collection_item_conversions::*; | ||
pub use hash_map::*; | ||
pub use render_collection_accesses::*; | ||
pub use vec::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 8 additions & 8 deletions
16
rid-macro-impl/src/render_dart/hash_map/hash_map_field_access.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.