Skip to content

Commit

Permalink
Add function in rust core
Browse files Browse the repository at this point in the history
Added function in rs bindings to that it will be available in the
node bindings on frontend.
  • Loading branch information
sudeeptarlekar committed Jan 8, 2024
1 parent df5a992 commit 59df6db
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions application/apps/indexer/session/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
crossbeam-channel = "0.5"
dirs = "5.0"
file-tools = { path = "../addons/file-tools" }
futures = "0.3"
indexer_base = { path = "../indexer_base" }
lazy_static = "1.4"
Expand Down
18 changes: 18 additions & 0 deletions application/apps/indexer/session/src/unbound/api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::events::ComputationError;
use processor::search::filter::SearchFilter;
use serde::Serialize;
use std::path::Path;
use tokio::sync::{mpsc::UnboundedSender, oneshot};

use super::commands::{Command, CommandOutcome};
Expand Down Expand Up @@ -103,6 +104,23 @@ impl UnboundSessionAPI {
.await
}

pub async fn is_file_binary(
&self,
id: u64,
file_path: &'static Path
) -> Result<CommandOutcome<bool>, ComputationError> {
let (tx_results, rx_results) = oneshot::channel();
self.process_command(
id,
rx_results,
Command::IsFileBinary(
file_path,
tx_results
)
)
.await
}

pub async fn spawn_process(
&self,
id: u64,
Expand Down
13 changes: 13 additions & 0 deletions application/apps/indexer/session/src/unbound/commands/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use super::{CommandOutcome, CommandOutcome::Finished};
use crate::events::{ComputationError, ComputationError::OperationNotSupported};
use std::path::Path;
use file_tools::is_binary;

pub fn is_file_binary(
file_path: &Path
) -> Result<CommandOutcome<bool>, ComputationError> {
match is_binary(file_path) {
Ok(is_binary) => Ok(Finished(is_binary)),
Err(err) => Err(OperationNotSupported(err.to_string()))
}
}
7 changes: 7 additions & 0 deletions application/apps/indexer/session/src/unbound/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
mod cancel_test;
mod checksum;
mod dlt;
mod file;
mod folder;
mod process;
mod regex;
mod serial;
mod shells;
mod someip;

use std::path::Path;

use crate::{events::ComputationError, unbound::commands::someip::get_someip_statistic};

use log::{error, trace};
Expand Down Expand Up @@ -73,6 +76,7 @@ pub enum Command {
GetShellProfiles(oneshot::Sender<Result<CommandOutcome<String>, ComputationError>>),
GetContextEnvvars(oneshot::Sender<Result<CommandOutcome<String>, ComputationError>>),
SerialPortsList(oneshot::Sender<Result<CommandOutcome<Vec<String>>, ComputationError>>),
IsFileBinary(&'static Path, oneshot::Sender<Result<CommandOutcome<bool>, ComputationError>>),
CancelTest(
i64,
i64,
Expand All @@ -96,6 +100,7 @@ impl std::fmt::Display for Command {
Command::GetDltStats(_, _) => "Getting dlt stats",
Command::GetSomeipStatistic(_, _) => "Getting someip statistic",
Command::GetRegexError(_, _) => "Checking regex",
Command::IsFileBinary(_, _) => "Checking if file is binary",
}
)
}
Expand Down Expand Up @@ -129,6 +134,7 @@ pub async fn process(command: Command, signal: Signal) {
Command::GetShellProfiles(tx) => tx.send(shells::get_valid_profiles(signal)).is_err(),
Command::GetContextEnvvars(tx) => tx.send(shells::get_context_envvars(signal)).is_err(),
Command::SerialPortsList(tx) => tx.send(serial::available_ports(signal)).is_err(),
Command::IsFileBinary(file_path, tx) => tx.send(file::is_file_binary(file_path)).is_err(),
Command::CancelTest(a, b, tx) => tx
.send(cancel_test::cancel_test(a, b, signal).await)
.is_err(),
Expand All @@ -149,6 +155,7 @@ pub async fn err(command: Command, err: ComputationError) {
Command::GetShellProfiles(tx) => tx.send(Err(err)).is_err(),
Command::GetContextEnvvars(tx) => tx.send(Err(err)).is_err(),
Command::SerialPortsList(tx) => tx.send(Err(err)).is_err(),
Command::IsFileBinary(_filepath, tx) => tx.send(Err(err)).is_err(),
Command::CancelTest(_a, _b, tx) => tx.send(Err(err)).is_err(),
} {
error!("Fail to send error response for command: {cmd}");
Expand Down
8 changes: 8 additions & 0 deletions application/apps/rustcore/rs-bindings/Cargo.lock

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

18 changes: 18 additions & 0 deletions application/apps/rustcore/rs-bindings/src/js/jobs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use session::{
use std::{convert::TryFrom, thread};
use tokio::runtime::Runtime;
use tokio_util::sync::CancellationToken;
use std::path::Path;

struct UnboundJobs {
api: Option<UnboundSessionAPI>,
Expand Down Expand Up @@ -135,6 +136,23 @@ impl UnboundJobs {
.map(CommandOutcomeWrapper)
}

async fn is_file_binary(
&self,
id: i64,
file_path: &'static Path,
) -> Result<CommandOutcomeWrapper<bool>, ComputationErrorWrapper> {
self.api
.as_ref()
.ok_or(ComputationError::SessionUnavailable)?
.is_file_binary(
id_from_i64(id)?,
file_path
)
.await
.map_err(ComputationErrorWrapper)
.map(CommandOutcomeWrapper)
}

#[node_bindgen]
async fn spawn_process(
&self,
Expand Down

0 comments on commit 59df6db

Please sign in to comment.