beancount-parser-lima 0.16.2

A zero-copy parser for Beancount
Documentation
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
use ariadne::{Color, Label};
use glob::{self, glob_with};
use lazy_format::lazy_format;
use std::{
    collections::{HashMap, HashSet, VecDeque},
    ffi::OsStr,
    fmt::{self, Formatter},
    fs::File,
    io::{self, Read, Write},
    iter::once,
    path::{Path, PathBuf},
};

pub use crate::{trim::trim_trailing_whitespace, types::*};

use crate::{IncludedGlob, SourceId, get_includes};

/// Contains the content of the Beancount source file, and the content of
/// the transitive closure of all the include'd source files.
///
/// Zero-copy parsing means that all string values are returned as references into these strings.
///
/// # Examples
/// ```
/// # use std::path::PathBuf;
/// use beancount_parser_lima::{BeancountParser, BeancountSources};
///
/// let sources = BeancountSources::try_from(PathBuf::from("examples/data/full.beancount")).unwrap();
/// let beancount_parser = BeancountParser::new(&sources);
///
/// let result = beancount_parser.parse();
/// ```
#[derive(Clone)]
pub struct BeancountSources {
    root_path: Option<PathBuf>,
    root_source_id: SourceId,
    root_content: String,
    root_content_char_indices: Vec<usize>,
    included_globs: HashMap<PathBuf, IncludedGlob>,
    included_content: HashMap<PathBuf, IncludedSource>,
    source_id_strings: Vec<String>, // indexed by SourceId
}

#[derive(Clone, Debug)]
enum IncludedSource {
    Content(SourceId, String, Vec<usize>), // the content and its char indices
    Error(String),
    Duplicate,
}

impl BeancountSources {
    fn try_read_with_includes(root_path: PathBuf) -> io::Result<Self> {
        let root_content = read(&root_path)?;
        Ok(Self::read_with_includes(Some(root_path), root_content))
    }

