From 52b39b15988824c0dca7035fea4d92feb49e49c8 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 24 Sep 2022 13:30:50 +0200 Subject: [PATCH] Cleanup code and fix clippy warnings Signed-off-by: trivernis --- src/scripting/executor.rs | 5 +++++ src/scripting/record_serializer.rs | 26 +++++++++----------------- src/scripting/script.rs | 18 ++++++++++++++++++ src/tasks/setup_users.rs | 15 +++++---------- 4 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/scripting/executor.rs b/src/scripting/executor.rs index 0565b3e..e2bfb82 100644 --- a/src/scripting/executor.rs +++ b/src/scripting/executor.rs @@ -90,6 +90,7 @@ impl NuExecutor { add_variables_to_state(vars, &mut engine_state, &mut stack); let (block, main_id) = read_script_file(&self.script_path, &mut engine_state).await?; + // put everything the script defines into scope nu_engine::eval_block( &engine_state, &mut stack, @@ -104,6 +105,7 @@ impl NuExecutor { // block in a different thread to be able to execute scripts in parallel tokio::task::spawn_blocking(move || { + // create a call to the main method wit the given arguments and execute it let call_block = create_call(main_id, args); nu_engine::eval_block( @@ -169,6 +171,7 @@ async fn read_script_file( } } +/// Parses a nu script fn parse_nu<'a>( engine_state: &'a mut EngineState, script: &[u8], @@ -184,6 +187,8 @@ fn parse_nu<'a>( } } +/// Creates a call nu expression with the given main block declaration ID +/// and arguments in the form of record values fn create_call(decl_id: DeclId, args: Vec) -> Block { let args = args .into_iter() diff --git a/src/scripting/record_serializer.rs b/src/scripting/record_serializer.rs index 38b10fc..69645a1 100644 --- a/src/scripting/record_serializer.rs +++ b/src/scripting/record_serializer.rs @@ -9,10 +9,14 @@ pub struct RecordSerializer { list_serializer: Option, map_serializer: Option, } + +#[derive(Default)] pub struct RecordListSerializer { serializer: Option>, entries: Vec, } + +#[derive(Default)] pub struct RecordMapSerializer { serializer: Option>, last_key: Option, @@ -20,7 +24,7 @@ pub struct RecordMapSerializer { } impl RecordSerializer { - pub fn list_serializer<'a>(&'a mut self, cap: Option) -> &'a mut RecordListSerializer { + pub fn list_serializer(&mut self, cap: Option) -> &mut RecordListSerializer { if self.list_serializer.is_none() { self.list_serializer = Some(RecordListSerializer::with_capacity_opt(cap)); } @@ -36,13 +40,6 @@ impl RecordSerializer { } impl RecordListSerializer { - pub fn new() -> Self { - Self { - entries: Vec::new(), - serializer: None, - } - } - pub fn with_capacity(capacity: usize) -> Self { Self { entries: Vec::with_capacity(capacity), @@ -53,7 +50,7 @@ impl RecordListSerializer { pub fn with_capacity_opt(capacity: Option) -> Self { match capacity { Some(cap) => Self::with_capacity(cap), - None => Self::new(), + None => Self::default(), } } @@ -66,13 +63,6 @@ impl RecordListSerializer { } impl RecordMapSerializer { - pub fn new() -> Self { - Self { - last_key: None, - entries: Vec::new(), - serializer: None, - } - } pub fn with_capacity(capacity: usize) -> Self { Self { last_key: None, @@ -80,12 +70,14 @@ impl RecordMapSerializer { serializer: None, } } + pub fn with_capacity_opt(capacity: Option) -> Self { match capacity { Some(cap) => Self::with_capacity(cap), - None => Self::new(), + None => Self::default(), } } + pub fn serializer(&mut self) -> &mut RecordSerializer { if self.serializer.is_none() { self.serializer = Some(Box::new(RecordSerializer::default())); diff --git a/src/scripting/script.rs b/src/scripting/script.rs index d0e0383..4ef8ade 100644 --- a/src/scripting/script.rs +++ b/src/scripting/script.rs @@ -65,3 +65,21 @@ impl NuScript { .await } } + +#[macro_export] +macro_rules! script { + ($script:ident { + file = $name:literal + args = $argtype:ident + }) => { + pub struct $script; + + impl $crate::scripting::script::Script for $script { + type Args = $argtype; + + fn get_name() -> &'static str { + $name + } + } + }; +} diff --git a/src/tasks/setup_users.rs b/src/tasks/setup_users.rs index 977e9ef..23fd4c4 100644 --- a/src/tasks/setup_users.rs +++ b/src/tasks/setup_users.rs @@ -1,8 +1,11 @@ use serde::Serialize; -use crate::scripting::script::Script; +use crate::script; -pub struct SetupUsersScript; +script!(SetupUsersScript { + file = "setup-users.nu" + args = UsersConfig +}); #[derive(Clone, Debug, Serialize)] pub struct UsersConfig { @@ -16,11 +19,3 @@ pub struct User { pub sudoer: bool, pub shell: String, } - -impl Script for SetupUsersScript { - type Args = UsersConfig; - - fn get_name() -> &'static str { - "setup-users.nu" - } -}