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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//! Safe, idiomatic Rust bindings to the IUPAC InChI reference library for
//! generating **InChI** and **InChIKey** chemical identifiers.
//!
//! The InChI C library is vendored and statically linked through the
//! [`inchi-sys`](inchi_sys) crate, so there is nothing to install and no
//! network access at build time. This crate wraps it in a panic-free,
//! [`Result`]-based API where every native allocation is released
//! automatically (RAII) and no `unsafe` is exposed at the public boundary.
//!
//! # Quickstart
//!
//! Generate an InChI and InChIKey from a Molfile. Pass `()` for default
//! options (or [`Options`] to customize):
//!
//! ```
//! use inchi::{from_molfile, inchikey};
//!
//! // A minimal V2000 Molfile for methane (one carbon; hydrogens are implicit).
//! let methane = "\n example\n\n 1 0 0 0 0 0 0 0 0 0999 V2000\n\
//! \x20 0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\nM END\n";
//!
//! let result = from_molfile(methane, ())?;
//! assert_eq!(result.inchi(), "InChI=1S/CH4/h1H4");
//!
//! let key = inchikey(result.inchi())?;
//! assert_eq!(key, "VNWKTOKETHGBQD-UHFFFAOYSA-N");
//! # Ok::<(), inchi::InchiError>(())
//! ```
//!
//! Or build a structure programmatically:
//!
//! ```
//! use inchi::{Molecule, Atom, BondOrder};
//!
//! let mut ethanol = Molecule::new();
//! let c1 = ethanol.add_atom(Atom::new("C"));
//! let c2 = ethanol.add_atom(Atom::new("C"));
//! let o = ethanol.add_atom(Atom::new("O"));
//! ethanol.add_bond(c1, c2, BondOrder::Single)?;
//! ethanol.add_bond(c2, o, BondOrder::Single)?;
//!
//! assert_eq!(ethanol.to_inchi(())?.inchi(), "InChI=1S/C2H6O/c1-2-3/h3H,2H2,1H3");
//! # Ok::<(), inchi::InchiError>(())
//! ```
//!
//! # API overview
//!
//! This crate wraps the complete InChI generation, parsing, and validation API.
//! For concepts and worked examples (standard vs. non-standard InChI, stereo
//! conventions, polymers), see the [`guide`].
//!
//! **Structure → InChI**
//! - [`from_molfile`] — Molfile/SDF text (`MakeINCHIFromMolfileText`).
//! - [`Molecule::to_inchi`] — a programmatically built structure (`GetINCHI`,
//! or `GetINCHIEx` when [polymer units](Molecule::add_polymer_unit) are present).
//!
//! **InChI → structure**
//! - [`struct_from_inchi`] — atoms, bonds, and 0D stereo (`GetStructFromINCHI`).
//! - [`struct_from_inchi_ex`] — the above plus polymer data (`GetStructFromINCHIEx`).
//! - [`struct_from_aux_info`] — recover a structure from an `AuxInfo` block.
//!
//! **InChI → InChIKey** (feature `key`)
//! - [`inchikey`] / [`inchikey_with_hashes`] (`GetINCHIKeyFromINCHI`).
//!
//! **Conversion & validation**
//! - [`inchi_to_inchi`] — re-derive/normalize an InChI (`GetINCHIfromINCHI`).
//! - [`check_inchi`] — validate an InChI string (`CheckINCHI`).
//! - [`check_inchikey`] — validate an InChIKey string (`CheckINCHIKey`).
//!
//! Generation is tuned through [`Options`], including the experimental
//! [`Polymers`] extension. Native allocations are released automatically.
//!
//! # Feature flags
//!
//! | Feature | Default | Description |
//! | --------------------- | ------- | ---------------------------------------------------------------- |
//! | `key` | yes | Enables [`inchikey`]/[`inchikey_with_hashes`] (`GetINCHIKeyFromINCHI`). |
//! | `regenerate-bindings` | no | Regenerates the FFI bindings with `bindgen` (needs `libclang`). |
//!
//! # Thread safety
//!
//! The underlying InChI C library keeps internal `static` state and is not
//! guaranteed to be re-entrant, so every call into it is serialized behind a
//! global lock. All functions in this crate are therefore safe to call from
//! multiple threads, but calls do not run concurrently with one another. The
//! public types are [`Send`] and [`Sync`].
//!
//! # Minimum supported Rust version
//!
//! This crate supports Rust **1.74** and later. Raising the MSRV is treated as
//! a semver-breaking change.
pub use inchi_to_inchi;
pub use ;
pub use ;
pub use ;
pub use InchiOutput;
pub use ;
pub use ;
pub use ;
use c_char;
/// Generates an InChI directly from a Molfile (V2000/V3000 SDF text).
///
/// This is the most convenient entry point: hand it the text of a `.mol`/`.sdf`
/// record and it returns the InChI, auxiliary information, and any warnings.
///
/// # Errors
///
/// Returns [`InchiError::Failed`] if the library could not produce an InChI,
/// [`InchiError::EmptyResult`] if it reported success but produced nothing, and
/// [`InchiError::InteriorNul`] if `molfile` contains a NUL byte.
///
/// ```
/// use inchi::from_molfile;
///
/// let water = "\n ex\n\n 1 0 0 0 0 0 0 0 0 0999 V2000\n\
/// \x20 0.0000 0.0000 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0\nM END\n";
/// let out = from_molfile(water, ())?;
/// assert_eq!(out.inchi(), "InChI=1S/H2O/h1H2");
/// # Ok::<(), inchi::InchiError>(())
/// ```
///
/// The second argument accepts `()` for defaults or an [`Options`] for control
/// (stereo, fixed-H, polymers, …).
/// Computes the 27-character InChIKey for an InChI string.
///
/// # Errors
///
/// Returns [`InchiError::Failed`] if the input is not a valid InChI, and
/// [`InchiError::InteriorNul`] if it contains a NUL byte.
///
/// ```
/// use inchi::inchikey;
///
/// assert_eq!(inchikey("InChI=1S/C2H6O/c1-2-3/h3H,2H2,1H3")?, "LFQSCWFLJHTTHZ-UHFFFAOYSA-N");
/// # Ok::<(), inchi::InchiError>(())
/// ```
/// An InChIKey together with the optional 256-bit hash extensions.
///
/// The extension blocks are only populated when requested via
/// [`inchikey_with_hashes`]; otherwise they are [`None`]. Each, when present, is
/// a string of up to 64 hexadecimal characters.
/// Computes the InChIKey and, optionally, its two 256-bit hash extensions.
///
/// The hash extensions are an opt-in feature of the InChIKey algorithm that
/// reduce the (already tiny) collision probability of the 27-character key. Pass
/// `true` for `extra1`/`extra2` to compute the first/second extension block;
/// each requested block appears in the returned [`InchiKey`].
///
/// [`inchikey`] is the convenience form that returns just the key string.
///
/// # Errors
///
/// Returns [`InchiError::Failed`] if the input is not a valid InChI, and
/// [`InchiError::InteriorNul`] if it contains a NUL byte.
///
/// ```
/// use inchi::inchikey_with_hashes;
///
/// let k = inchikey_with_hashes("InChI=1S/CH4/h1H4", true, false)?;
/// assert_eq!(k.key, "VNWKTOKETHGBQD-UHFFFAOYSA-N");
/// assert!(k.extra1.is_some());
/// assert!(k.extra2.is_none());
/// # Ok::<(), inchi::InchiError>(())
/// ```
/// Reads a NUL-terminated hash-extension buffer into an owned `String`.
/// Shared post-processing for the `Get*`/`Make*` entry points: classifies the
/// return code and reads the populated [`OutputGuard`](raw::OutputGuard).
pub