    fn read_with_includes(root_path: Option<PathBuf>, root_content: String) -> Self {
        let root_source_id = SourceId::default();
        let root_source_id_string = root_path
            .as_ref()
            .map(|p| p.to_string_lossy().into())
            .unwrap_or("inline".to_string());
        let mut source_id_strings = Vec::from([root_source_id_string]);

        let mut pending_includes = get_includes(&root_content, root_source_id)
            .into_iter()
            .map(|included| resolve_included_path(root_path.as_ref(), included.as_ref()))
            .collect::<VecDeque<_>>();

        let mut included_globs = HashMap::new();
        let mut included_content: HashMap<PathBuf, IncludedSource> = HashMap::new();

        // for duplicate detection
        let mut canonical_paths =
            if let Some(canonical_root) = root_path.as_ref().and_then(|p| p.canonicalize().ok()) {
                HashSet::from([canonical_root])
            } else {
                HashSet::default()
            };

        while !pending_includes.is_empty() {
            let included = pending_includes.pop_front().unwrap();
            let included_str = included.to_string_lossy();
            let included_str = included_str.as_ref();

            match glob_with(
                included_str,
                glob::MatchOptions {
                    case_sensitive: true,
                    require_literal_separator: true,
                    require_literal_leading_dot: true,
                },
            ) {
                Err(e) => {
                    included_globs.insert(included, IncludedGlob::Error(e.to_string()));
                }
                Ok(globbed_includes) => {
                    let mut glob_expansions = Vec::default();

                    for globbed_include in globbed_includes {
                        match globbed_include {
                            Err(e) => {
                                let path = e.path().to_path_buf();
                                glob_expansions.push(path.clone());
                                included_content.insert(path, IncludedSource::Error(e.to_string()));
                            }
                            Ok(globbed_include) => {
                                glob_expansions.push(globbed_include.clone());

                                if let Ok(canonical_path) = globbed_include.canonicalize() {
                                    if canonical_paths.contains(&canonical_path) {
                                        // don't overwrite existing content
                                        included_content
                                            .entry(globbed_include)
                                            .or_insert(IncludedSource::Duplicate);
                                    } else {
                                        canonical_paths.insert(canonical_path);

                                        let source_id = SourceId::from(source_id_strings.len());
                                        source_id_strings
                                            .push(globbed_include.to_string_lossy().into());

                                        let included_source = read(&globbed_include).map_or_else(
                                            |e| {
                                                IncludedSource::Error(format!(
                                                    "{}: {}",
                                                    globbed_include.to_string_lossy(),
                                                    e
                                                ))
                                            },
                                            |c| {
                                                // find the char indices for the content
                                                // needed for mapping byte indices to char indices, to convert Chumsky spans to Ariadne spans
                                                // see https://github.com/zesterer/chumsky/issues/65#issuecomment-1689216633
                                                let char_indices = c
                                                    .char_indices()
                                                    .map(|(i, _)| i)
                                                    .collect::<Vec<_>>();
                                                IncludedSource::Content(source_id, c, char_indices)
                                            },
                                        );

                                        // stabilisation of VacantEntry::insert_entry() would enable us to avoid cloning the path here
                                        // and doing an immediate lookup
                                        included_content
                                            .insert(globbed_include.clone(), included_source);
                                        let included_source =
                                            included_content.get(&globbed_include).unwrap();

                                        if let IncludedSource::Content(_, content, _) =
                                            included_source
                                        {
                                            let mut includes = get_includes(content, source_id)
                                                .into_iter()
                                                .map(|included_path| {
                                                    resolve_included_path(
                                                        Some(&globbed_include),
                                                        included_path.as_ref(),
                                                    )
                                                })
                                                .collect::<VecDeque<_>>();
                                            pending_includes.append(&mut includes);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    included_globs.insert(included, IncludedGlob::Expanded(glob_expansions));
                }
            }
        }

        let root_content_char_indices = root_content
            .char_indices()
            .map(|(i, _)| i)
            .collect::<Vec<_>>();

        Self {
            root_path,
            root_source_id,
            root_content,
            root_content_char_indices,
            included_globs,
            included_content,
            source_id_strings,
        }
    }

    #[deprecated(since = "0.12.0", note = "Use `write_errors_or_warnings` instead")]
    pub fn write<W, E, K>(&self, w: &mut W, errors_or_warnings: Vec<E>) -> io::Result<()>
    where
        W: Write,
        E: Into<AnnotatedErrorOrWarning<K>>,
        K: ErrorOrWarningKind,
    {
        self.write_errors_or_warnings(w, errors_or_warnings)
    }

    /// Write human-readable error reports.
    pub fn write_errors_or_warnings<W, E, K>(
        &self,
        w: &mut W,
        errors_or_warnings: Vec<E>,
    ) -> io::Result<()>
    where
        W: Write,
        E: Into<AnnotatedErrorOrWarning<K>>,
        K: ErrorOrWarningKind,
    {
        for error_or_warning in errors_or_warnings.into_iter() {
            let AnnotatedErrorOrWarning {
                error_or_warning,
                annotation,
            } = error_or_warning.into();

            self.write_report::<W, K, ErrorOrWarning<K>>(w, &error_or_warning)?;

            if let Some(annotation) = annotation {
                // clippy thinks this is better than write! 🤷
                w.write_fmt(core::format_args!("{}\n", &annotation))?;
            }
        }
        Ok(())
    }

    /// Write human-readable error or warning report.
    pub fn write_report<W, K, R>(&self, w: &mut W, report: &R) -> io::Result<()>
    where
        W: Write,
        K: ErrorOrWarningKind,
        R: Report,
    {
        write_report::<W, K, R, _>(
            w,
            report,
            &|span| self.get_adjusted_source(span),
            self.sources(),
        )
    }

    /// Resolve the span into filename, line number range, and spanned content.
    /// Filename will be present unless the sources were created from an inline string.
    pub fn resolve_span<'a>(&'a self, span: &Span) -> SpannedSource<'a> {
        let (source_content, source_id_str, byte_span, rune_span) = self.get_adjusted_source(*span);

        let file_name = if Into::<SourceId>::into(span.source) == self.root_source_id {
            self.root_path.as_ref().and(Some(source_id_str))
        } else {
            Some(source_id_str)
        };

        let mut source_chars = source_content.chars();
        let start_line = source_chars
            .by_ref()
            .take(rune_span.start)
            .filter(|c| *c == '\n')
            .count()
            + 1;
        let lines_spanned = source_chars
            .by_ref()
            .take(rune_span.end - rune_span.start)
            .filter(|c| *c == '\n')
            .count();
        let end_line = start_line + lines_spanned;

        SpannedSource {
            file_name,
            start_line,
            end_line,
            content: source_content
                .get(byte_span.start..byte_span.end)
                .unwrap_or(""),
        }
    }

    fn byte_to_rune(&self, char_indices: &[usize], byte_span: Span) -> Span {
        let mut rune_span = byte_span;
        rune_span.start = char_indices.partition_point(|&i| i < byte_span.start);
        rune_span.end = char_indices.partition_point(|&i| i < byte_span.end);
        rune_span
    }

    pub fn error_source_text<'a, K>(&'a self, error_or_warning: &ErrorOrWarning<K>) -> &'a str
    where
        K: ErrorOrWarningKind,
    {
        let (source_content, _, byte_span, _rune_span) =
            self.get_adjusted_source(error_or_warning.0.span);
        &source_content[byte_span.start..byte_span.end]
    }

    fn get_adjusted_source(&self, span: Span) -> (&str, &str, Span, Span) {
        let safe_span = if span.source >= self.source_id_strings.len() {
            // bad source collapses down to empty span,
            // because we don't really have a good way to reject that
            // and at least we mustn't panic
            Span {
                source: self.root_source_id.into(),
                start: 0,
                end: 0,
            }
        } else {
            span
        };
        let source_id = safe_span.source.into();
        let source_id_str = self.source_id_string(source_id);
        let empty_char_indices = Vec::default();
        let (source_content, source_content_char_indices) = if source_id == self.root_source_id {
            (self.root_content.as_str(), &self.root_content_char_indices)
        } else if let IncludedSource::Content(_, content, content_char_indices) =
            self.included_content.get(Path::new(source_id_str)).unwrap()
        {
            (content.as_str(), content_char_indices)
        } else {
            ("", &empty_char_indices)
        };

        let byte_span = trimmed_span(source_content, safe_span);
        let rune_span = byte_to_rune(source_content_char_indices, byte_span);

        (source_content, source_id_str, byte_span, rune_span)
    }

    fn source_id_string(&self, source_id: SourceId) -> &str {
        self.source_id_strings[Into::<usize>::into(source_id)].as_str()
    }

    fn sources(&self) -> Vec<(String, &str)> {
        once((
            self.source_id_string(self.root_source_id).to_string(),
            self.root_content.as_str(),
        ))
        .chain(
            self.included_content
                .iter()
                .filter_map(|(_, included_source)| {
                    if let IncludedSource::Content(source_id, content, _) = included_source {
                        Some((
                            self.source_id_string(*source_id).to_string(),
                            content.as_str(),
                        ))
                    } else {
                        None
                    }
                }),
        )
        .collect()
    }

    pub(crate) fn content_iter(&self) -> impl Iterator<Item = (SourceId, Option<&Path>, &str)> {
        once((
            self.root_source_id,
            self.root_path.as_deref(),
            self.root_content.as_str(),
        ))
        .chain(
            self.included_content
                .iter()
                .filter_map(|(pathbuf, included_source)| {
                    if let IncludedSource::Content(source_id, content, _) = included_source {
                        Some((*source_id, Some(pathbuf.as_path()), content.as_str()))
                    } else {
                        None
                    }
                }),
        )
    }

    /// Number of real sources, excluding synthetic ones
    pub(crate) fn num_sources(&self) -> usize {
        self.source_id_strings.len()
    }

    pub(crate) fn root_path(&self) -> Option<&Path> {
        self.root_path.as_deref()
    }

    pub(crate) fn included_globs(&self) -> &HashMap<PathBuf, IncludedGlob> {
        &self.included_globs
    }

    pub(crate) fn error_paths(&self) -> HashMap<Option<&Path>, String> {
        self.included_content
            .iter()
            .filter_map(|(pathbuf, included_source)| {
                if let IncludedSource::Error(e) = included_source {
                    Some((Some(pathbuf.as_path()), e.clone()))
                } else {
                    None
                }
            })
            .collect::<HashMap<_, _>>()
    }
}

impl TryFrom<PathBuf> for BeancountSources {
    type Error = io::Error;

    fn try_from(source_path: PathBuf) -> io::Result<Self> {
        Self::try_read_with_includes(source_path)
    }
}

impl TryFrom<&Path> for BeancountSources {
    type Error = io::Error;

    fn try_from(source_path: &Path) -> io::Result<Self> {
        Self::try_read_with_includes(source_path.to_owned())
    }
}

impl From<String> for BeancountSources {
    fn from(source_string: String) -> Self {
        Self::read_with_includes(None, source_string)
    }
}

impl From<&str> for BeancountSources {
    fn from(source_string: &str) -> Self {
        Self::read_with_includes(None, source_string.to_owned())
    }
}

impl std::fmt::Debug for BeancountSources {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        writeln!(f, "BeancountSources(",)?;

        for (path, included_source) in &self.included_content {
            match included_source {
                IncludedSource::Content(source_id, content, _) => writeln!(
                    f,
                    "    {} ok len {},",
                    self.source_id_string(*source_id),
                    content.len()
                )?,
                IncludedSource::Error(e) => writeln!(f, "    {:?} err {},", path, e)?,
                IncludedSource::Duplicate => writeln!(f, "    {:?} duplicate include", path)?,
            }
        }

        writeln!(f, ")",)
    }
}

#[derive(Clone)]
pub struct SyntheticSources<'a> {
    sources: &'a BeancountSources,
    base_id: usize,
    content: HashMap<String, (SourceId, String, Vec<usize>)>, // content and char indices, indexed by source name
    source_id_strings: Vec<String>,                           // indexed by SourceId - base_id
}

impl<'a> SyntheticSources<'a> {
    pub fn new(sources: &'a BeancountSources) -> Self {
        SyntheticSources {
            sources,
            base_id: sources.num_sources(),
            content: HashMap::default(),
            source_id_strings: Vec::default(),
        }
    }
}

impl<'a> SyntheticSources<'a> {
    fn sources(&self) -> Vec<(String, &str)> {
        let mut sources = self.sources.sources();
        sources.extend(
            self.content.iter().map(|(source_id_str, (_, content, _))| {
                (source_id_str.to_string(), content.as_str())
            }),
        );
        sources
    }

