From 85a7e47aaa9e8d24f47d95472ebdc8f25836d1d6 Mon Sep 17 00:00:00 2001 From: Abhishek Shah Date: Sat, 9 Mar 2024 14:11:55 +0530 Subject: [PATCH] default to use insert_str_runtime for implicit runtime decl entries. --- src/engines/pallet_engine/mod.rs | 63 +++++++++++++++--------------- src/engines/pallet_engine/steps.rs | 4 +- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/src/engines/pallet_engine/mod.rs b/src/engines/pallet_engine/mod.rs index d7d5ad69..42136731 100644 --- a/src/engines/pallet_engine/mod.rs +++ b/src/engines/pallet_engine/mod.rs @@ -26,6 +26,7 @@ use pallet_entry::Numbers; use pallet_entry::{AddPalletEntry, ReadPalletEntry}; use parser::RuntimeDeclaration; use proc_macro2::TokenStream; +use quote::quote; use std::{ collections::HashMap, fs::{self, File, OpenOptions}, @@ -78,7 +79,6 @@ pub struct PalletEngine { state: State, /// Cursor for tracking where we are in the output cursor: usize, - } impl Drop for PalletEngine { fn drop(&mut self) { @@ -180,9 +180,8 @@ impl PalletEngine { _ => {}, }; } - let imports = ImportDetails { - last_import: last_import.expect("Imports are always present"), - }; + let imports = + ImportDetails { last_import: last_import.expect("Imports are always present") }; let Some(details) = details else { bail!("No pallets/construct_runtime! found in input"); }; @@ -328,7 +327,7 @@ impl PalletEngine { /// Insert configuartion for a pallet - only for pallet-template atm fn insert_config(&mut self, config: (TokenStream, usize)) -> anyhow::Result<()> { self.append_tokens(config.0); - self.cursor += config.1; + self.cursor += config.1; Ok(()) } /// Append lines [start..end] from `input` source to `output`. @@ -449,32 +448,34 @@ impl PalletEngine { /// Overwrites existing CRT in output, this means that `self.cursor` has to backtracked to crt_start /// and updated after the pallet entry has been inserted fn add_pallet_runtime(&mut self, new_pallet: AddPalletEntry) -> anyhow::Result<()> { - use std::io::{Seek, SeekFrom}; - // re-render output CRT: - self.insert_at(self.cursor, "//---- ReRender from add_pallet_runtime ----\n")?; - println!("cursor -> {}", self.cursor); - match &mut self.details.declaration { - RuntimeDeclaration::Implicit(i) => { - let mut ultimate = i - .pallets - .last() - .ok_or(anyhow!("Fatal: No pallets defined in construct_runtime!"))? - .clone(); - ultimate.index = new_pallet.index; - ultimate.path.inner.segments[0].ident = new_pallet.path; - ultimate.name = new_pallet.name; - i.pallets.push(ultimate); - }, - RuntimeDeclaration::Explicit(e) => { - todo!() - }, - RuntimeDeclaration::ExplicitExpanded(e) => { - todo!() - }, - }; - // new_lines will tell cursor to update accounting for the new pallet - // self.render_pallets(new_lines)?; + let mut entry = String::new(); + let AddPalletEntry { name, path, index } = new_pallet; + if let Some(idx) = index { + entry = format!("\t\t{}: {} = {},", name, path, idx); + } else { + entry = format!("\t\t{}: {},", name, path); + } + self.insert_str_runtime(&entry)?; + // match &mut self.details.declaration { + // RuntimeDeclaration::Implicit(i) => { + // let mut ultimate = i + // .pallets + // .last() + // .ok_or(anyhow!("Fatal: No pallets defined in construct_runtime!"))? + // .clone(); + // ultimate.index = new_pallet.index; + // ultimate.path.inner.segments[0].ident = new_pallet.path; + // ultimate.name = new_pallet.name; + // i.pallets.push(ultimate); + // }, + // RuntimeDeclaration::Explicit(e) => { + // todo!() + // }, + // RuntimeDeclaration::ExplicitExpanded(e) => { + // todo!() + // }, + // }; Ok(()) } } -// TODO \ No newline at end of file +// TODO diff --git a/src/engines/pallet_engine/steps.rs b/src/engines/pallet_engine/steps.rs index 8525e3ee..cb4183c5 100644 --- a/src/engines/pallet_engine/steps.rs +++ b/src/engines/pallet_engine/steps.rs @@ -61,7 +61,7 @@ pub(super) fn step_builder(pallet: AddPallet) -> Result> { ))); steps.push(SwitchTo(State::ConstructRuntime)); steps.push(ConstructRuntimeEntry(AddPalletEntry::new( - // Index + // Index - None, means Pallet Engine will automatically compute an index None, // Path "pallet_parachain_template", @@ -114,9 +114,7 @@ pub(super) fn run_steps(mut pe: PalletEngine, steps: Vec) -> Result<()> { }, SwitchTo(State::ConstructRuntime) => pe.prepare_crt()?, ConstructRuntimeEntry(entry) => { - // TODO : Switch to add_pallet_runtime pe.add_pallet_runtime(entry)? - // pe.insert_str_runtime("\t\tTemplate: pallet_parachain_template = 100,")?; }, // ListBenchmarks(step) => pe.insert(step), // ListBenchmarks(step) => pe.insert(step),