Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
toblux committed Mar 14, 2024
1 parent b9b3bdf commit 29b65b2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 64 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ Support for `async-std` is also available by enabling the `async-std` feature:
clamav-client = { version = "0.5.0", features = ["async-std"] }
```

## Migrations

### Migrate to version 0.5.0

The `*_socket` and `*_tcp` functions have been deprecated in favor of more general functions with the same name, but without the suffixes. These updated functions, such as `ping`, `scan_buffer`, and `scan_file`, now have the connection type (TCP or Unix socket) as a parameter, effectively replacing the `host_address` and `socket_path` parameters.

Here's an example:

```rust
let clamd_host_address = "localhost:3310";
let result = clamav_client::scan_file_tcp("README.md", clamd_host_address, None);
assert!(result.is_ok());
```

becomes this in version 0.5.0:

```rust
let clamd_tcp = clamav_client::Tcp{ host_address: "localhost:3310" };
let result = clamav_client::scan_file("README.md", clamd_tcp, None);
assert!(result.is_ok());
```

## Examples

### Usage
Expand Down
48 changes: 24 additions & 24 deletions src/async_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,29 +311,29 @@ pub struct Socket<P: AsRef<Path>> {

/// The communication protocol to use
#[async_trait(?Send)]
pub trait AsyncTransportProtocol {
pub trait TransportProtocol {
/// Bidirectional stream
type Stream: ReadExt + WriteExt + Unpin;

/// Converts the protocol instance into the corresponding stream
async fn to_stream(&self) -> io::Result<Self::Stream>;
async fn connect(&self) -> io::Result<Self::Stream>;
}

#[async_trait(?Send)]
impl<A: ToSocketAddrs> AsyncTransportProtocol for Tcp<A> {
impl<A: ToSocketAddrs> TransportProtocol for Tcp<A> {
type Stream = TcpStream;

async fn to_stream(&self) -> io::Result<Self::Stream> {
async fn connect(&self) -> io::Result<Self::Stream> {
TcpStream::connect(&self.host_address).await
}
}

#[async_trait(?Send)]
#[cfg(unix)]
impl<P: AsRef<Path>> AsyncTransportProtocol for Socket<P> {
impl<P: AsRef<Path>> TransportProtocol for Socket<P> {
type Stream = UnixStream;

async fn to_stream(&self) -> io::Result<Self::Stream> {
async fn connect(&self) -> io::Result<Self::Stream> {
UnixStream::connect(&self.socket_path).await
}
}
Expand All @@ -345,7 +345,7 @@ impl<P: AsRef<Path>> AsyncTransportProtocol for Socket<P> {
///
/// # Arguments
///
/// * `transport_protocol`: The protocol to use (either TCP or a Unix socket connection)
/// * `connection`: The protocol to use (either TCP or a Unix socket connection)
///
/// # Returns
///
Expand All @@ -365,8 +365,8 @@ impl<P: AsRef<Path>> AsyncTransportProtocol for Socket<P> {
/// # }
/// ```
///
pub async fn ping<T: AsyncTransportProtocol>(transport_protocol: T) -> IoResult {
let stream = transport_protocol.to_stream().await?;
pub async fn ping<T: TransportProtocol>(connection: T) -> IoResult {
let stream = connection.connect().await?;
send_command(stream, PING, Some(PONG.len())).await
}

Expand All @@ -378,7 +378,7 @@ pub async fn ping<T: AsyncTransportProtocol>(transport_protocol: T) -> IoResult
///
/// # Arguments
///
/// * `transport_protocol`: The protocol to use (either TCP or a Unix socket connection)
/// * `connection`: The protocol to use (either TCP or a Unix socket connection)
///
/// # Returns
///
Expand All @@ -395,8 +395,8 @@ pub async fn ping<T: AsyncTransportProtocol>(transport_protocol: T) -> IoResult
/// # }
/// ```
///
pub async fn get_version<T: AsyncTransportProtocol>(transport_protocol: T) -> IoResult {
let stream = transport_protocol.to_stream().await?;
pub async fn get_version<T: TransportProtocol>(connection: T) -> IoResult {
let stream = connection.connect().await?;
send_command(stream, VERSION, None).await
}

Expand All @@ -408,20 +408,20 @@ pub async fn get_version<T: AsyncTransportProtocol>(transport_protocol: T) -> Io
/// # Arguments
///
/// * `file_path`: The path to the file to be scanned
/// * `transport_protocol`: The protocol to use (either TCP or a Unix socket connection)
/// * `connection`: The protocol to use (either TCP or a Unix socket connection)
/// * `chunk_size`: An optional chunk size for reading data. If [`None`], a default chunk size is used
///
/// # Returns
///
/// An [`IoResult`] containing the server's response as a vector of bytes
///
pub async fn scan_file<P: AsRef<Path>, T: AsyncTransportProtocol>(
pub async fn scan_file<P: AsRef<Path>, T: TransportProtocol>(
file_path: P,
transport_protocol: T,
connection: T,
chunk_size: Option<usize>,
) -> IoResult {
let file = File::open(file_path).await?;
let stream = transport_protocol.to_stream().await?;
let stream = connection.connect().await?;
scan(file, chunk_size, stream).await
}

Expand All @@ -432,19 +432,19 @@ pub async fn scan_file<P: AsRef<Path>, T: AsyncTransportProtocol>(
/// # Arguments
///
/// * `buffer`: The data to be scanned
/// * `transport_protocol`: The protocol to use (either TCP or a Unix socket connection)
/// * `connection`: The protocol to use (either TCP or a Unix socket connection)
/// * `chunk_size`: An optional chunk size for reading data. If [`None`], a default chunk size is used
///
/// # Returns
///
/// An [`IoResult`] containing the server's response as a vector of bytes
///
pub async fn scan_buffer<T: AsyncTransportProtocol>(
pub async fn scan_buffer<T: TransportProtocol>(
buffer: &[u8],
transport_protocol: T,
connection: T,
chunk_size: Option<usize>,
) -> IoResult {
let stream = transport_protocol.to_stream().await?;
let stream = connection.connect().await?;
scan(buffer, chunk_size, stream).await
}

Expand All @@ -455,7 +455,7 @@ pub async fn scan_buffer<T: AsyncTransportProtocol>(
/// # Arguments
///
/// * `input_stream`: The stream to be scanned
/// * `transport_protocol`: The protocol to use (either TCP or a Unix socket connection)
/// * `connection`: The protocol to use (either TCP or a Unix socket connection)
/// * `chunk_size`: An optional chunk size for reading data. If [`None`], a default chunk size is used
///
/// # Returns
Expand All @@ -464,12 +464,12 @@ pub async fn scan_buffer<T: AsyncTransportProtocol>(
///
pub async fn scan_stream<
S: Stream<Item = Result<bytes::Bytes, io::Error>>,
T: AsyncTransportProtocol,
T: TransportProtocol,
>(
input_stream: S,
transport_protocol: T,
connection: T,
chunk_size: Option<usize>,
) -> IoResult {
let output_stream = transport_protocol.to_stream().await?;
let output_stream = connection.connect().await?;
_scan_stream(input_stream, chunk_size, output_stream).await
}
30 changes: 15 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,13 @@ pub trait TransportProtocol {
type Stream: Read + Write;

/// Converts the protocol instance into the corresponding stream
fn to_stream(&self) -> io::Result<Self::Stream>;
fn connect(&self) -> io::Result<Self::Stream>;
}

impl<A: ToSocketAddrs> TransportProtocol for Tcp<A> {
type Stream = TcpStream;

fn to_stream(&self) -> io::Result<Self::Stream> {
fn connect(&self) -> io::Result<Self::Stream> {
TcpStream::connect(&self.host_address)
}
}
Expand All @@ -358,7 +358,7 @@ impl<A: ToSocketAddrs> TransportProtocol for Tcp<A> {
impl<P: AsRef<Path>> TransportProtocol for Socket<P> {
type Stream = UnixStream;

fn to_stream(&self) -> io::Result<Self::Stream> {
fn connect(&self) -> io::Result<Self::Stream> {
UnixStream::connect(&self.socket_path)
}
}
Expand All @@ -370,7 +370,7 @@ impl<P: AsRef<Path>> TransportProtocol for Socket<P> {
///
/// # Arguments
///
/// * `transport_protocol`: The protocol to use (either TCP or a Unix socket connection)
/// * `connection`: The protocol to use (either TCP or a Unix socket connection)
///
/// # Returns
///
Expand All @@ -387,8 +387,8 @@ impl<P: AsRef<Path>> TransportProtocol for Socket<P> {
/// # assert!(clamd_available);
/// ```
///
pub fn ping<T: TransportProtocol>(transport_protocol: T) -> IoResult {
let stream = transport_protocol.to_stream()?;
pub fn ping<T: TransportProtocol>(connection: T) -> IoResult {
let stream = connection.connect()?;
_ping(stream)
}

Expand All @@ -400,7 +400,7 @@ pub fn ping<T: TransportProtocol>(transport_protocol: T) -> IoResult {
///
/// # Arguments
///
/// * `transport_protocol`: The protocol to use (either TCP or a Unix socket connection)
/// * `connection`: The protocol to use (either TCP or a Unix socket connection)
///
/// # Returns
///
Expand All @@ -414,8 +414,8 @@ pub fn ping<T: TransportProtocol>(transport_protocol: T) -> IoResult {
/// # assert!(version.starts_with(b"ClamAV"));
/// ```
///
pub fn get_version<T: TransportProtocol>(transport_protocol: T) -> IoResult {
let stream = transport_protocol.to_stream()?;
pub fn get_version<T: TransportProtocol>(connection: T) -> IoResult {
let stream = connection.connect()?;
_get_version(stream)
}

Expand All @@ -427,7 +427,7 @@ pub fn get_version<T: TransportProtocol>(transport_protocol: T) -> IoResult {
/// # Arguments
///
/// * `file_path`: The path to the file to be scanned
/// * `transport_protocol`: The protocol to use (either TCP or a Unix socket connection)
/// * `connection`: The protocol to use (either TCP or a Unix socket connection)
/// * `chunk_size`: An optional chunk size for reading data. If [`None`], a default chunk size is used
///
/// # Returns
Expand All @@ -436,11 +436,11 @@ pub fn get_version<T: TransportProtocol>(transport_protocol: T) -> IoResult {
///
pub fn scan_file<P: AsRef<Path>, T: TransportProtocol>(
file_path: P,
transport_protocol: T,
connection: T,
chunk_size: Option<usize>,
) -> IoResult {
let file = File::open(file_path)?;
let stream = transport_protocol.to_stream()?;
let stream = connection.connect()?;
scan(file, chunk_size, stream)
}

Expand All @@ -452,7 +452,7 @@ pub fn scan_file<P: AsRef<Path>, T: TransportProtocol>(
/// # Arguments
///
/// * `buffer`: The data to be scanned
/// * `transport_protocol`: The protocol to use (either TCP or a Unix socket connection)
/// * `connection`: The protocol to use (either TCP or a Unix socket connection)
/// * `chunk_size`: An optional chunk size for reading data. If [`None`], a default chunk size is used
///
/// # Returns
Expand All @@ -461,9 +461,9 @@ pub fn scan_file<P: AsRef<Path>, T: TransportProtocol>(
///
pub fn scan_buffer<T: TransportProtocol>(
buffer: &[u8],
transport_protocol: T,
connection: T,
chunk_size: Option<usize>,
) -> IoResult {
let stream = transport_protocol.to_stream()?;
let stream = connection.connect()?;
scan(buffer, chunk_size, stream)
}
Loading

0 comments on commit 29b65b2

Please sign in to comment.