1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
//! Holds various data structures used for chasing files

use std::io::BufReader;
use std::fs::File;
use std::time::Duration;

use std::path::PathBuf;

pub const DEFAULT_ROTATION_CHECK_WAIT_MILLIS: u64 = 100;
pub const DEFAULT_NOT_ROTATED_WAIT_MILLIS: u64 = 50;

#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
pub struct Line(pub usize);

#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
pub struct Pos(pub u64);

#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
pub(crate) struct FileId(pub(crate) u64);

/// Your entry point for chasing a file.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
pub struct Chaser {
    pub(crate) line: Line,
    pub(crate) path: PathBuf,
    pub(crate) rotation_check_wait: Duration,
    pub(crate) rotation_check_attempts: Option<usize>,
    pub(crate) not_rotated_wait: Duration,
}

#[derive(Debug)]
pub(crate) struct Chasing<'a> {
    pub(crate) chaser: &'a mut Chaser,
    pub(crate) file_id: FileId,
    pub(crate) reader: BufReader<File>,
    pub(crate) buffer: String,
    pub(crate) line: Line,
    pub(crate) pos: Pos,
}

impl Chaser {
    /// Creates a new Chaser
    pub fn new<S>(path: S) -> Chaser
    where
        S: Into<PathBuf>,
    {
        Chaser {
            line: Line(0),
            path: path.into(),
            rotation_check_attempts: None,
            rotation_check_wait: Duration::from_millis(DEFAULT_ROTATION_CHECK_WAIT_MILLIS),
            not_rotated_wait: Duration::from_millis(DEFAULT_NOT_ROTATED_WAIT_MILLIS),
        }
    }

    pub fn set_path<S>(&mut self, path: &str) -> ()
    where
        S: Into<PathBuf>,
    {
        self.path = path.into();
    }

    pub fn get_path(&self) -> &PathBuf {
        &self.path
    }

    pub fn get_line(&self) -> Line {
        self.line
    }

    pub fn set_line(&mut self, line: Line) -> () {
        self.line = line;
    }

    pub fn get_rotation_check_wait(&self) -> &Duration {
        &self.rotation_check_wait
    }

    pub fn set_rotation_check_wait(&mut self, duration: Duration) -> () {
        self.rotation_check_wait = duration;
    }
    pub fn get_not_rotated_wait(&self) -> &Duration {
        &self.not_rotated_wait
    }

    pub fn set_not_rotated_wait(&mut self, duration: Duration) -> () {
        self.not_rotated_wait = duration;
    }

    pub fn get_rotation_check_attempts(&self) -> Option<usize> {
        self.rotation_check_attempts
    }

    pub fn set_rotation_check_attempts(&mut self, attempts: Option<usize>) -> () {
        self.rotation_check_attempts = attempts;
    }
}