Skip to content

Commit

Permalink
Obey clippy
Browse files Browse the repository at this point in the history
Signed-off-by: Heinz N. Gies <[email protected]>
  • Loading branch information
Licenser committed Jun 11, 2024
1 parent 3a9b311 commit baad318
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 46 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 18 additions & 5 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,40 @@ use tremor_connectors::ConnectorBuilder;
use tremor_script::{ast, highlighter::Highlighter};
use tremor_system::{
killswitch::{KillSwitch, ShutdownMode},
selector::{PluginType, RuleSelector, RuleSelectorBuilder},
selector::{PluginType, Rules, RulesBuilder},
};

/// Runtime builder for configuring the runtime
/// note that includees and excludes are handled in order!
/// In other wordds using with_connector("foo").without_connector("foo") will result in foo being included
/// In other wordds using `with_connector("foo").without_connector("foo`") will result in foo being included
/// this is especially important when using the type based inclusions and exclusions
pub struct RuntimeBuilder {
connectors: RuleSelectorBuilder,
connectors: RulesBuilder,
}

impl RuntimeBuilder {
/// Marks a given connector as includec by name
#[must_use]
pub fn with_connector(mut self, connector: &str) -> Self {
self.connectors = self.connectors.include(connector);
self
}
/// Marks multiple connectors as includec by name
#[must_use]
pub fn with_connectors(mut self, connectors: &[&str]) -> Self {
for connector in connectors {
self.connectors = self.connectors.include(*connector);
}
self
}
/// Marks a given connector as excludec by name
#[must_use]
pub fn without_connector(mut self, connector: &str) -> Self {
self.connectors = self.connectors.exclude(connector);
self
}
/// Marks multiple connectors as excludec by name
#[must_use]
pub fn without_connectors(mut self, connectors: &[&str]) -> Self {
for connector in connectors {
self.connectors = self.connectors.exclude(*connector);
Expand All @@ -62,33 +66,39 @@ impl RuntimeBuilder {
}

/// includes debug connectors
#[must_use]
pub fn with_debug_connectors(mut self) -> Self {
self.connectors = self.connectors.include(PluginType::Debug);
self
}
/// excludes debug connectors
#[must_use]
pub fn without_debug_connectors(mut self) -> Self {
self.connectors = self.connectors.exclude(PluginType::Debug);
self
}

/// includes all normal (non debug) connectors
#[must_use]
pub fn with_normal_connectors(mut self) -> Self {
self.connectors = self.connectors.include(PluginType::Normal);
self
}
/// excludes all normal (non debug) connectors
#[must_use]
pub fn without_normal_connectors(mut self) -> Self {
self.connectors = self.connectors.exclude(PluginType::Normal);
self
}
/// If no rule matches, include the connector
#[must_use]
pub fn default_include_connectors(self) -> RuntimeConfig {
let connectors = self.connectors.default_include();
RuntimeConfig { connectors }
}

/// If no rule matches, exclude the connector
#[must_use]
pub fn default_exclude_connectors(self) -> RuntimeConfig {
let connectors = self.connectors.default_exclude();
RuntimeConfig { connectors }
Expand All @@ -99,11 +109,13 @@ impl RuntimeBuilder {
/// Configuration for the runtime
pub struct RuntimeConfig {
/// if debug connectors should be loaded
connectors: RuleSelector,
connectors: Rules,
}

impl RuntimeConfig {
/// Builds the runtime
/// # Errors
/// if the runtime can't be started
pub async fn build(self) -> Result<(Runtime, JoinHandle<Result<()>>)> {
Runtime::start(self).await
}
Expand All @@ -121,9 +133,10 @@ pub struct Runtime {

impl Runtime {
/// creates a runtime builder
#[must_use]
pub fn builder() -> RuntimeBuilder {
RuntimeBuilder {
connectors: RuleSelector::builder(),
connectors: Rules::builder(),
}
}
/// Instantiate a flow from
Expand Down
15 changes: 8 additions & 7 deletions tremor-archive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,6 @@ pub(crate) async fn build_archive_from_source<W: AsyncWrite + Unpin + Send>(
}
};
let mut other_warnings = BTreeSet::new();
let reg = &*FN_REGISTRY.read().map_err(|_| Error::ReadLock)?;
let helper = Helper::new(reg, &aggr_reg);

for stmt in &deploy.deploy.stmts {
match stmt {
Expand Down Expand Up @@ -224,6 +222,9 @@ pub(crate) async fn build_archive_from_source<W: AsyncWrite + Unpin + Send>(
.0
.iter()
.map(|(k, v)| {
let reg = &*FN_REGISTRY.read().map_err(|_| Error::ReadLock)?;
let helper = Helper::new(reg, &aggr_reg);

Ok((
k.to_string(),
v.clone()
Expand Down Expand Up @@ -286,16 +287,16 @@ pub(crate) async fn build_archive_from_source<W: AsyncWrite + Unpin + Send>(
header.set_cksum();
ar.append_data(&mut header, "main.troy", src.as_bytes())
.await?;

for (id, paths) in MODULES
let modules = MODULES
.read()
.map_err(|_| Error::ReadLock)?
.modules()
.iter()
.map(|m| (m.arena_idx, m.paths()))
{
.map(|m| (m.arena_idx, m.paths().to_vec()))
.collect::<Vec<_>>();
for (id, paths) in modules {
if let Some(src) = Arena::get(id)? {
for p in paths {
for p in &paths {
let mut file: PathBuf = p.module().iter().collect();
file.push(p.id());
let mut header = Header::new_gnu();
Expand Down
2 changes: 1 addition & 1 deletion tremor-common/src/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ mod test {
let instance = Instance::from("test3");
assert_eq!(instance.to_string(), "test3");

let instance = Instance::from(instance);
let instance = instance;
assert_eq!(instance.to_string(), "test3");
}

Expand Down
2 changes: 1 addition & 1 deletion tremor-connectors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ pub struct ConnectorType(String);

impl ConnectorType {
/// name of the connector
pub fn name(&self) -> &str {
#[must_use] pub fn name(&self) -> &str {
self.0.as_str()
}
}
Expand Down
1 change: 1 addition & 0 deletions tremor-script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ tremor-kv = "0.6"
unicode-xid = "0.2"
url = "2"
xz2 = "0.1"
tokio = { version = "1", default-features = false, features = [] }

[build-dependencies]
lalrpop = "0.20"
Expand Down
65 changes: 33 additions & 32 deletions tremor-system/src/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,17 @@ pub enum PluginType {
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Hash, Eq)]

/// Single selector rule
#[derive(Default)]
pub enum Selector {
/// Select all plugins
#[default]
All,
/// Select plugins by name
Name(String),
/// Select plugins by type
Type(PluginType),
}

impl Default for Selector {
fn default() -> Self {
Selector::All
}
}
impl Selector {
/// Test if a plugin should be selected
pub fn test(&self, name: &(impl AsRef<str> + ?Sized), t: PluginType) -> bool {
Expand Down Expand Up @@ -75,18 +72,18 @@ pub enum Deposition {
Exclude,
}

impl Into<Deposition> for bool {
fn into(self) -> Deposition {
if self {
impl From<bool> for Deposition {
fn from(val: bool) -> Self {
if val {
Deposition::Include
} else {
Deposition::Exclude
}
}
}
impl Into<bool> for Deposition {
fn into(self) -> bool {
match self {
impl From<Deposition> for bool {
fn from(val: Deposition) -> Self {
match val {
Deposition::Include => true,
Deposition::Exclude => false,
}
Expand All @@ -95,48 +92,52 @@ impl Into<bool> for Deposition {
#[derive(Debug, Clone)]

/// Selector combiner for includes and excludes. Excludes take precedence over incluedes.
pub struct RuleSelector {
pub struct Rules {
rules: Vec<(Selector, Deposition)>,
default: Deposition,
}
/// Rule selector builder
pub struct RuleSelectorBuilder {
pub struct RulesBuilder {
rules: Vec<(Selector, Deposition)>,
}

impl RuleSelectorBuilder {
impl RulesBuilder {
/// adds an include rule

#[must_use]
pub fn include(mut self, s: impl Into<Selector>) -> Self {
self.rules.push((s.into(), Deposition::Include));
self
}
/// adds an exclude rule
#[must_use]
pub fn exclude(mut self, s: impl Into<Selector>) -> Self {
self.rules.push((s.into(), Deposition::Exclude));
self
}

/// default disposition include
pub fn default_include(self) -> RuleSelector {
RuleSelector {
#[must_use]
pub fn default_include(self) -> Rules {
Rules {
rules: self.rules,
default: Deposition::Include,
}
}
/// default disposition exclude
pub fn default_exclude(self) -> RuleSelector {
RuleSelector {
#[must_use]
pub fn default_exclude(self) -> Rules {
Rules {
rules: self.rules,
default: Deposition::Exclude,
}
}
}

impl RuleSelector {
impl Rules {
/// Create a new rule selector builder
pub fn builder() -> RuleSelectorBuilder {
RuleSelectorBuilder { rules: vec![] }
#[must_use]
pub fn builder() -> RulesBuilder {
RulesBuilder { rules: vec![] }
}
/// Test if a plugin should be selected
pub fn test(&self, name: &(impl AsRef<str> + ?Sized), t: PluginType) -> bool {
Expand All @@ -161,25 +162,25 @@ mod test {
}
#[test]
fn test_rule_selector() {
let rs = RuleSelector::builder()
let rs = Rules::builder()
.include("foo")
.exclude(PluginType::Debug)
.default_include();
assert_eq!(rs.test("foo", PluginType::Normal), true);
assert_eq!(rs.test("foo", PluginType::Debug), true);
assert_eq!(rs.test("bar", PluginType::Normal), true);
assert_eq!(rs.test("bar", PluginType::Debug), false);
assert!(rs.test("foo", PluginType::Normal));
assert!(rs.test("foo", PluginType::Debug));
assert!(rs.test("bar", PluginType::Normal));
assert!(!rs.test("bar", PluginType::Debug));
}

#[test]
fn test_rule_selector_builder() {
let rs = RuleSelector::builder()
let rs = Rules::builder()
.include("foo")
.exclude(PluginType::Debug)
.default_exclude();
assert_eq!(rs.test("foo", PluginType::Normal), true);
assert_eq!(rs.test("foo", PluginType::Debug), true);
assert_eq!(rs.test("bar", PluginType::Normal), false);
assert_eq!(rs.test("bar", PluginType::Debug), false);
assert!(rs.test("foo", PluginType::Normal));
assert!(rs.test("foo", PluginType::Debug));
assert!(!rs.test("bar", PluginType::Normal));
assert!(!rs.test("bar", PluginType::Debug));
}
}

0 comments on commit baad318

Please sign in to comment.