    /// A synthetic span is a content fragment which doesn't occur in the original sources, but
    /// may be referred to in error reports.  Multiple fragments may share the same source name.
    pub fn create_synthetic_span(&mut self, source_name: &str, content_fragment: &str) -> Span {
        if let Some((source_id, content, char_indices)) = self.content.get_mut(source_name) {
            let start = content.len();
            let end = start + content_fragment.len();
            let span = Span {
                source: (*source_id).into(),
                start,
                end,
            };

            let original_len = char_indices.len();
            char_indices.extend(
                content_fragment
                    .char_indices()
                    .map(|(i, _)| i + original_len),
            );
            content.push_str(content_fragment);

            span
        } else {
            let source = self.source_id_strings.len() + self.base_id;
            self.source_id_strings.push(source_name.to_string());

            let span = Span {
                source,
                start: 0,
                end: content_fragment.len(),
            };

            let content = content_fragment.to_string();
            let char_indices = content.char_indices().map(|(i, _)| i).collect::<Vec<_>>();
            self.content.insert(
                source_name.to_string(),
                (source.into(), content, char_indices),
            );

            span
        }
    }

    /// Write human-readable error reports.
    pub fn write_errors_or_warnings<W, E, K>(
        &self,
        w: &mut W,
        errors_or_warnings: Vec<E>,
    ) -> io::Result<()>
    where
        W: Write,
        E: Into<AnnotatedErrorOrWarning<K>>,
        K: ErrorOrWarningKind,
    {
        for error_or_warning in errors_or_warnings.into_iter() {
            let AnnotatedErrorOrWarning {
                error_or_warning,
                annotation,
            } = error_or_warning.into();

            self.write_report::<W, K, ErrorOrWarning<K>>(w, &error_or_warning)?;

            if let Some(annotation) = annotation {
                // clippy thinks this is better than write! 🤷
                w.write_fmt(core::format_args!("{}\n", &annotation))?;
            }
        }
        Ok(())
    }

