diff --git a/README.md b/README.md index f87d719..7915886 100644 --- a/README.md +++ b/README.md @@ -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 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. + +For 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: + +```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 diff --git a/src/async_std.rs b/src/async_std.rs index 989c64a..d8eae2b 100644 --- a/src/async_std.rs +++ b/src/async_std.rs @@ -311,29 +311,29 @@ pub struct Socket> { /// 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; + async fn connect(&self) -> io::Result; } #[async_trait(?Send)] -impl AsyncTransportProtocol for Tcp { +impl TransportProtocol for Tcp { type Stream = TcpStream; - async fn to_stream(&self) -> io::Result { + async fn connect(&self) -> io::Result { TcpStream::connect(&self.host_address).await } } #[async_trait(?Send)] #[cfg(unix)] -impl> AsyncTransportProtocol for Socket

{ +impl> TransportProtocol for Socket

{ type Stream = UnixStream; - async fn to_stream(&self) -> io::Result { + async fn connect(&self) -> io::Result { UnixStream::connect(&self.socket_path).await } } @@ -345,7 +345,7 @@ impl> AsyncTransportProtocol for Socket

{ /// /// # 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 /// @@ -365,8 +365,8 @@ impl> AsyncTransportProtocol for Socket

{ /// # } /// ``` /// -pub async fn ping(transport_protocol: T) -> IoResult { - let stream = transport_protocol.to_stream().await?; +pub async fn ping(connection: T) -> IoResult { + let stream = connection.connect().await?; send_command(stream, PING, Some(PONG.len())).await } @@ -378,7 +378,7 @@ pub async fn ping(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 /// @@ -395,8 +395,8 @@ pub async fn ping(transport_protocol: T) -> IoResult /// # } /// ``` /// -pub async fn get_version(transport_protocol: T) -> IoResult { - let stream = transport_protocol.to_stream().await?; +pub async fn get_version(connection: T) -> IoResult { + let stream = connection.connect().await?; send_command(stream, VERSION, None).await } @@ -408,20 +408,20 @@ pub async fn get_version(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, T: AsyncTransportProtocol>( +pub async fn scan_file, T: TransportProtocol>( file_path: P, - transport_protocol: T, + connection: T, chunk_size: Option, ) -> 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 } @@ -432,19 +432,19 @@ pub async fn scan_file, 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( +pub async fn scan_buffer( buffer: &[u8], - transport_protocol: T, + connection: T, chunk_size: Option, ) -> IoResult { - let stream = transport_protocol.to_stream().await?; + let stream = connection.connect().await?; scan(buffer, chunk_size, stream).await } @@ -455,7 +455,7 @@ pub async fn scan_buffer( /// # 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 @@ -464,12 +464,12 @@ pub async fn scan_buffer( /// pub async fn scan_stream< S: Stream>, - T: AsyncTransportProtocol, + T: TransportProtocol, >( input_stream: S, - transport_protocol: T, + connection: T, chunk_size: Option, ) -> IoResult { - let output_stream = transport_protocol.to_stream().await?; + let output_stream = connection.connect().await?; _scan_stream(input_stream, chunk_size, output_stream).await } diff --git a/src/lib.rs b/src/lib.rs index 7518699..38c220d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; + fn connect(&self) -> io::Result; } impl TransportProtocol for Tcp { type Stream = TcpStream; - fn to_stream(&self) -> io::Result { + fn connect(&self) -> io::Result { TcpStream::connect(&self.host_address) } } @@ -358,7 +358,7 @@ impl TransportProtocol for Tcp { impl> TransportProtocol for Socket

