gliclass/input/text.rs
1/// Input text and labels
2pub struct TextInput {
3 pub texts: Vec<String>,
4 pub labels: Labels,
5}
6
7/// Labels, either a single list for all texts, or a list per text.
8pub enum Labels {
9 /// The same set of labels will be used for all the provided texts
10 Unique(Vec<String>),
11 /// Specify a set of label per input text (in such case the texts and labels arrays must have the same length).
12 PerText(Vec<Vec<String>>),
13}
14
15impl Labels {
16 /// Returns the list of labels for the text at the given index.
17 pub fn get(&self, text_index: usize) -> Option<&Vec<String>> {
18 match self {
19 Labels::Unique(labels) => Some(labels),
20 Labels::PerText(labels) => { labels.get(text_index) }
21 }
22 }
23}
24
25impl TextInput {
26 /// Creates an input with the same set of labels for every text
27 pub fn new(texts: Vec<String>, labels: Vec<String>) -> Self {
28 Self {
29 texts,
30 labels: Labels::Unique(labels),
31 }
32 }
33
34 /// Creates an input with the same set of labels for every text
35 pub fn from_str(texts: &[&str], labels: &[&str]) -> Self {
36 Self::new(
37 texts.iter().map(ToString::to_string).collect(),
38 labels.iter().map(ToString::to_string).collect(),
39 )
40 }
41
42 /// Creates an input with a different set of labels for each text.
43 /// The texts and labels arrays must have the same length, otherwise an error will be raised at some point.
44 pub fn new_per_text(texts: Vec<String>, labels: Vec<Vec<String>>) -> Self {
45 Self {
46 texts,
47 labels: Labels::PerText(labels),
48 }
49 }
50
51 /// Creates an input with a different set of labels for each text.
52 /// The texts and labels arrays must have the same length, otherwise an error will be raised at some point.
53 pub fn from_str_per_text(texts: &[&str], labels: &[&[&str]]) -> Self {
54 Self::new_per_text(
55 texts.iter().map(ToString::to_string).collect(),
56 labels.iter().map(|inner| inner.iter().map(ToString::to_string).collect()).collect(),
57 )
58 }
59}