    /// Write human-readable error or warning report.
    pub fn write_report<W, K, R>(&self, w: &mut W, report: &R) -> io::Result<()>
    where
        W: Write,
        K: ErrorOrWarningKind,
        R: Report,
    {
        write_report::<W, K, R, _>(
            w,
            report,
            &|span| self.get_adjusted_source(span),
            self.sources(),
        )
    }

    /// Resolve the span into filename, line number range, and spanned content.
    /// Filename will be present unless the sources were created from an inline string.
    pub fn resolve_span<'s>(&'s self, span: &Span) -> SpannedSource<'s> {
        resolve_span(*span, &|span| self.get_adjusted_source(span), true)
    }

    pub fn error_source_text<'s, K>(&'s self, error_or_warning: &ErrorOrWarning<K>) -> &'s str
    where
        K: ErrorOrWarningKind,
    {
        let (source_content, _, byte_span, _rune_span) =
            self.get_adjusted_source(error_or_warning.0.span);
        &source_content[byte_span.start..byte_span.end]
    }

    fn get_adjusted_source(&self, span: Span) -> (&str, &str, Span, Span) {
        if span.source >= self.base_id && span.source < self.base_id + self.source_id_strings.len()
        {
            let source_id_str = self.source_id_strings[span.source - self.base_id].as_str();

            let (_, content, content_char_indices) = self.content.get(source_id_str).unwrap();
            let content = content.as_str();

            let byte_span = trimmed_span(content, span);
            let rune_span = byte_to_rune(content_char_indices, byte_span);

            (content, source_id_str, byte_span, rune_span)
        } else {
            self.sources.get_adjusted_source(span)
        }
    }
}