{ type Stream = UnixStream; - fn to_stream(&self) -> io::Result { + fn connect(&self) -> io::Result { UnixStream::connect(&self.socket_path) } } @@ -370,7 +370,7 @@ impl> TransportProtocol for Socket

{ /// /// # 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 /// @@ -387,8 +387,8 @@ impl> TransportProtocol for Socket

{ /// # assert!(clamd_available); /// ``` /// -pub fn ping(transport_protocol: T) -> IoResult { - let stream = transport_protocol.to_stream()?; +pub fn ping(connection: T) -> IoResult { + let stream = connection.connect()?; _ping(stream) } @@ -400,7 +400,7 @@ pub fn ping(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 /// @@ -414,8 +414,8 @@ pub fn ping(transport_protocol: T) -> IoResult { /// # assert!(version.starts_with(b"ClamAV")); /// ``` /// -pub fn get_version(transport_protocol: T) -> IoResult { - let stream = transport_protocol.to_stream()?; +pub fn get_version(connection: T) -> IoResult { + let stream = connection.connect()?; _get_version(stream) } @@ -427,7 +427,7 @@ pub fn get_version(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 @@ -436,11 +436,11 @@ pub fn get_version(transport_protocol: T) -> IoResult { /// pub fn scan_file, T: TransportProtocol>( file_path: P, - transport_protocol: T, + connection: T, chunk_size: Option, ) -> IoResult { let file = File::open(file_path)?; - let stream = transport_protocol.to_stream()?; + let stream = connection.connect()?; scan(file, chunk_size, stream) } @@ -452,7 +452,7 @@ pub fn scan_file, 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 @@ -461,9 +461,9 @@ pub fn scan_file, T: TransportProtocol>( /// pub fn scan_buffer( buffer: &[u8], - transport_protocol: T, + connection: T, chunk_size: Option, ) -> IoResult { - let stream = transport_protocol.to_stream()?; + let stream = connection.connect()?; scan(buffer, chunk_size, stream) } diff --git a/src/tokio.rs b/src/tokio.rs index e59305c..43b5d76 100644 --- a/src/tokio.rs +++ b/src/tokio.rs @@ -103,7 +103,7 @@ async fn _scan_stream< /// /// # 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 /// @@ -323,29 +323,29 @@ pub struct Socket> { /// The communication protocol to use #[async_trait(?Send)] -pub trait AsyncTransportProtocol { +pub trait TransportProtocol { /// Bidirectional stream type Stream: AsyncRead + AsyncWrite + Unpin; /// Converts the protocol instance into the corresponding stream - async fn to_stream(&self) -> io::Result; + async fn connect(&self) -> io::Result; } #[async_trait(?Send)] -impl AsyncTransportProtocol for Tcp { +impl TransportProtocol for Tcp { type Stream = TcpStream; - async fn to_stream(&self) -> io::Result { + async fn connect(&self) -> io::Result { TcpStream::connect(&self.host_address).await } } #[async_trait(?Send)] #[cfg(unix)] -impl> AsyncTransportProtocol for Socket

{ +impl> TransportProtocol for Socket

{ type Stream = UnixStream; - async fn to_stream(&self) -> io::Result { + async fn connect(&self) -> io::Result { UnixStream::connect(&self.socket_path).await } } @@ -357,7 +357,7 @@ impl> AsyncTransportProtocol for Socket

{ /// /// # 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 /// @@ -377,8 +377,8 @@ impl> AsyncTransportProtocol for Socket

{ /// # } /// ``` /// -pub async fn ping(transport_protocol: T) -> IoResult { - let stream = transport_protocol.to_stream().await?; +pub async fn ping(connection: T) -> IoResult { + let stream = connection.connect().await?; send_command(stream, PING, Some(PONG.len())).await } @@ -390,7 +390,7 @@ pub async fn ping(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 /// @@ -407,8 +407,8 @@ pub async fn ping(transport_protocol: T) -> IoResult /// # } /// ``` /// -pub async fn get_version(transport_protocol: T) -> IoResult { - let stream = transport_protocol.to_stream().await?; +pub async fn get_version(connection: T) -> IoResult { + let stream = connection.connect().await?; send_command(stream, VERSION, None).await } @@ -420,20 +420,20 @@ pub async fn get_version(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, T: AsyncTransportProtocol>( +pub async fn scan_file, T: TransportProtocol>( file_path: P, - transport_protocol: T, + connection: T, chunk_size: Option, ) -> 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 } @@ -444,19 +444,19 @@ pub async fn scan_file, 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( +pub async fn scan_buffer( buffer: &[u8], - transport_protocol: T, + connection: T, chunk_size: Option, ) -> IoResult { - let stream = transport_protocol.to_stream().await?; + let stream = connection.connect().await?; scan(buffer, chunk_size, stream).await } @@ -467,7 +467,7 @@ pub async fn scan_buffer( /// # 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 @@ -477,12 +477,12 @@ pub async fn scan_buffer( #[cfg(feature = "tokio-stream")] pub async fn scan_stream< S: Stream>, - T: AsyncTransportProtocol, + T: TransportProtocol, >( input_stream: S, - transport_protocol: T, + connection: T, chunk_size: Option, ) -> IoResult { - let output_stream = transport_protocol.to_stream().await?; + let output_stream = connection.connect().await?; _scan_stream(input_stream, chunk_size, output_stream).await }