Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove IDL path field #2564

Merged
merged 2 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use flate2::read::ZlibDecoder;
use flate2::write::{GzEncoder, ZlibEncoder};
use flate2::Compression;
use heck::{ToKebabCase, ToSnakeCase};
use regex::RegexBuilder;
use regex::{Regex, RegexBuilder};
use reqwest::blocking::multipart::{Form, Part};
use reqwest::blocking::Client;
use semver::{Version, VersionReq};
Expand Down Expand Up @@ -2328,8 +2328,7 @@ fn idl_build(no_docs: bool) -> Result<()> {
}

let prog_ty = std::mem::take(&mut idl.types);
defined_types
.extend(prog_ty.into_iter().map(|ty| (ty.path.clone().unwrap(), ty)));
defined_types.extend(prog_ty.into_iter().map(|ty| (ty.name.clone(), ty)));
idl.types = defined_types.into_values().collect::<Vec<_>>();

idls.push(idl);
Expand All @@ -2353,7 +2352,7 @@ fn idl_build(no_docs: bool) -> Result<()> {
event
.defined_types
.into_iter()
.map(|ty| (ty.path.clone().unwrap(), ty)),
.map(|ty| (ty.name.clone(), ty)),
);
state = State::Pass;
continue;
Expand Down Expand Up @@ -2381,13 +2380,37 @@ fn idl_build(no_docs: bool) -> Result<()> {
}
}

