chiral_common/
traits.rs

1//! Traits of chiral: Serialization, Operator
2//! 
3
4pub type SerializedFormat = String;
5
6pub trait Serialization {
7    fn ser_to(&self) -> SerializedFormat; 
8    fn ser_from(content: &SerializedFormat) -> Self; 
9}
10
11pub trait TraitFileRequirements {
12    fn dir(&self) -> String { ".".to_string() }
13    fn dir_full(&self) -> String { ".".to_string() }
14    fn files_in(&self) -> Vec<String> { vec![] }
15    fn files_out(&self) -> Vec<String> { vec![] }
16}
17
18pub trait TraitData {
19    fn blank() -> Self;
20    fn len(&self) -> usize;
21}
22
23pub trait TraitDataStore {
24    fn get_id_smiles_pairs(&self, dsk: &crate::kinds::Dataset, div_index: &crate::job::DividendIndex) -> Option<crate::app::chem::types::IdSmilesPairs>;
25}
26
27pub trait TraitFileClient : std::marker::Sync + std::marker::Send {
28    fn download_files(&self, _local_dir: &str, _remote_dir: &str, _files: &Vec<String>) -> anyhow::Result<()> { Ok(()) }
29    fn upload_files(&self, _local_dir: &str, _remote_dir: &str, _files: &Vec<String>) -> anyhow::Result<()> { Ok(()) }
30    fn remove_local_files(&self, _local_dir: &str, _files: &Vec<String>) -> anyhow::Result<()> { Ok(()) }
31    fn remove_local_dir(&self, _local_dir: &str) -> anyhow::Result<()> { Ok(()) }
32}
33
34pub trait TraitOperator {
35    type InputType;
36    type DataType;
37    type OutputType;
38    type ReportType;
39
40    fn new(opk: &crate::kinds::Operator) -> Self;
41    fn get_kind(&self) -> crate::kinds::Operator;
42    fn prepare_data(&self, dsk: &crate::kinds::Dataset, div_index: &crate::job::DividendIndex, ds: std::sync::Arc<std::sync::Mutex<dyn TraitDataStore>>) -> Option<Self::DataType>;
43    fn compute(&self, input: &Self::InputType, data: &Self::DataType, div_index: &crate::job::DividendIndex) -> Self::OutputType;
44    fn report(&self, job_id: crate::job::ID, input: Self::InputType, data: &Self::DataType, output: Self::OutputType) -> Self::ReportType;
45}
46
47pub trait TraitInput: Serialization {
48    fn default() -> Self;
49}
50
51pub trait TraitOutput: Serialization {
52    fn blank() -> Self;
53    fn clear(&mut self);
54    fn append(&mut self, other: &mut Self);
55    fn len(&self) -> usize;
56}
57
58pub trait TraitReport: Serialization {
59    fn print(&self);
60    fn save(&self, filepath: &std::path::PathBuf) -> std::io::Result<u64> {
61        std::fs::File::create(filepath)
62            .and_then(|mut dest| {
63                std::io::copy(&mut self.ser_to().as_bytes(), &mut dest)
64            })
65    }
66}