From b7bc0366c2ce59433acb82074bbc5811feab9cfd Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Mon, 8 Jul 2024 20:48:56 +0200 Subject: [PATCH] prowocessing: apply most basic reviews --- crates/app/src/main.rs | 6 ++-- .../src/experimental/trait_based/data.rs | 2 -- .../src/experimental/trait_based/data/io.rs | 32 +++++++++++-------- .../src/experimental/trait_based/data/raw.rs | 32 +++++++++---------- .../src/experimental/trait_based/ops/num.rs | 8 ++--- .../src/experimental/trait_based/ops/str.rs | 8 ++--- 6 files changed, 46 insertions(+), 42 deletions(-) diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index d5a260c..ffd7271 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -27,7 +27,7 @@ enum Commands { }, Dev { #[command(subcommand)] - dev_command: DevCommands, + command: DevCommands, }, } @@ -50,7 +50,9 @@ fn main() { machine.feed(ir); machine.eval_full(); } - Commands::Dev { dev_command } => dev_command.run(), + Commands::Dev { + command: dev_command, + } => dev_command.run(), } } diff --git a/crates/prowocessing/src/experimental/trait_based/data.rs b/crates/prowocessing/src/experimental/trait_based/data.rs index 51ec597..e3dd671 100644 --- a/crates/prowocessing/src/experimental/trait_based/data.rs +++ b/crates/prowocessing/src/experimental/trait_based/data.rs @@ -1,7 +1,5 @@ //! Definitions of the data transfer and storage types. -/// Types for element and pipeline IO pub mod io; -/// Raw data types contained in `io` pub mod raw; diff --git a/crates/prowocessing/src/experimental/trait_based/data/io.rs b/crates/prowocessing/src/experimental/trait_based/data/io.rs index 849f955..d091621 100644 --- a/crates/prowocessing/src/experimental/trait_based/data/io.rs +++ b/crates/prowocessing/src/experimental/trait_based/data/io.rs @@ -1,22 +1,26 @@ -use super::raw::{Data, OwnedData}; +//! Types for element and pipeline IO + +use std::convert::Into; + +use super::raw::{Data, DataRef}; /// Newtype struct with borrowed types for pipeline/element inputs, so that doesn't force a move or clone -pub struct Inputs<'a>(Vec>); +pub struct Inputs<'a>(Vec>); impl<'a> Inputs<'a> { /// get inner value(s) - pub(crate) fn inner(&self) -> Vec> { + pub(crate) fn inner(&self) -> Vec> { self.0.clone() } } -impl<'a> From>> for Inputs<'a> { - fn from(value: Vec>) -> Self { +impl<'a> From>> for Inputs<'a> { + fn from(value: Vec>) -> Self { Self(value) } } -impl<'a, T: Into>> From for Inputs<'a> { +impl<'a, T: Into>> From for Inputs<'a> { fn from(value: T) -> Self { Self(vec![value.into()]) } @@ -24,25 +28,25 @@ impl<'a, T: Into>> From for Inputs<'a> { impl<'a> From<&'a Outputs> for Inputs<'a> { fn from(value: &'a Outputs) -> Self { - Self(value.0.iter().map(std::convert::Into::into).collect()) + Self(value.0.iter().map(Into::into).collect()) } } -/// Newtype struct around `OwnedData` for pipeline/element outputs -pub struct Outputs(Vec); +/// Used for pipeline/element outputs +pub struct Outputs(Vec); impl Outputs { /// consume self and return inner value(s) - pub fn into_inner(self) -> Vec { + pub fn into_inner(self) -> Vec { self.0 } } -impl From> for Outputs { - fn from(value: Vec) -> Self { +impl From> for Outputs { + fn from(value: Vec) -> Self { Self(value) } } -impl> From for Outputs { +impl> From for Outputs { fn from(value: T) -> Self { Self(vec![value.into()]) } @@ -53,7 +57,7 @@ impl From> for Outputs { value .0 .into_iter() - .map(|i: Data<'_>| Data::to_owned_data(&i)) + .map(|i: DataRef<'_>| DataRef::to_owned_data(&i)) .collect(), ) } diff --git a/crates/prowocessing/src/experimental/trait_based/data/raw.rs b/crates/prowocessing/src/experimental/trait_based/data/raw.rs index e028474..acc75c1 100644 --- a/crates/prowocessing/src/experimental/trait_based/data/raw.rs +++ b/crates/prowocessing/src/experimental/trait_based/data/raw.rs @@ -1,58 +1,58 @@ -//! Dynamic data storage and transfer types +//! Dynamic data storage and transfer types for use in [`io`] /// Owned data type, for use mostly in outputs and storage #[derive(Clone, Debug)] -pub enum OwnedData { +pub enum Data { String(String), Int(i32), } -impl From for OwnedData { +impl From for Data { fn from(value: String) -> Self { Self::String(value) } } -impl From for OwnedData { +impl From for Data { fn from(value: i32) -> Self { Self::Int(value) } } /// Unowned data type, for inputs into runner functions -#[derive(Clone, Copy)] -pub enum Data<'a> { +#[derive(Clone, Copy, Debug)] +pub enum DataRef<'a> { String(&'a str), Int(i32), } -impl Data<'_> { +impl DataRef<'_> { /// converts itself to `OwnedData` - pub fn to_owned_data(&self) -> OwnedData { + pub fn to_owned_data(&self) -> Data { match self { - Data::String(s) => (*s).to_owned().into(), - Data::Int(i) => (*i).into(), + DataRef::String(s) => (*s).to_owned().into(), + DataRef::Int(i) => (*i).into(), } } } -impl<'a> From<&'a str> for Data<'a> { +impl<'a> From<&'a str> for DataRef<'a> { fn from(value: &'a str) -> Self { Self::String(value) } } -impl From for Data<'_> { +impl From for DataRef<'_> { fn from(value: i32) -> Self { Self::Int(value) } } -impl<'a> From<&'a OwnedData> for Data<'a> { - fn from(value: &'a OwnedData) -> Self { +impl<'a> From<&'a Data> for DataRef<'a> { + fn from(value: &'a Data) -> Self { match value { - OwnedData::String(s) => Data::String(s), - OwnedData::Int(i) => Data::Int(*i), + Data::String(s) => DataRef::String(s), + Data::Int(i) => DataRef::Int(*i), } } } diff --git a/crates/prowocessing/src/experimental/trait_based/ops/num.rs b/crates/prowocessing/src/experimental/trait_based/ops/num.rs index 0b2a295..09b4372 100644 --- a/crates/prowocessing/src/experimental/trait_based/ops/num.rs +++ b/crates/prowocessing/src/experimental/trait_based/ops/num.rs @@ -4,7 +4,7 @@ use core::panic; use crate::experimental::trait_based::{ data::{ io::{Inputs, Outputs}, - raw::Data, + raw::DataRef, }, element::{DataType, ElementSignature, PipelineElement}, }; @@ -14,7 +14,7 @@ pub struct Add(pub i32); impl PipelineElement for Add { fn runner(&self) -> fn(&Inputs) -> Outputs { |input| { - if let [Data::Int(i0), Data::Int(i1), ..] = input.inner()[..] { + if let [DataRef::Int(i0), DataRef::Int(i1), ..] = input.inner()[..] { (i0 + i1).into() } else { panic!("Invalid data passed") @@ -35,7 +35,7 @@ pub struct Subtract(pub i32); impl PipelineElement for Subtract { fn runner(&self) -> fn(&Inputs) -> Outputs { |input| { - if let [Data::Int(i0), Data::Int(i1), ..] = input.inner()[..] { + if let [DataRef::Int(i0), DataRef::Int(i1), ..] = input.inner()[..] { (i0 + i1).into() } else { panic!("Invalid data passed") @@ -56,7 +56,7 @@ pub struct Stringify; impl PipelineElement for Stringify { fn runner(&self) -> fn(&Inputs) -> Outputs { |input| { - if let [Data::Int(int), ..] = input.inner()[..] { + if let [DataRef::Int(int), ..] = input.inner()[..] { int.to_string().into() } else { panic!("Invalid data passed") diff --git a/crates/prowocessing/src/experimental/trait_based/ops/str.rs b/crates/prowocessing/src/experimental/trait_based/ops/str.rs index 38fe992..dc0d99d 100644 --- a/crates/prowocessing/src/experimental/trait_based/ops/str.rs +++ b/crates/prowocessing/src/experimental/trait_based/ops/str.rs @@ -2,7 +2,7 @@ use crate::experimental::trait_based::{ data::{ io::{Inputs, Outputs}, - raw::Data, + raw::DataRef, }, element::{DataType, ElementSignature, PipelineElement}, }; @@ -12,7 +12,7 @@ pub struct Concatenate(pub String); impl PipelineElement for Concatenate { fn runner(&self) -> fn(&Inputs) -> Outputs { |input| { - if let [Data::String(s0), Data::String(s1), ..] = input.inner()[..] { + if let [DataRef::String(s0), DataRef::String(s1), ..] = input.inner()[..] { format!("{s0}{s1}").into() } else { panic!("Invalid data passed") @@ -33,7 +33,7 @@ pub struct Upper; impl PipelineElement for Upper { fn runner(&self) -> fn(&Inputs) -> Outputs { |input| { - if let [Data::String(s), ..] = input.inner()[..] { + if let [DataRef::String(s), ..] = input.inner()[..] { s.to_uppercase().into() } else { panic!("Invalid data passed") @@ -54,7 +54,7 @@ pub struct Lower; impl PipelineElement for Lower { fn runner(&self) -> fn(&Inputs) -> Outputs { |input| { - if let [Data::String(s), ..] = input.inner()[..] { + if let [DataRef::String(s), ..] = input.inner()[..] { s.to_lowercase().into() } else { panic!("Invalid data passed")