clamav_tcp/
lib.rs

1#![forbid(unsafe_code)]
2use std::{
3    io::Error,
4    net::{SocketAddr, TcpStream, ToSocketAddrs},
5};
6pub mod ping;
7pub mod responses;
8pub mod scan;
9pub mod version;
10pub use ping::ping;
11pub use responses::ScanResult;
12pub use scan::scan;
13use thiserror::Error;
14pub use version::version;
15
16pub type Byte = u8;
17
18#[derive(Error, Debug)]
19pub enum ClamAVClientError {
20    #[error("unable to connect to clamav")]
21    /// If unable to establish a [TcpStream] with the ClamAV instance.
22    UnableToConnect(#[from] Error), //- test
23    #[error("invalid socket address")]
24    /// If the socket address passed to [scan] or [ping] is invalid.
25    ///
26    /// eg.
27    /// ```
28    /// use clamav_tcp;
29    /// assert_eq!(clamav_tcp::ping("hello world").is_err(), true);
30    /// ```
31    ///
32    /// ```
33    /// use clamav_tcp;
34    /// assert_eq!(clamav_tcp::ping("127.0.0.1:3310").is_ok(), true);
35    /// ```
36    InvalidSocketAddress(Error),
37    #[error("unable to parse response to utf-8")]
38    /// When parsing the ClamAV response and the response is not valid UTF-8.
39    InvalidUTf8(Error),
40    /// When the response is valid UTF-8 but it cannot be mapped to a struct.
41    #[error("unable to parse the clamav response")]
42    UnableToParseResponse(String),
43    #[error("unable to write to the stream")]
44    /// Unable to write to the [TcpStream].
45    UnableToWriteToStream(Error),
46}
47
48fn connect_tcp_socket(addr: impl ToSocketAddrs) -> Result<TcpStream, ClamAVClientError> {
49    let addr: Vec<SocketAddr> = addr
50        .to_socket_addrs()
51        .map_err(ClamAVClientError::InvalidSocketAddress)?
52        .collect();
53
54    let stream = TcpStream::connect(&addr[0..]).map_err(ClamAVClientError::UnableToConnect)?;
55    Ok(stream)
56}