Skip to content

Commit

Permalink
feat: move messages to its own trait
Browse files Browse the repository at this point in the history
Resolves #53
  • Loading branch information
Erin van der Veen committed May 14, 2024
1 parent eeb3320 commit 3e41af2
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion genealogos-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ cyclonedx-bom.workspace = true
env_logger.workspace = true
indicatif.workspace = true
indicatif-log-bridge.workspace = true
genealogos = { workspace = true, features = [ "backend_handle_messages", "clap" ] }
genealogos = { workspace = true, features = [ "clap" ] }
nixtract.workspace = true

[features]
Expand Down
4 changes: 2 additions & 2 deletions genealogos-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::path;
use clap::Parser;

use genealogos::args;
use genealogos::backend::{Backend, BackendHandle};
use genealogos::backend::{Backend, BackendHandle, BackendHandleMessages};

/// `cli` application for processing data files and generating CycloneDX output
#[derive(Parser, Debug)]
Expand Down Expand Up @@ -57,7 +57,7 @@ fn main() -> Result<()> {
};

// Initialize the backend and get access to the status update messages
let (mut backend, handle) = *args.backend.get_backend()?;
let (mut backend, handle) = *args.backend.get_backend_messages()?;

// Set backend options
backend.include_narinfo(args.include_narinfo);
Expand Down
4 changes: 0 additions & 4 deletions genealogos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ default = []
# It disables all tests that require a working internet connection.
nix = []

# The messages function of the `BanendHandle` trait violates object safety.
# For those users of the library that don't need to use this trait as an object, and would like access to the `messages` function, this feature should be enabled.
backend_handle_messages = []

clap = [ "args", "dep:clap" ]
rocket = [ "args", "dep:rocket" ]

Expand Down
8 changes: 7 additions & 1 deletion genealogos/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::str::FromStr as _;
#[cfg(feature = "clap")]
use std::sync::OnceLock;

use crate::backend::{Backend, BackendHandle};
use crate::backend::{Backend, BackendHandle, BackendHandleMessages};
use crate::bom::Bom;
use crate::error::*;

Expand All @@ -26,7 +26,13 @@ pub enum BackendArg {
}

impl BackendArg {
/// Get a backend and handle that is object safe
pub fn get_backend(&self) -> Result<Box<(impl Backend, impl BackendHandle)>> {
self.get_backend_messages()
}

/// Get a backend and handle that is not object safe and implements the `messages` function
pub fn get_backend_messages(&self) -> Result<Box<(impl Backend, impl BackendHandleMessages)>> {
match self {
BackendArg::Nixtract => Ok(Box::new(crate::backend::nixtract_backend::Nixtract::new())),
}
Expand Down
13 changes: 9 additions & 4 deletions genealogos/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,15 @@ pub trait BackendHandle {
/// Gets all messages that were produced since the previous call to this function
fn new_messages(&self) -> Result<Vec<Message>>;

/// Gets an iterator over all messages
#[cfg(feature = "backend_handle_messages")]
fn messages(&self) -> Result<impl Iterator<Item = Message>>;

/// Gets an upper bound to the number of different ids to expect in the messages
fn max_index(&self) -> usize;
}

/// `BackendHandleMessages` is a trait that defines additional behaviour of a backend handle.
///
/// This trait is seperate from the `BackendHandle` trait because it can't be made int a trait object.
/// BackendHandle is a supertrait of this trait.
pub trait BackendHandleMessages: BackendHandle {
/// Gets an iterator over all messages
fn messages(&self) -> Result<impl Iterator<Item = Message>>;
}
11 changes: 6 additions & 5 deletions genealogos/src/backend/nixtract_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,18 @@ impl super::BackendHandle for NixtractHandle {
Ok(messages)
}

#[cfg(feature = "backend_handle_messages")]
fn max_index(&self) -> usize {
rayon::current_num_threads()
}
}

impl super::BackendHandleMessages for NixtractHandle {
fn messages(&self) -> crate::Result<impl Iterator<Item = super::Message>> {
Ok(self.receiver.iter().map(|m| super::Message {
index: m.id,
content: m.to_string(),
}))
}

fn max_index(&self) -> usize {
rayon::current_num_threads()
}
}

impl<T> From<T> for Model
Expand Down

0 comments on commit 3e41af2

Please sign in to comment.