@@ -4,78 +4,81 @@ use anchor_lang_idl::types::{IdlArrayLen, IdlGenericArg, IdlType};
44use anyhow:: Result ;
55use std:: path:: { Path , PathBuf } ;
66
7+ use crate :: templates:: bundle:: create_bundle_template;
78use crate :: templates:: component:: create_component_template_simple;
89use crate :: templates:: program:: { create_program_template_multiple, create_program_template_single} ;
910use crate :: templates:: system:: create_system_template_simple;
10- use crate :: templates:: bundle:: create_bundle_template;
1111use crate :: templates:: workspace:: {
12- cargo_toml, cargo_toml_with_serde, workspace_manifest, xargo_toml,
12+ cargo_toml, cargo_toml_with_serde, workspace_manifest, xargo_toml,
1313} ;
1414
1515/// Create a component from the given name.
1616pub fn create_component ( name : & str ) -> Result < ( ) > {
17- let program_path = Path :: new ( "programs-ecs/components" ) . join ( name) ;
18- let common_files = vec ! [
19- (
20- PathBuf :: from( "Cargo.toml" . to_string( ) ) ,
21- workspace_manifest( ) . to_string( ) ,
22- ) ,
23- ( program_path. join( "Cargo.toml" ) , cargo_toml( name) ) ,
24- ( program_path. join( "Xargo.toml" ) , xargo_toml( ) . to_string( ) ) ,
25- ] as Files ;
17+ let program_path = Path :: new ( "programs-ecs/components" ) . join ( name) ;
18+ let common_files = vec ! [
19+ (
20+ PathBuf :: from( "Cargo.toml" . to_string( ) ) ,
21+ workspace_manifest( ) . to_string( ) ,
22+ ) ,
23+ ( program_path. join( "Cargo.toml" ) , cargo_toml( name) ) ,
24+ ( program_path. join( "Xargo.toml" ) , xargo_toml( ) . to_string( ) ) ,
25+ ] as Files ;
2626
27- let template_files = create_component_template_simple ( name, & program_path) ;
28- anchor_cli:: create_files ( & [ common_files, template_files] . concat ( ) )
27+ let template_files = create_component_template_simple ( name, & program_path) ;
28+ anchor_cli:: create_files ( & [ common_files, template_files] . concat ( ) )
2929}
3030
3131/// Create a bundle from a given name.
3232pub ( crate ) fn create_bundle ( name : & str ) -> Result < ( ) > {
33- let program_path = Path :: new ( "programs-ecs/bundles" ) . join ( name) ;
34- let common_files = vec ! [
35- ( PathBuf :: from( "Cargo.toml" . to_string( ) ) , workspace_manifest( ) . to_string( ) ) ,
36- ( program_path. join( "Cargo.toml" ) , cargo_toml_with_serde( name) ) ,
37- ( program_path. join( "Xargo.toml" ) , xargo_toml( ) . to_string( ) ) ,
38- ] as Files ;
33+ let program_path = Path :: new ( "programs-ecs/bundles" ) . join ( name) ;
34+ let common_files = vec ! [
35+ (
36+ PathBuf :: from( "Cargo.toml" . to_string( ) ) ,
37+ workspace_manifest( ) . to_string( ) ,
38+ ) ,
39+ ( program_path. join( "Cargo.toml" ) , cargo_toml_with_serde( name) ) ,
40+ ( program_path. join( "Xargo.toml" ) , xargo_toml( ) . to_string( ) ) ,
41+ ] as Files ;
3942
40- let template_files = create_bundle_template ( name, & program_path) ;
41- anchor_cli:: create_files ( & [ common_files, template_files] . concat ( ) )
43+ let template_files = create_bundle_template ( name, & program_path) ;
44+ anchor_cli:: create_files ( & [ common_files, template_files] . concat ( ) )
4245}
4346
4447/// Create a system from the given name.
4548pub ( crate ) fn create_system ( name : & str ) -> Result < ( ) > {
46- let program_path = Path :: new ( "programs-ecs/systems" ) . join ( name) ;
47- let common_files = vec ! [
48- (
49- PathBuf :: from( "Cargo.toml" . to_string( ) ) ,
50- workspace_manifest( ) . to_string( ) ,
51- ) ,
52- ( program_path. join( "Cargo.toml" ) , cargo_toml_with_serde( name) ) ,
53- ( program_path. join( "Xargo.toml" ) , xargo_toml( ) . to_string( ) ) ,
54- ] as Files ;
49+ let program_path = Path :: new ( "programs-ecs/systems" ) . join ( name) ;
50+ let common_files = vec ! [
51+ (
52+ PathBuf :: from( "Cargo.toml" . to_string( ) ) ,
53+ workspace_manifest( ) . to_string( ) ,
54+ ) ,
55+ ( program_path. join( "Cargo.toml" ) , cargo_toml_with_serde( name) ) ,
56+ ( program_path. join( "Xargo.toml" ) , xargo_toml( ) . to_string( ) ) ,
57+ ] as Files ;
5558
56- let template_files = create_system_template_simple ( name, & program_path) ;
57- anchor_cli:: create_files ( & [ common_files, template_files] . concat ( ) )
59+ let template_files = create_system_template_simple ( name, & program_path) ;
60+ anchor_cli:: create_files ( & [ common_files, template_files] . concat ( ) )
5861}
5962
6063/// Create an anchor program
6164pub fn create_program ( name : & str , template : ProgramTemplate ) -> Result < ( ) > {
62- let program_path = Path :: new ( "programs" ) . join ( name) ;
63- let common_files = vec ! [
64- ( "Cargo.toml" . into( ) , workspace_manifest( ) ) ,
65- ( program_path. join( "Cargo.toml" ) , cargo_toml( name) ) ,
66- ( program_path. join( "Xargo.toml" ) , xargo_toml( ) . into( ) ) ,
67- ] ;
65+ let program_path = Path :: new ( "programs" ) . join ( name) ;
66+ let common_files = vec ! [
67+ ( "Cargo.toml" . into( ) , workspace_manifest( ) ) ,
68+ ( program_path. join( "Cargo.toml" ) , cargo_toml( name) ) ,
69+ ( program_path. join( "Xargo.toml" ) , xargo_toml( ) . into( ) ) ,
70+ ] ;
6871
69- let template_files = match template {
70- ProgramTemplate :: Single => create_program_template_single ( name, & program_path) ,
71- ProgramTemplate :: Multiple => create_program_template_multiple ( name, & program_path) ,
72- } ;
72+ let template_files = match template {
73+ ProgramTemplate :: Single => create_program_template_single ( name, & program_path) ,
74+ ProgramTemplate :: Multiple => create_program_template_multiple ( name, & program_path) ,
75+ } ;
7376
74- create_files ( & [ common_files, template_files] . concat ( ) )
77+ create_files ( & [ common_files, template_files] . concat ( ) )
7578}
7679
7780pub fn registry_account ( ) -> & ' static str {
78- r#"
81+ r#"
7982{
8083 "pubkey": "EHLkWwAT9oebVv9ht3mtqrvHhRVMKrt54tF3MfHTey2K",
8184 "account": {
@@ -95,51 +98,51 @@ pub fn registry_account() -> &'static str {
9598
9699/// Map Idl type to rust type
97100pub fn convert_idl_type_to_str ( ty : & IdlType ) -> String {
98- match ty {
99- IdlType :: Bool => "bool" . into ( ) ,
100- IdlType :: U8 => "u8" . into ( ) ,
101- IdlType :: I8 => "i8" . into ( ) ,
102- IdlType :: U16 => "u16" . into ( ) ,
103- IdlType :: I16 => "i16" . into ( ) ,
104- IdlType :: U32 => "u32" . into ( ) ,
105- IdlType :: I32 => "i32" . into ( ) ,
106- IdlType :: F32 => "f32" . into ( ) ,
107- IdlType :: U64 => "u64" . into ( ) ,
108- IdlType :: I64 => "i64" . into ( ) ,
109- IdlType :: F64 => "f64" . into ( ) ,
110- IdlType :: U128 => "u128" . into ( ) ,
111- IdlType :: I128 => "i128" . into ( ) ,
112- IdlType :: U256 => "u256" . into ( ) ,
113- IdlType :: I256 => "i256" . into ( ) ,
114- IdlType :: Bytes => "bytes" . into ( ) ,
115- IdlType :: String => "String" . into ( ) ,
116- IdlType :: Pubkey => "Pubkey" . into ( ) ,
117- IdlType :: Option ( ty) => format ! ( "Option<{}>" , convert_idl_type_to_str( ty) ) ,
118- IdlType :: Vec ( ty) => format ! ( "Vec<{}>" , convert_idl_type_to_str( ty) ) ,
119- IdlType :: Array ( ty, len) => format ! (
120- "[{}; {}]" ,
121- convert_idl_type_to_str( ty) ,
122- match len {
123- IdlArrayLen :: Generic ( len) => len. into( ) ,
124- IdlArrayLen :: Value ( len) => len. to_string( ) ,
125- }
126- ) ,
127- IdlType :: Defined { name, generics } => generics
128- . iter ( )
129- . map ( |generic| match generic {
130- IdlGenericArg :: Type { ty } => convert_idl_type_to_str ( ty) ,
131- IdlGenericArg :: Const { value } => value. into ( ) ,
132- } )
133- . reduce ( |mut acc, cur| {
134- if !acc. is_empty ( ) {
135- acc. push ( ',' ) ;
136- }
137- acc. push_str ( & cur) ;
138- acc
139- } )
140- . map ( |generics| format ! ( "{name}<{generics}>" ) )
141- . unwrap_or ( name. into ( ) ) ,
142- IdlType :: Generic ( ty) => ty. into ( ) ,
143- _ => unimplemented ! ( "{ty:?}" ) ,
144- }
101+ match ty {
102+ IdlType :: Bool => "bool" . into ( ) ,
103+ IdlType :: U8 => "u8" . into ( ) ,
104+ IdlType :: I8 => "i8" . into ( ) ,
105+ IdlType :: U16 => "u16" . into ( ) ,
106+ IdlType :: I16 => "i16" . into ( ) ,
107+ IdlType :: U32 => "u32" . into ( ) ,
108+ IdlType :: I32 => "i32" . into ( ) ,
109+ IdlType :: F32 => "f32" . into ( ) ,
110+ IdlType :: U64 => "u64" . into ( ) ,
111+ IdlType :: I64 => "i64" . into ( ) ,
112+ IdlType :: F64 => "f64" . into ( ) ,
113+ IdlType :: U128 => "u128" . into ( ) ,
114+ IdlType :: I128 => "i128" . into ( ) ,
115+ IdlType :: U256 => "u256" . into ( ) ,
116+ IdlType :: I256 => "i256" . into ( ) ,
117+ IdlType :: Bytes => "bytes" . into ( ) ,
118+ IdlType :: String => "String" . into ( ) ,
119+ IdlType :: Pubkey => "Pubkey" . into ( ) ,
120+ IdlType :: Option ( ty) => format ! ( "Option<{}>" , convert_idl_type_to_str( ty) ) ,
121+ IdlType :: Vec ( ty) => format ! ( "Vec<{}>" , convert_idl_type_to_str( ty) ) ,
122+ IdlType :: Array ( ty, len) => format ! (
123+ "[{}; {}]" ,
124+ convert_idl_type_to_str( ty) ,
125+ match len {
126+ IdlArrayLen :: Generic ( len) => len. into( ) ,
127+ IdlArrayLen :: Value ( len) => len. to_string( ) ,
128+ }
129+ ) ,
130+ IdlType :: Defined { name, generics } => generics
131+ . iter ( )
132+ . map ( |generic| match generic {
133+ IdlGenericArg :: Type { ty } => convert_idl_type_to_str ( ty) ,
134+ IdlGenericArg :: Const { value } => value. into ( ) ,
135+ } )
136+ . reduce ( |mut acc, cur| {
137+ if !acc. is_empty ( ) {
138+ acc. push ( ',' ) ;
139+ }
140+ acc. push_str ( & cur) ;
141+ acc
142+ } )
143+ . map ( |generics| format ! ( "{name}<{generics}>" ) )
144+ . unwrap_or ( name. into ( ) ) ,
145+ IdlType :: Generic ( ty) => ty. into ( ) ,
146+ _ => unimplemented ! ( "{ty:?}" ) ,
147+ }
145148}
0 commit comments