rig_core/wire/citation.rs
1//! Citations as a provider states them, before the completion fold resolves
2//! their spans. A decoder hands each one to
3//! [`Out::cite`](crate::wire::Out::cite) with the unit its provider counts
4//! offsets in, so no decoder converts a unit or builds a byte span itself.
5
6use crate::message::Source;
7
8/// A citation as the provider stated it.
9#[non_exhaustive]
10#[derive(Clone, Debug, PartialEq)]
11pub struct WireCitation {
12 /// The part of the text it covers; `None` for the whole block.
13 pub span: Option<WireSpan>,
14 /// What supports the claim.
15 pub sources: Vec<Source>,
16}
17
18impl WireCitation {
19 /// A citation of `span` (or, with `None`, the whole block) by `sources`.
20 pub fn new(span: Option<WireSpan>, sources: Vec<Source>) -> Self {
21 Self { span, sources }
22 }
23}
24
25/// Offsets into a text block as the provider counts them.
26#[non_exhaustive]
27#[derive(Clone, Debug, PartialEq, Eq)]
28pub struct WireSpan {
29 /// The first offset.
30 pub start: u64,
31 /// The offset after the last.
32 pub end: u64,
33 /// What the offsets count.
34 pub unit: SpanUnit,
35 /// The text the provider says the span covers, which the resolved span
36 /// must match.
37 pub quoted: Option<String>,
38}
39
40impl WireSpan {
41 /// `start..end` counted in `unit`, with no quoted text.
42 pub fn new(start: u64, end: u64, unit: SpanUnit) -> Self {
43 Self {
44 start,
45 end,
46 unit,
47 quoted: None,
48 }
49 }
50
51 /// Set the text the provider says the span covers.
52 pub fn quoted(mut self, text: impl Into<String>) -> Self {
53 self.quoted = Some(text.into());
54 self
55 }
56}
57
58/// What a provider's offsets count.
59#[non_exhaustive]
60#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
61pub enum SpanUnit {
62 /// UTF-8 bytes.
63 Bytes,
64 /// Unicode scalar values.
65 Chars,
66 /// UTF-16 code units.
67 Utf16,
68}