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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use alloc::vec::Vec;
use super::{
fetch::{fetch_dataset, fetch_dataset_collection},
reader::{DatasetSmilesIter, DatasetSmilesRecordIter},
types::{
DatasetArtifact, DatasetCollectionArtifact, DatasetCompression, DatasetError,
DatasetFetchOptions, DatasetFile,
},
};
/// Metadata for a downloadable dataset source.
pub trait DatasetSource {
/// Stable dataset identifier used for cache subdirectories and diagnostics.
fn id(&self) -> &'static str;
/// Upstream URL for the dataset artifact.
fn url(&self) -> &'static str;
/// File name of the cached upstream artifact.
fn file_name(&self) -> &'static str;
/// File name of the decompressed artifact.
///
/// For uncompressed sources this should equal [`Self::file_name`].
fn extracted_file_name(&self) -> &'static str {
self.file_name()
}
/// Compression used by the upstream dataset artifact.
fn compression(&self) -> DatasetCompression {
DatasetCompression::None
}
/// Fetches the dataset using default options.
///
/// # Errors
///
/// Returns [`DatasetError`] if the download fails or the cached artifact
/// cannot be materialized on disk.
///
/// # Examples
///
/// ```no_run
/// use smiles_rs::datasets::{DatasetSource, MASS_SPEC_GYM_SMILES};
///
/// let artifact = MASS_SPEC_GYM_SMILES.fetch()?;
/// assert!(artifact.path().ends_with("MassSpecGym.tsv"));
/// # Ok::<(), smiles_rs::DatasetError>(())
/// ```
fn fetch(&self) -> Result<DatasetArtifact, DatasetError> {
self.fetch_with_options(&DatasetFetchOptions::default())
}
/// Fetches the dataset using explicit options.
///
/// # Errors
///
/// Returns [`DatasetError`] if the download fails or the requested cached
/// artifact layout cannot be materialized on disk.
///
/// # Examples
///
/// ```no_run
/// use smiles_rs::datasets::{ArchiveMode, DatasetFetchOptions, DatasetSource, PUBCHEM_SMILES};
///
/// let artifact = PUBCHEM_SMILES.fetch_with_options(&DatasetFetchOptions {
/// archive_mode: ArchiveMode::Decompress,
/// ..DatasetFetchOptions::default()
/// })?;
/// assert!(artifact.path().ends_with("CID-SMILES"));
/// # Ok::<(), smiles_rs::DatasetError>(())
/// ```
fn fetch_with_options(
&self,
options: &DatasetFetchOptions,
) -> Result<DatasetArtifact, DatasetError> {
fetch_dataset(self, options)
}
}
/// Metadata for a dataset that is distributed across multiple files.
pub trait DatasetCollectionSource {
/// Stable dataset identifier used for cache subdirectories and diagnostics.
fn id(&self) -> &'static str;
/// Upstream files that make up this dataset collection.
fn files(&self) -> Vec<DatasetFile>;
/// Fetches the dataset collection using default options.
///
/// # Errors
///
/// Returns [`DatasetError`] if any file cannot be downloaded or
/// materialized on disk.
fn fetch_collection(&self) -> Result<DatasetCollectionArtifact, DatasetError> {
self.fetch_collection_with_options(&DatasetFetchOptions::default())
}
/// Fetches the dataset collection using explicit options.
///
/// # Errors
///
/// Returns [`DatasetError`] if any file cannot be downloaded or
/// materialized on disk.
fn fetch_collection_with_options(
&self,
options: &DatasetFetchOptions,
) -> Result<DatasetCollectionArtifact, DatasetError> {
fetch_dataset_collection(self, options)
}
}
/// A dataset source that can stream SMILES strings directly.
pub trait SmilesDatasetSource {
/// Opens a streaming iterator over the dataset SMILES using default fetch
/// options.
///
/// # Errors
///
/// Returns [`DatasetError`] if the dataset cannot be fetched or if the
/// materialized dataset cannot be opened for streaming.
///
/// # Examples
///
/// ```no_run
/// use smiles_rs::datasets::{PUBCHEM_SMILES, SmilesDatasetSource};
///
/// let mut smiles = PUBCHEM_SMILES.iter_smiles()?;
/// if let Some(first) = smiles.next() {
/// let _ = first?;
/// }
/// # Ok::<(), smiles_rs::DatasetError>(())
/// ```
fn iter_smiles(&self) -> Result<DatasetSmilesIter, DatasetError> {
self.iter_smiles_with_options(&DatasetFetchOptions::default())
}
/// Opens a streaming iterator over the dataset SMILES using explicit fetch
/// options.
///
/// # Errors
///
/// Returns [`DatasetError`] if the dataset cannot be fetched or if the
/// materialized dataset cannot be opened for streaming.
///
/// # Examples
///
/// ```no_run
/// use smiles_rs::datasets::{
/// ArchiveMode, DatasetFetchOptions, PUBCHEM_SMILES, SmilesDatasetSource,
/// };
///
/// let mut smiles = PUBCHEM_SMILES.iter_smiles_with_options(&DatasetFetchOptions {
/// archive_mode: ArchiveMode::KeepCompressed,
/// ..DatasetFetchOptions::default()
/// })?;
/// if let Some(first) = smiles.next() {
/// let _ = first?;
/// }
/// # Ok::<(), smiles_rs::DatasetError>(())
/// ```
fn iter_smiles_with_options(
&self,
options: &DatasetFetchOptions,
) -> Result<DatasetSmilesIter, DatasetError>;
}
/// A dataset source that can stream SMILES records with dataset identifiers.
pub trait SmilesDatasetRecordSource {
/// Opens a streaming iterator over dataset records using default fetch
/// options.
///
/// # Errors
///
/// Returns [`DatasetError`] if the dataset cannot be fetched or opened.
fn iter_records(&self) -> Result<DatasetSmilesRecordIter, DatasetError> {
self.iter_records_with_options(&DatasetFetchOptions::default())
}
/// Opens a streaming iterator over dataset records using explicit fetch
/// options.
///
/// # Errors
///
/// Returns [`DatasetError`] if the dataset cannot be fetched or opened.
fn iter_records_with_options(
&self,
options: &DatasetFetchOptions,
) -> Result<DatasetSmilesRecordIter, DatasetError>;
}