use crate::types::error::Error;
use crate::zeek::zeek_log_proto::ZeekProtocol;
use std::str::FromStr;
use std::path::Path;
use derive_builder::Builder;
#[derive(Debug, Default, Builder)]
#[builder(setter(into))]
#[builder(derive(PartialEq, Eq,))]
pub struct
ZeekSearchParams<'a>
{
#[builder(default)]
pub path_prefix: Option<&'a str>,
#[builder(default)]
pub start_date: Option<&'a str>,
#[builder(default)]
pub end_date: Option<&'a str>,
#[builder(default)]
pub proto_type: Option<&'a str>, #[builder(default)]
pub src_ip: Option<&'a str>,
}
impl<'a> ZeekSearchParams<'a>
{
pub fn check(&self) -> u8 {
match (&self.src_ip, &self.proto_type, &self.end_date)
{
(None, None, None) => return 0,
(None, None, Some(_end)) => return 1,
(None, Some(_log), None) => return 2,
(None, Some(_log), Some(_end)) => return 3,
(Some(src_ip), None, None) => return 4,
(Some(src_ip), None, Some(_end)) => return 5,
(Some(src_ip), Some(_log), None) => return 6,
(Some(src_ip), Some(_log), Some(_end)) => return 7,
_ => return 8 }
}
pub fn get_start_date_path(&self) -> String
{
let mut search_path = String::new();
if Self::path_prefix_exists(self)
{
search_path.push_str(&self.path_prefix.unwrap());
search_path.push_str("/");
search_path.push_str(&self.start_date.unwrap());
search_path.push_str("/");
}
else
{
search_path.push_str(&self.start_date.unwrap());
search_path.push_str("/");
}
if search_path.as_bytes()[0] as u16 == 126 as u16
{
search_path.remove(0);
search_path.insert_str(0, std::env::var("HOME")
.expect("should exist on linux").as_str());
}
search_path
}
fn path_prefix_exists(&self) -> bool
{
match &self.path_prefix
{
Some(_) => { return true }
None => { return false}
}
}
fn check_date_format(p: &'a Path) -> bool
{
let val = &p.to_str();
if let Some(v) = val
{
let v : Vec<_> = v.split('/').collect();
let v : Vec<_> = v[v.len()-1].split('-').collect();
if v.len() != 3
{
return false
}
for i in 0..v.len()
{
let number = u16::from_str(v[i]);
if let Err(_) = number
{
return false
}
}
return true
}
false
}
}