rust_template.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. use anchor_cli::rust_template::ProgramTemplate;
  2. use anchor_cli::{create_files, Files};
  3. use anchor_lang_idl::types::{IdlArrayLen, IdlGenericArg, IdlType};
  4. use anyhow::Result;
  5. use std::path::{Path, PathBuf};
  6. use crate::templates::component::create_component_template_simple;
  7. use crate::templates::program::{create_program_template_multiple, create_program_template_single};
  8. use crate::templates::system::create_system_template_simple;
  9. use crate::templates::workspace::{
  10. cargo_toml, cargo_toml_with_serde, workspace_manifest, xargo_toml,
  11. };
  12. /// Create a component from the given name.
  13. pub fn create_component(name: &str) -> Result<()> {
  14. let program_path = Path::new("programs-ecs/components").join(name);
  15. let common_files = vec![
  16. (
  17. PathBuf::from("Cargo.toml".to_string()),
  18. workspace_manifest().to_string(),
  19. ),
  20. (program_path.join("Cargo.toml"), cargo_toml(name)),
  21. (program_path.join("Xargo.toml"), xargo_toml().to_string()),
  22. ] as Files;
  23. let template_files = create_component_template_simple(name, &program_path);
  24. anchor_cli::create_files(&[common_files, template_files].concat())
  25. }
  26. /// Create a system from the given name.
  27. pub(crate) fn create_system(name: &str) -> Result<()> {
  28. let program_path = Path::new("programs-ecs/systems").join(name);
  29. let common_files = vec![
  30. (
  31. PathBuf::from("Cargo.toml".to_string()),
  32. workspace_manifest().to_string(),
  33. ),
  34. (program_path.join("Cargo.toml"), cargo_toml_with_serde(name)),
  35. (program_path.join("Xargo.toml"), xargo_toml().to_string()),
  36. ] as Files;
  37. let template_files = create_system_template_simple(name, &program_path);
  38. anchor_cli::create_files(&[common_files, template_files].concat())
  39. }
  40. /// Create an anchor program
  41. pub fn create_program(name: &str, template: ProgramTemplate) -> Result<()> {
  42. let program_path = Path::new("programs").join(name);
  43. let common_files = vec![
  44. ("Cargo.toml".into(), workspace_manifest()),
  45. (program_path.join("Cargo.toml"), cargo_toml(name)),
  46. (program_path.join("Xargo.toml"), xargo_toml().into()),
  47. ];
  48. let template_files = match template {
  49. ProgramTemplate::Single => create_program_template_single(name, &program_path),
  50. ProgramTemplate::Multiple => create_program_template_multiple(name, &program_path),
  51. };
  52. create_files(&[common_files, template_files].concat())
  53. }
  54. pub fn registry_account() -> &'static str {
  55. r#"
  56. {
  57. "pubkey": "EHLkWwAT9oebVv9ht3mtqrvHhRVMKrt54tF3MfHTey2K",
  58. "account": {
  59. "lamports": 1002240,
  60. "data": [
  61. "L65u9ri2/NoCAAAAAAAAAA==",
  62. "base64"
  63. ],
  64. "owner": "WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n",
  65. "executable": false,
  66. "rentEpoch": 18446744073709551615,
  67. "space": 16
  68. }
  69. }
  70. "#
  71. }
  72. /// Map Idl type to rust type
  73. pub fn convert_idl_type_to_str(ty: &IdlType) -> String {
  74. match ty {
  75. IdlType::Bool => "bool".into(),
  76. IdlType::U8 => "u8".into(),
  77. IdlType::I8 => "i8".into(),
  78. IdlType::U16 => "u16".into(),
  79. IdlType::I16 => "i16".into(),
  80. IdlType::U32 => "u32".into(),
  81. IdlType::I32 => "i32".into(),
  82. IdlType::F32 => "f32".into(),
  83. IdlType::U64 => "u64".into(),
  84. IdlType::I64 => "i64".into(),
  85. IdlType::F64 => "f64".into(),
  86. IdlType::U128 => "u128".into(),
  87. IdlType::I128 => "i128".into(),
  88. IdlType::U256 => "u256".into(),
  89. IdlType::I256 => "i256".into(),
  90. IdlType::Bytes => "bytes".into(),
  91. IdlType::String => "String".into(),
  92. IdlType::Pubkey => "Pubkey".into(),
  93. IdlType::Option(ty) => format!("Option<{}>", convert_idl_type_to_str(ty)),
  94. IdlType::Vec(ty) => format!("Vec<{}>", convert_idl_type_to_str(ty)),
  95. IdlType::Array(ty, len) => format!(
  96. "[{}; {}]",
  97. convert_idl_type_to_str(ty),
  98. match len {
  99. IdlArrayLen::Generic(len) => len.into(),
  100. IdlArrayLen::Value(len) => len.to_string(),
  101. }
  102. ),
  103. IdlType::Defined { name, generics } => generics
  104. .iter()
  105. .map(|generic| match generic {
  106. IdlGenericArg::Type { ty } => convert_idl_type_to_str(ty),
  107. IdlGenericArg::Const { value } => value.into(),
  108. })
  109. .reduce(|mut acc, cur| {
  110. if !acc.is_empty() {
  111. acc.push(',');
  112. }
  113. acc.push_str(&cur);
  114. acc
  115. })
  116. .map(|generics| format!("{name}<{generics}>"))
  117. .unwrap_or(name.into()),
  118. IdlType::Generic(ty) => ty.into(),
  119. _ => unimplemented!("{ty:?}"),
  120. }
  121. }