Skip to content

Commit

Permalink
i2c: Use PathBuf for socket paths instead of Strings
Browse files Browse the repository at this point in the history
Signed-off-by: Bilal Elmoussaoui <[email protected]>
  • Loading branch information
bilelmoussaoui committed Sep 15, 2023
1 parent 8056cf0 commit 432304b
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions crates/i2c/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ mod i2c;
mod vhu_i2c;

use log::{error, info, warn};
use std::num::ParseIntError;
use std::process::exit;
use std::sync::{Arc, RwLock};
use std::thread::{spawn, JoinHandle};
use std::{num::ParseIntError, path::PathBuf};

use clap::Parser;
use thiserror::Error as ThisError;
Expand Down Expand Up @@ -53,7 +53,7 @@ pub(crate) enum Error {
struct I2cArgs {
/// Location of vhost-user Unix domain socket. This is suffixed by 0,1,2..socket_count-1.
#[clap(short, long)]
socket_path: String,
socket_path: PathBuf,

/// Number of guests (sockets) to connect to.
#[clap(short = 'c', long, default_value_t = 1)]
Expand Down Expand Up @@ -153,7 +153,7 @@ impl TryFrom<&str> for AdapterConfig {

#[derive(PartialEq, Debug)]
struct I2cConfiguration {
socket_path: String,
socket_path: PathBuf,
socket_count: usize,
devices: AdapterConfig,
}
Expand All @@ -168,7 +168,7 @@ impl TryFrom<I2cArgs> for I2cConfiguration {

let devices = AdapterConfig::try_from(args.device_list.trim())?;
Ok(I2cConfiguration {
socket_path: args.socket_path.trim().to_string(),
socket_path: args.socket_path,
socket_count: args.socket_count,
devices,
})
Expand All @@ -184,7 +184,7 @@ fn start_backend<D: 'static + I2cDevice + Send + Sync>(args: I2cArgs) -> Result<
let mut handles = Vec::new();

for i in 0..config.socket_count {
let socket = config.socket_path.to_owned() + &i.to_string();
let socket = config.socket_path.join(i.to_string());
let i2c_map = i2c_map.clone();

let handle: JoinHandle<Result<()>> = spawn(move || loop {
Expand Down Expand Up @@ -249,6 +249,9 @@ fn main() {

#[cfg(test)]
mod tests {

use std::path::Path;

use assert_matches::assert_matches;

use super::*;
Expand All @@ -270,9 +273,9 @@ mod tests {
}

impl I2cArgs {
fn from_args(path: &str, devices: &str, count: usize) -> I2cArgs {
fn from_args(path: impl AsRef<Path>, devices: &str, count: usize) -> I2cArgs {
I2cArgs {
socket_path: path.to_string(),
socket_path: path.as_ref().to_owned(),
socket_count: count,
device_list: devices.to_string(),
}
Expand Down Expand Up @@ -345,7 +348,7 @@ mod tests {

let expected_config = I2cConfiguration {
socket_count: 5,
socket_path: String::from(socket_name),
socket_path: PathBuf::from(socket_name),
devices: expected_devices,
};

Expand Down

0 comments on commit 432304b

Please sign in to comment.