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
//! Data for CLI 
//! 

use serde::{Serialize, Deserialize};
use chrono::serde::ts_milliseconds;

#[derive(Serialize, Deserialize)]
struct JobInfo {
    opk: chiral_common::kinds::Operator,
    #[serde(with = "ts_milliseconds")]
    time_save: chrono::DateTime<chrono::Utc> 
}

#[derive(Serialize, Deserialize)]
pub struct DataLocal {
     jobs: std::collections::HashMap<chiral_common::JobID, JobInfo> 
}

impl DataLocal {
    fn serialize(&self) -> String { serde_json::to_string(self).unwrap() }
    fn deserialize(content: &String) -> Self { serde_json::from_str(content).unwrap() }

    pub fn load() -> Self {
        let mut dl = Self { jobs: std::collections::HashMap::<chiral_common::JobID, JobInfo>::new() };
        let filepath = crate::utils::get_jobs_filepath();
        if filepath.exists() {
            let content = std::fs::read_to_string(filepath).unwrap();
            dl = Self::deserialize(&content);
        }

        dl
    }

    pub fn save(&self) -> Result<(), std::io::Error> {
        let filepath = crate::utils::get_jobs_filepath();
        std::fs::File::create(filepath)
            .and_then(|mut dest| {
                std::io::copy(&mut self.serialize().as_bytes(), &mut dest)
            })
            .map(|_| ())
    }

    pub fn add_job(&mut self, id: chiral_common::JobID, opk: chiral_common::kinds::Operator) {
        let time_save = chrono::Utc::now(); 
        self.jobs.insert(id, JobInfo { opk, time_save });
    }

    pub fn print_jobs(&self) {
        for (id, ji) in self.jobs.iter() {
            let opk = &ji.opk;
            let time_save = ji.time_save;
            println!("{id:24}{opk:10}{time_save:?}");
        }
    }

    pub fn get_job(&self, id: &chiral_common::JobID) -> Option<&chiral_common::kinds::Operator> {
        match self.jobs.get(id) {
            Some(ji) => Some(&ji.opk),
            None => None
        }
    }

    pub fn get_full_job_id(&self, prefix: &chiral_common::JobID) -> Result<&chiral_common::JobID, crate::errors::CommandLineError> {
        for (job_id, _) in self.jobs.iter() {
            if job_id.starts_with(prefix) {
                return Ok(job_id);
            }
        }

        let msg = format!("Report file for JobID prefix '{prefix}' not found");
        Err(crate::errors::CommandLineError::JobIDNotFound(msg))
    }

    pub fn print_report(&self, prefix: &chiral_common::JobID) -> Result<(), crate::errors::CommandLineError> {
        let job_id = self.get_full_job_id(prefix)?;
        let fp = crate::utils::get_report_filepath(job_id.as_str());
        if !fp.exists() {
            return Err(crate::errors::CommandLineError::ReportNotFound(format!("Report file '{fp:?}' not found")));
        }

        let content = std::fs::read_to_string(fp).unwrap();
        if let Some(opk) = self.get_job(&job_id) {
            match opk {
                chiral_common::kinds::Operator::OpenBabelSSMatching => action_report::<chiral_operator::substructure::ob_ss_match::Report>(&content),
                chiral_common::kinds::Operator::OpenBabelSimilaritySearching => action_report::<chiral_operator::fingerprint::ob_similarity::Report>(&content)
            }
            Ok(())
        } else {
            Err(crate::errors::CommandLineError::ReportNotFound(format!("Report type for JobID '{job_id}' not found")))
        }
    }
}

fn action_report<R: chiral_common::OperatorReport>(content: &String) {
    let report = R::deserialize(content);
    report.print();
}