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
//! Text stored as source.
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
/// A piece of text, stored as its source so that renderers typeset it lazily.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Text {
/// The source text. With the LaTeX interpreter, segments delimited by `$…$`
/// are typeset as mathematics and the remainder as plain text.
pub content: String,
/// How the source text is interpreted.
pub interpreter: Interpreter,
}
/// How the source of a [`Text`] is interpreted.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum Interpreter {
/// Mixed plain text and `$…$` LaTeX mathematics.
#[default]
Latex,
/// Plain text, rendered literally, including any dollar signs.
None,
}
impl Text {
/// Creates text with the default (LaTeX) interpreter.
pub fn new(content: impl Into<String>) -> Self {
Self {
content: content.into(),
interpreter: Interpreter::Latex,
}
}
/// Creates text that is rendered literally, without interpreting mathematics.
pub fn plain(content: impl Into<String>) -> Self {
Self {
content: content.into(),
interpreter: Interpreter::None,
}
}
}
impl From<&str> for Text {
fn from(content: &str) -> Self {
Self::new(content)
}
}
impl From<String> for Text {
fn from(content: String) -> Self {
Self::new(content)
}
}