@@ -3,24 +3,24 @@ use std::{cell::RefCell, iter::zip};
3
3
use itertools:: { EitherOrBoth , Itertools } ;
4
4
use miette:: Diagnostic ;
5
5
use nom:: {
6
+ IResult ,
6
7
branch:: alt,
7
8
bytes:: complete:: tag,
8
9
character:: complete:: { alpha1, alphanumeric1, char, one_of, space0, space1} ,
9
10
combinator:: { cut, map, opt, recognize} ,
10
11
error:: ErrorKind ,
11
12
multi:: { many0, many1, separated_list1} ,
12
13
sequence:: { delimited, pair, preceded, terminated, tuple} ,
13
- IResult ,
14
14
} ;
15
- use nom_miette:: { map_res , wrap_err , FromExternalError , LabeledErrorKind , LabeledParseError } ;
15
+ use nom_miette:: { FromExternalError , LabeledErrorKind , LabeledParseError , map_res , wrap_err } ;
16
16
use polychem:: {
17
+ Count , ModificationId , Polymer , Polymerizer ,
17
18
errors:: PolychemError ,
18
19
parsers:: {
19
20
chemical_composition,
20
21
errors:: PolychemErrorKind ,
21
22
primitives:: { count, lowercase, offset_kind, uppercase} ,
22
23
} ,
23
- Count , ModificationId , Polymer , Polymerizer ,
24
24
} ;
25
25
use thiserror:: Error ;
26
26
@@ -38,13 +38,6 @@ const CTON_BOND: &str = "CToN";
38
38
const CROSSLINK_BOND : & str = "Link" ;
39
39
const LAT_CROSSLINK_BOND : & str = "Lat-Link" ;
40
40
41
- // FIXME: A horrible hack that's needed to specify the lifetimes captured by `impl FnMut(...) -> ...` correctly. Once
42
- // Rust 2024 is stabilized, however, this hack can be removed. Keep an eye on:
43
- // https://github.com/rust-lang/rust/issues/117587
44
- // FIXME: Make private again
45
- pub trait Captures < U > { }
46
- impl < T : ?Sized , U > Captures < U > for T { }
47
-
48
41
// FIXME: Paste all of these EBNF comments into another file and make sure they are valid!
49
42
50
43
/// Muropeptide = Monomer , { Connection , Monomer } , [ Connection ] , [ { " " }- ,
@@ -57,8 +50,7 @@ impl<T: ?Sized, U> Captures<U> for T {}
57
50
#[ allow( clippy:: too_many_lines) ]
58
51
pub fn muropeptide < ' z , ' a , ' p , ' s > (
59
52
polymerizer : & ' z Polymerizer < ' a , ' p > ,
60
- ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , Muropeptide < ' a , ' p > > + Captures < ( & ' z ( ) , & ' a ( ) , & ' p ( ) ) >
61
- {
53
+ ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , Muropeptide < ' a , ' p > > {
62
54
move |i| {
63
55
let polymer = RefCell :: new ( polymerizer. new_polymer ( ) ) ;
64
56
// FIXME: Perhaps there is a better way to shorten that `polymer` borrow...
@@ -208,22 +200,19 @@ pub fn muropeptide<'z, 'a, 'p, 's>(
208
200
} ;
209
201
210
202
let polymer = polymer. into_inner ( ) ;
211
- Ok ( (
212
- rest,
213
- Muropeptide {
214
- polymer,
215
- monomers,
216
- connections,
217
- } ,
218
- ) )
203
+ Ok ( ( rest, Muropeptide {
204
+ polymer,
205
+ monomers,
206
+ connections,
207
+ } ) )
219
208
}
220
209
}
221
210
222
211
/// Monomer = Glycan , [ "-" , Peptide ] | Peptide ;
223
212
// FIXME: Make private again
224
213
pub fn monomer < ' c , ' a , ' p , ' s > (
225
214
polymer : & ' c RefCell < Polymer < ' a , ' p > > ,
226
- ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , Monomer > + Captures < ( & ' c ( ) , & ' a ( ) , & ' p ( ) ) > {
215
+ ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , Monomer > {
227
216
let optional_peptide = opt ( preceded ( char ( '-' ) , cut ( peptide ( polymer) ) ) ) ;
228
217
let glycan_and_peptide = map_res (
229
218
pair ( glycan ( polymer) , optional_peptide) ,
@@ -261,8 +250,7 @@ pub fn monomer<'c, 'a, 'p, 's>(
261
250
/// Glycan = { Monosaccharide }- ;
262
251
fn glycan < ' c , ' a , ' p , ' s > (
263
252
polymer : & ' c RefCell < Polymer < ' a , ' p > > ,
264
- ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , Vec < Monosaccharide > > + Captures < ( & ' c ( ) , & ' a ( ) , & ' p ( ) ) >
265
- {
253
+ ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , Vec < Monosaccharide > > {
266
254
let parser = many1 ( monosaccharide ( polymer) ) ;
267
255
map_res ( parser, |residues| {
268
256
polymer
@@ -275,7 +263,7 @@ fn glycan<'c, 'a, 'p, 's>(
275
263
/// Peptide = { Amino Acid }- ;
276
264
fn peptide < ' c , ' a , ' p , ' s > (
277
265
polymer : & ' c RefCell < Polymer < ' a , ' p > > ,
278
- ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , Vec < AminoAcid > > + Captures < ( & ' c ( ) , & ' a ( ) , & ' p ( ) ) > {
266
+ ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , Vec < AminoAcid > > {
279
267
let parser = many1 ( amino_acid ( polymer) ) ;
280
268
map_res ( parser, |residues| {
281
269
let residue_ids = residues. iter ( ) . map ( |aa| aa. residue ) ;
@@ -289,7 +277,7 @@ fn peptide<'c, 'a, 'p, 's>(
289
277
/// Monosaccharide = lowercase , [ Modifications ] ;
290
278
fn monosaccharide < ' c , ' a , ' p , ' s > (
291
279
polymer : & ' c RefCell < Polymer < ' a , ' p > > ,
292
- ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , Monosaccharide > + Captures < ( & ' c ( ) , & ' a ( ) , & ' p ( ) ) > {
280
+ ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , Monosaccharide > {
293
281
let parser = pair ( recognize ( lowercase) , opt ( modifications ( polymer) ) ) ;
294
282
map_res ( parser, |( abbr, modifications) | {
295
283
let residue = polymer. borrow_mut ( ) . new_residue ( abbr) ?;
@@ -307,7 +295,7 @@ fn monosaccharide<'c, 'a, 'p, 's>(
307
295
/// Amino Acid = Unbranched Amino Acid , [ Lateral Chain ] ;
308
296
fn amino_acid < ' c , ' a , ' p , ' s > (
309
297
polymer : & ' c RefCell < Polymer < ' a , ' p > > ,
310
- ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , AminoAcid > + Captures < ( & ' c ( ) , & ' a ( ) , & ' p ( ) ) > {
298
+ ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , AminoAcid > {
311
299
let parser = pair ( unbranched_amino_acid ( polymer) , opt ( lateral_chain ( polymer) ) ) ;
312
300
map_res ( parser, |( residue, lateral_chain) | {
313
301
if let Some ( LateralChain { direction, peptide } ) = & lateral_chain {
@@ -351,8 +339,7 @@ fn amino_acid<'c, 'a, 'p, 's>(
351
339
/// { { " " } , "," , { " " } , Any Modification } , ")" ;
352
340
fn modifications < ' c , ' a , ' p , ' s > (
353
341
polymer : & ' c RefCell < Polymer < ' a , ' p > > ,
354
- ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , Vec < ModificationId > > + Captures < ( & ' c ( ) , & ' a ( ) , & ' p ( ) ) >
355
- {
342
+ ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , Vec < ModificationId > > {
356
343
let separator = delimited ( space0, char ( ',' ) , space0) ;
357
344
delimited (
358
345
char ( '(' ) ,
@@ -364,8 +351,7 @@ fn modifications<'c, 'a, 'p, 's>(
364
351
/// Unbranched Amino Acid = [ lowercase ] , uppercase , [ Modifications ] ;
365
352
fn unbranched_amino_acid < ' c , ' a , ' p , ' s > (
366
353
polymer : & ' c RefCell < Polymer < ' a , ' p > > ,
367
- ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , UnbranchedAminoAcid > + Captures < ( & ' c ( ) , & ' a ( ) , & ' p ( ) ) >
368
- {
354
+ ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , UnbranchedAminoAcid > {
369
355
let abbr = recognize ( preceded ( opt ( lowercase) , uppercase) ) ;
370
356
let parser = pair ( abbr, opt ( modifications ( polymer) ) ) ;
371
357
map_res ( parser, |( abbr, modifications) | {
@@ -385,7 +371,7 @@ fn unbranched_amino_acid<'c, 'a, 'p, 's>(
385
371
/// Lateral Chain = "[" , Peptide Direction , { Unbranched Amino Acid }- , "]" ;
386
372
fn lateral_chain < ' c , ' a , ' p , ' s > (
387
373
polymer : & ' c RefCell < Polymer < ' a , ' p > > ,
388
- ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , LateralChain > + Captures < ( & ' c ( ) , & ' a ( ) , & ' p ( ) ) > {
374
+ ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , LateralChain > {
389
375
let peptide = many1 ( unbranched_amino_acid ( polymer) ) ;
390
376
let parser = delimited ( char ( '[' ) , peptide, char ( ']' ) ) ;
391
377
map ( parser, |peptide| LateralChain {
@@ -417,15 +403,15 @@ fn identifier(i: &str) -> ParseResult<&str> {
417
403
/// Any Modification = Named Modification | Offset Modification
418
404
pub fn any_modification < ' c , ' a , ' p , ' s > (
419
405
polymer : & ' c RefCell < Polymer < ' a , ' p > > ,
420
- ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , ModificationId > + Captures < ( & ' c ( ) , & ' a ( ) , & ' p ( ) ) > {
406
+ ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , ModificationId > {
421
407
alt ( ( named_modification ( polymer) , offset_modification ( polymer) ) )
422
408
}
423
409
424
410
// FIXME: I probably need to add a lot of `wrap_err`s around these parsers!
425
411
/// Named Modification = [ Multiplier ] , Identifier
426
412
pub fn named_modification < ' c , ' a , ' p , ' s > (
427
413
polymer : & ' c RefCell < Polymer < ' a , ' p > > ,
428
- ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , ModificationId > + Captures < ( & ' c ( ) , & ' a ( ) , & ' p ( ) ) > {
414
+ ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , ModificationId > {
429
415
let parser = pair ( opt ( multiplier) , identifier) ;
430
416
map_res ( parser, |( multiplier, named_mod) | {
431
417
polymer
@@ -439,7 +425,7 @@ pub fn named_modification<'c, 'a, 'p, 's>(
439
425
/// Chemical Composition ;
440
426
pub fn offset_modification < ' c , ' a , ' p , ' s > (
441
427
polymer : & ' c RefCell < Polymer < ' a , ' p > > ,
442
- ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , ModificationId > + Captures < ( & ' c ( ) , & ' a ( ) , & ' p ( ) ) > {
428
+ ) -> impl FnMut ( & ' s str ) -> ParseResult < ' s , ModificationId > {
443
429
let chemical_composition = chemical_composition ( polymer. borrow ( ) . atomic_db ( ) ) ;
444
430
let parser = tuple ( ( offset_kind, opt ( multiplier) , chemical_composition) ) ;
445
431
@@ -535,7 +521,9 @@ pub enum ConstructionError {
535
521
PolychemError ( #[ from] Box < PolychemError > ) ,
536
522
537
523
// FIXME: Eventually make this error either more informative (printing the number of each), or replace it!
538
- #[ error( "the number of described crosslinks (e.g. 3-3) doesn't match the number of crosslink connectors (=)" ) ]
524
+ #[ error(
525
+ "the number of described crosslinks (e.g. 3-3) doesn't match the number of crosslink connectors (=)"
526
+ ) ]
539
527
CrosslinkCountMismatch ,
540
528
541
529
// FIXME: Eventually make this error either more informative, or replace it!
@@ -553,7 +541,9 @@ pub enum ConstructionError {
553
541
554
542
#[ derive( Clone , Eq , PartialEq , Debug , Diagnostic , Error ) ]
555
543
pub enum MuropeptideErrorKind {
556
- #[ error( "expected an ASCII letter, optionally followed by any number of ASCII letters, digits, and underscores" ) ]
544
+ #[ error(
545
+ "expected an ASCII letter, optionally followed by any number of ASCII letters, digits, and underscores"
546
+ ) ]
557
547
ExpectedIdentifier ,
558
548
559
549
// FIXME: Kill this and merge into the error below!
0 commit comments