// Convert path to name if there are no conflicts
let path_regex = Regex::new(r#""((\w+::)+)(\w+)""#).unwrap();
let idls = idls
.into_iter()
.map(|idl| {
let mut modified_content = serde_json::to_string_pretty(&idl).unwrap();

// TODO: Remove. False positive https://github.com/rust-lang/rust-clippy/issues/10577
#[allow(clippy::redundant_clone)]
for captures in path_regex.captures_iter(&modified_content.clone()) {
let path = captures.get(0).unwrap().as_str();
let name = captures.get(3).unwrap().as_str();

// Replace path with name
let content_with_name = modified_content.replace(path, &format!(r#""{name}""#));

// Check whether there is a conflict
let has_conflict = content_with_name.contains(&format!("::{name}"));
if !has_conflict {
modified_content = content_with_name;
}
}

modified_content
})
.collect::<Vec<_>>();

if idls.len() == 1 {
println!(
"{}",
serde_json::to_string_pretty(&idls.first().unwrap()).unwrap()
);
} else if idls.len() >= 2 {
println!("{}", serde_json::to_string_pretty(&idls).unwrap());
println!("{}", idls[0]);
} else {
println!("{:?}", idls);
};

Ok(())
Expand Down Expand Up @@ -2670,7 +2693,7 @@ fn deserialize_idl_type_to_json(
}
IdlType::GenericLenArray(_, _) => todo!("Generic length arrays are not yet supported"),
IdlType::Generic(_) => todo!("Generic types are not yet supported"),
IdlType::DefinedWithTypeArgs { path: _, args: _ } => {
IdlType::DefinedWithTypeArgs { name: _, args: _ } => {
todo!("Defined types with type args are not yet supported")
}
})
Expand Down
10 changes: 3 additions & 7 deletions lang/syn/src/idl/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub fn idl_type_ts_from_syn_type(
let params = quote! { vec![#(#params),*] };
Ok((
quote! { #idl::IdlType::DefinedWithTypeArgs {
path: <#path>::__anchor_private_full_path(),
name: <#path>::__anchor_private_full_path(),
args: #params
} },
defined,
Expand Down Expand Up @@ -283,7 +283,6 @@ pub fn idl_type_definition_ts_from_syn_struct(
) -> Result<(TokenStream, Vec<syn::TypePath>), ()> {
let (idl, _) = get_module_paths();

let name = item_strct.ident.to_string();
let docs = match docs::parse(&item_strct.attrs) {
Some(docs) if !no_docs => quote! {Some(vec![#(#docs.into()),*])},
_ => quote! {None},
Expand Down Expand Up @@ -324,8 +323,7 @@ pub fn idl_type_definition_ts_from_syn_struct(
Ok((
quote! {
#idl::IdlTypeDefinition {
name: #name.into(),
path: Some(Self::__anchor_private_full_path()),
name: Self::__anchor_private_full_path(),
generics: #generics,
docs: #docs,
ty: #idl::IdlTypeDefinitionTy::Struct{
Expand All @@ -349,7 +347,6 @@ pub fn idl_type_definition_ts_from_syn_enum(
) -> Result<(TokenStream, Vec<syn::TypePath>), ()> {
let (idl, _) = get_module_paths();

let name = enum_item.ident.to_string();
let docs = match docs::parse(&enum_item.attrs) {
Some(docs) if !no_docs => quote! {Some(vec![#(#docs.into()),*])},
_ => quote! {None},
Expand Down Expand Up @@ -421,8 +418,7 @@ pub fn idl_type_definition_ts_from_syn_enum(
Ok((
quote! {
#idl::IdlTypeDefinition {
name: #name.into(),
path: Some(Self::__anchor_private_full_path()),
name: Self::__anchor_private_full_path(),
generics: #generics,
docs: #docs,
ty: #idl::IdlTypeDefinitionTy::Enum{
Expand Down
2 changes: 0 additions & 2 deletions lang/syn/src/idl/parse/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ fn parse_ty_defs(ctx: &CrateContext, no_docs: bool) -> Result<Vec<IdlTypeDefinit

Some(fields.map(|fields| IdlTypeDefinition {
name,
path: None,
generics: None,
docs: doc,
ty: IdlTypeDefinitionTy::Struct { fields },
Expand Down Expand Up @@ -408,7 +407,6 @@ fn parse_ty_defs(ctx: &CrateContext, no_docs: bool) -> Result<Vec<IdlTypeDefinit
.collect::<Vec<IdlEnumVariant>>();
Some(Ok(IdlTypeDefinition {
name,
path: None,
generics: None,
docs: doc,
ty: IdlTypeDefinitionTy::Enum { variants },
Expand Down
11 changes: 7 additions & 4 deletions lang/syn/src/idl/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,16 @@ pub struct IdlEventField {

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlTypeDefinition {
/// - `idl-parse`: always the name of the type
/// - `idl-build`: full path if there is a name conflict, otherwise the name of the type
pub name: String,
/// Documentation comments
#[serde(skip_serializing_if = "Option::is_none")]
pub path: Option<String>,
pub docs: Option<Vec<String>>,
/// Generics, only supported with `idl-build`
#[serde(skip_serializing_if = "Option::is_none")]
pub generics: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub docs: Option<Vec<String>>,
/// Type definition, `struct` or `enum`
#[serde(rename = "type")]
pub ty: IdlTypeDefinitionTy,
}
Expand Down Expand Up @@ -207,7 +210,7 @@ pub enum IdlType {
GenericLenArray(Box<IdlType>, String),
Generic(String),
DefinedWithTypeArgs {
path: String,
name: String,
args: Vec<IdlDefinedTypeArg>,
},
}
Expand Down
35 changes: 15 additions & 20 deletions tests/idl-build/tests/testdata/generics_build_exp.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"name": "genericField",
"type": {
"definedWithTypeArgs": {
"path": "generics::GenericType",
"name": "GenericType",
"args": [
{
"type": "u32"
Expand All @@ -50,15 +50,14 @@
"accounts": [
{
"name": "GenericAccount",
"path": "generics::GenericAccount",
"type": {
"kind": "struct",
"fields": [
{
"name": "data",
"type": {
"definedWithTypeArgs": {
"path": "generics::GenericType",
"name": "GenericType",
"args": [
{
"type": "u32"
Expand All @@ -80,7 +79,6 @@
"types": [
{
"name": "GenericEnum",
"path": "generics::GenericEnum",
"generics": [
"T",
"U",
Expand Down Expand Up @@ -122,7 +120,7 @@
"fields": [
{
"definedWithTypeArgs": {
"path": "generics::GenericNested",
"name": "GenericNested",
"args": [
{
"type": {
Expand Down Expand Up @@ -157,7 +155,6 @@
},
{
"name": "GenericNested",
"path": "generics::GenericNested",
"generics": [
"V",
"Z"
Expand All @@ -182,7 +179,6 @@
},
{
"name": "GenericType",
"path": "generics::GenericType",
"generics": [
"T",
"U",
Expand All @@ -207,7 +203,7 @@
"name": "gen3",
"type": {
"definedWithTypeArgs": {
"path": "generics::GenericNested",
"name": "GenericNested",
"args": [
{
"type": "u32"
Expand All @@ -225,7 +221,7 @@
"name": "gen4",
"type": {
"definedWithTypeArgs": {
"path": "generics::GenericNested",
"name": "GenericNested",
"args": [
{
"type": {
Expand All @@ -234,7 +230,7 @@
},
{
"type": {
"defined": "some_external_program::Baz"
"defined": "Baz"
}
}
]
Expand All @@ -245,7 +241,7 @@
"name": "gen5",
"type": {
"definedWithTypeArgs": {
"path": "generics::GenericNested",
"name": "GenericNested",
"args": [
{
"type": {
Expand All @@ -265,7 +261,7 @@
"name": "gen6",
"type": {
"definedWithTypeArgs": {
"path": "generics::GenericNested",
"name": "GenericNested",
"args": [
{
"type": "u32"
Expand All @@ -281,7 +277,7 @@
"name": "gen7",
"type": {
"definedWithTypeArgs": {
"path": "generics::GenericNested",
"name": "GenericNested",
"args": [
{
"type": {
Expand All @@ -291,7 +287,7 @@
{
"type": {
"definedWithTypeArgs": {
"path": "generics::GenericNested",
"name": "GenericNested",
"args": [
{
"type": {
Expand Down Expand Up @@ -324,7 +320,7 @@
"name": "warr",
"type": {
"definedWithTypeArgs": {
"path": "generics::WrappedU8Array",
"name": "WrappedU8Array",
"args": [
{
"type": {
Expand All @@ -339,7 +335,7 @@
"name": "warrval",
"type": {
"definedWithTypeArgs": {
"path": "generics::WrappedU8Array",
"name": "WrappedU8Array",
"args": [
{
"value": "10"
Expand All @@ -352,7 +348,7 @@
"name": "enm1",
"type": {
"definedWithTypeArgs": {
"path": "generics::GenericEnum",
"name": "GenericEnum",
"args": [
{
"type": {
Expand All @@ -377,12 +373,12 @@
"name": "enm2",
"type": {
"definedWithTypeArgs": {
"path": "generics::GenericEnum",
"name": "GenericEnum",
"args": [
{
"type": {
"definedWithTypeArgs": {
"path": "generics::GenericNested",
"name": "GenericNested",
"args": [
{
"type": {
Expand Down Expand Up @@ -411,7 +407,6 @@
},
{
"name": "Baz",
"path": "some_external_program::Baz",
"type": {
"kind": "struct",
"fields": [
Expand Down
Loading
Loading