// get included path relative to including path
pub(crate) fn resolve_included_path(
    including_path: Option<&PathBuf>,
    included_path: &Path,
) -> PathBuf {
    match including_path.and_then(|p| path_dir(p.as_ref())) {
        Some(p) => p.join(included_path),
        None => included_path.to_path_buf(),
    }
}

// get directory for a path if any
fn path_dir(p: &Path) -> Option<&Path> {
    p.parent().and_then(|p| {
        if !AsRef::<OsStr>::as_ref(&p).is_empty() {
            Some(p)
        } else {
            None
        }
    })
}

fn read<P>(file_path: P) -> io::Result<String>
where
    P: AsRef<Path>,
{
    let mut f = File::open(&file_path)?;
    let mut file_content = String::new();

    // read the whole file
    f.read_to_string(&mut file_content)?;
    Ok(file_content)
}

fn write_report<'a, W, K, R, F>(
    w: &mut W,
    report: &R,
    get_adjusted_source: &F,
    sources: Vec<(String, &str)>,
) -> io::Result<()>
where
    W: Write,
    K: ErrorOrWarningKind,
    R: Report,
    F: Fn(Span) -> (&'a str, &'a str, Span, Span),
{
    let (src_id, span) =
        source_id_string_and_adjusted_rune_span(report.span(), get_adjusted_source);
    let color = K::color();
    let report_kind = K::report_kind();

    ariadne::Report::build(report_kind, (src_id.clone(), (span.start..span.end)))
        .with_message(report.message())
        .with_labels(Some(
            Label::new((src_id, (span.start..span.end)))
                .with_message(report.reason())
                .with_color(color),
        ))
        .with_labels(report.contexts().map(|(label, span)| {
            let (src_id, span) = source_id_string_and_adjusted_rune_span(span, get_adjusted_source);
            Label::new((src_id, (span.start..span.end)))
                .with_message(lazy_format!("in this {}", label))
                .with_color(Color::Yellow)
        }))
        .with_labels(report.related().map(|(label, span)| {
            let (src_id, span) = source_id_string_and_adjusted_rune_span(span, get_adjusted_source);
            Label::new((src_id, (span.start..span.end)))
                .with_message(lazy_format!("{}", label))
                .with_color(Color::Yellow)
        }))
        .finish()
        .write(ariadne::sources(sources), w)
}

/// Resolve the span into filename, line number range, and spanned content.
/// Filename will be present unless the sources were created from an inline string.
fn resolve_span<'a, F>(
    span: Span,
    get_adjusted_source: &F,
    source_id_is_file_name: bool,
) -> SpannedSource<'a>
where
    F: Fn(Span) -> (&'a str, &'a str, Span, Span),
{
    let (source_content, source_id_str, byte_span, rune_span) = get_adjusted_source(span);

    let mut source_chars = source_content.chars();
    let start_line = source_chars
        .by_ref()
        .take(rune_span.start)
        .filter(|c| *c == '\n')
        .count()
        + 1;
    let lines_spanned = source_chars
        .by_ref()
        .take(rune_span.end - rune_span.start)
        .filter(|c| *c == '\n')
        .count();
    let end_line = start_line + lines_spanned;

    SpannedSource {
        file_name: source_id_is_file_name.then_some(source_id_str),
        start_line,
        end_line,
        content: source_content
            .get(byte_span.start..byte_span.end)
            .unwrap_or(""),
    }
}

fn source_id_string_and_adjusted_rune_span<'a, F>(
    span: Span,
    get_adjusted_source: &F,
) -> (String, Span)
where
    F: Fn(Span) -> (&'a str, &'a str, Span, Span),
{
    let (_, source_id, _byte_span, rune_span) = get_adjusted_source(span);
    (source_id.to_string(), rune_span)
}

fn trimmed_span(source: &str, span: Span) -> Span {
    let mut trimmed = span;

    // invalid spans fall back to nothing
    if source.get(span.start..span.end).is_none() {
        trimmed.start = 0;
        trimmed.end = 0;
    } else {
        trimmed.end = trim_trailing_whitespace(source, span.start, span.end);
    }
    trimmed
}

fn byte_to_rune(char_indices: &[usize], byte_span: Span) -> Span {
    let mut rune_span = byte_span;
    rune_span.start = char_indices.partition_point(|&i| i < byte_span.start);
    rune_span.end = char_indices.partition_point(|&i| i < byte_span.end);
    rune_span
}