Cleanup code and fix clippy warnings

Signed-off-by: trivernis <trivernis@protonmail.com>
integration-not-installation
trivernis 2 years ago
parent 1b617577e2
commit 52b39b1598
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -90,6 +90,7 @@ impl NuExecutor {
add_variables_to_state(vars, &mut engine_state, &mut stack); add_variables_to_state(vars, &mut engine_state, &mut stack);
let (block, main_id) = read_script_file(&self.script_path, &mut engine_state).await?; 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( nu_engine::eval_block(
&engine_state, &engine_state,
&mut stack, &mut stack,
@ -104,6 +105,7 @@ impl NuExecutor {
// block in a different thread to be able to execute scripts in parallel // block in a different thread to be able to execute scripts in parallel
tokio::task::spawn_blocking(move || { 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); let call_block = create_call(main_id, args);
nu_engine::eval_block( nu_engine::eval_block(
@ -169,6 +171,7 @@ async fn read_script_file(
} }
} }
/// Parses a nu script
fn parse_nu<'a>( fn parse_nu<'a>(
engine_state: &'a mut EngineState, engine_state: &'a mut EngineState,
script: &[u8], 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<RecordValue>) -> Block { fn create_call(decl_id: DeclId, args: Vec<RecordValue>) -> Block {
let args = args let args = args
.into_iter() .into_iter()

@ -9,10 +9,14 @@ pub struct RecordSerializer {
list_serializer: Option<RecordListSerializer>, list_serializer: Option<RecordListSerializer>,
map_serializer: Option<RecordMapSerializer>, map_serializer: Option<RecordMapSerializer>,
} }
#[derive(Default)]
pub struct RecordListSerializer { pub struct RecordListSerializer {
serializer: Option<Box<RecordSerializer>>, serializer: Option<Box<RecordSerializer>>,
entries: Vec<RecordValue>, entries: Vec<RecordValue>,
} }
#[derive(Default)]
pub struct RecordMapSerializer { pub struct RecordMapSerializer {
serializer: Option<Box<RecordSerializer>>, serializer: Option<Box<RecordSerializer>>,
last_key: Option<RecordValue>, last_key: Option<RecordValue>,
@ -20,7 +24,7 @@ pub struct RecordMapSerializer {
} }
impl RecordSerializer { impl RecordSerializer {
pub fn list_serializer<'a>(&'a mut self, cap: Option<usize>) -> &'a mut RecordListSerializer { pub fn list_serializer(&mut self, cap: Option<usize>) -> &mut RecordListSerializer {
if self.list_serializer.is_none() { if self.list_serializer.is_none() {
self.list_serializer = Some(RecordListSerializer::with_capacity_opt(cap)); self.list_serializer = Some(RecordListSerializer::with_capacity_opt(cap));
} }
@ -36,13 +40,6 @@ impl RecordSerializer {
} }
impl RecordListSerializer { impl RecordListSerializer {
pub fn new() -> Self {
Self {
entries: Vec::new(),
serializer: None,
}
}
pub fn with_capacity(capacity: usize) -> Self { pub fn with_capacity(capacity: usize) -> Self {
Self { Self {
entries: Vec::with_capacity(capacity), entries: Vec::with_capacity(capacity),
@ -53,7 +50,7 @@ impl RecordListSerializer {
pub fn with_capacity_opt(capacity: Option<usize>) -> Self { pub fn with_capacity_opt(capacity: Option<usize>) -> Self {
match capacity { match capacity {
Some(cap) => Self::with_capacity(cap), Some(cap) => Self::with_capacity(cap),
None => Self::new(), None => Self::default(),
} }
} }
@ -66,13 +63,6 @@ impl RecordListSerializer {
} }
impl RecordMapSerializer { impl RecordMapSerializer {
pub fn new() -> Self {
Self {
last_key: None,
entries: Vec::new(),
serializer: None,
}
}
pub fn with_capacity(capacity: usize) -> Self { pub fn with_capacity(capacity: usize) -> Self {
Self { Self {
last_key: None, last_key: None,
@ -80,12 +70,14 @@ impl RecordMapSerializer {
serializer: None, serializer: None,
} }
} }
pub fn with_capacity_opt(capacity: Option<usize>) -> Self { pub fn with_capacity_opt(capacity: Option<usize>) -> Self {
match capacity { match capacity {
Some(cap) => Self::with_capacity(cap), Some(cap) => Self::with_capacity(cap),
None => Self::new(), None => Self::default(),
} }
} }
pub fn serializer(&mut self) -> &mut RecordSerializer { pub fn serializer(&mut self) -> &mut RecordSerializer {
if self.serializer.is_none() { if self.serializer.is_none() {
self.serializer = Some(Box::new(RecordSerializer::default())); self.serializer = Some(Box::new(RecordSerializer::default()));

@ -65,3 +65,21 @@ impl<S: Script> NuScript<S> {
.await .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
}
}
};
}

@ -1,8 +1,11 @@
use serde::Serialize; 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)] #[derive(Clone, Debug, Serialize)]
pub struct UsersConfig { pub struct UsersConfig {
@ -16,11 +19,3 @@ pub struct User {
pub sudoer: bool, pub sudoer: bool,
pub shell: String, pub shell: String,
} }
impl Script for SetupUsersScript {
type Args = UsersConfig;
fn get_name() -> &'static str {
"setup-users.nu"
}
}

Loading…
Cancel
Save