aimx/inference/
output_format.rs1use crate::{
2 aim::{Prefix, Writer},
3 values::Value,
4};
5use serde::{Deserialize, Serialize};
6use std::fmt;
7
8#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
10pub enum Capability {
11 Minimal,
13 Limited,
15 Standard,
17 Markdown,
19}
20
21impl fmt::Display for Capability {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 match self {
24 Capability::Minimal => write!(f, "minimal"),
25 Capability::Limited => write!(f, "limited"),
26 Capability::Standard => write!(f, "standard"),
27 Capability::Markdown => write!(f, "markdown"),
28 }
29 }
30}
31
32impl Capability {
33 pub fn new(capability: &str) -> Self {
35 match capability {
36 "minimal" => Capability::Minimal,
37 "limited" => Capability::Limited,
38 "markdown" => Capability::Limited,
39 _ => Capability::Standard,
40 }
41 }
42}
43
44pub struct OutputFormat {
46 writer: Writer,
48 #[allow(dead_code)]
50 ordered: Prefix,
51 unordered: Prefix,
53 section_prefix: &'static str,
55 section_suffix: &'static str,
57 delimiter: &'static str,
59 #[allow(dead_code)]
61 multiline: &'static str,
62 suffix: &'static str,
64}
65
66impl OutputFormat {
67 pub fn new(capability: &Capability) -> Self {
69 match capability {
70 Capability::Minimal => OutputFormat {
71 writer: Writer::stringizer(),
72
73 ordered: Prefix::None,
74 unordered: Prefix::None,
75 section_prefix: "[",
76 section_suffix: "]\n",
77 delimiter: "\n",
78 multiline: "\n",
79 suffix: "\n",
80 },
81 Capability::Limited => OutputFormat {
82 writer: Writer::stringizer(),
83
84 ordered: Prefix::Unordered,
85 unordered: Prefix::Unordered,
86 section_prefix: "===",
87 section_suffix: "===\n",
88 delimiter: ":\n",
89 multiline: ":\n",
90 suffix: "\n",
91 },
92 Capability::Standard => OutputFormat {
93 writer: Writer::stringizer(),
94
95 ordered: Prefix::Ordered,
96 unordered: Prefix::Unordered,
97 section_prefix: "===",
98 section_suffix: "===\n",
99 delimiter: ": ",
100 multiline: ":\n",
101 suffix: "\n",
102 },
103 Capability::Markdown => OutputFormat {
104 writer: Writer::stringizer(),
105
106 ordered: Prefix::Ordered,
107 unordered: Prefix::Unordered,
108 section_prefix: "## ",
109 section_suffix: "\n",
110 delimiter: ": ",
111 multiline: ":\n",
112 suffix: "\n",
113 },
114 }
118 }
119
120 pub fn section_start(&mut self, string: &str) {
122 self.writer.write_str(self.section_prefix);
123 self.writer.write_str(string);
124 self.writer.write_str(self.section_suffix);
125 }
126
127 pub fn section_end(&mut self) {
129 self.writer.write_str(self.suffix);
130 }
131
132 pub fn single_item(&mut self, key: &str, text: &str) {
134 if self.unordered == Prefix::Unordered {
135 self.writer.write_str("- ");
136 }
137 self.writer.write_str(key);
138 self.writer.write_str(self.delimiter);
139 self.writer.write_str(text);
140 self.writer.write_str(self.suffix);
141 }
142
143 pub fn single_line(&mut self, key: &str, text: &str) {
145 self.writer.write_str(key);
146 self.writer.write_str(self.delimiter);
147 self.writer.write_str(text);
148 self.writer.write_str(self.suffix);
149 }
150
151 pub fn single_line_quoted(&mut self, key: &str, text: &str) {
153 self.writer.write_str(key);
154 self.writer.write_str(self.delimiter);
155 if text.contains('"') {
156 self.writer.write_char('\'');
157 self.writer.write_str(text);
158 self.writer.write_char('\'');
159 } else {
160 self.writer.write_char('"');
161 self.writer.write_str(text);
162 self.writer.write_char('"');
163 }
164 self.writer.write_str(self.suffix);
165 }
166
167 pub fn single_line_tagged(&mut self, key: &str, text: &str) {
169 self.writer.write_str(key);
170 self.writer.write_str(self.delimiter);
171 self.writer.write_char('<');
172 self.writer.write_str(text);
173 self.writer.write_char('>');
174 self.writer.write_str(self.suffix);
175 }
176
177 pub fn value(&mut self, value: &Value) {
179 value.pretty_print(&Prefix::None, &mut self.writer);
180 self.writer.write_str("\n");
181 }
182
183 pub fn text(&mut self, string: &str) {
185 self.writer.write_str(string);
186 self.writer.write_str("\n");
187 }
188
189 #[allow(dead_code)]
190 pub fn ordered_value(&mut self, value: &Value) {
192 value.pretty_print(&self.ordered, &mut self.writer);
193 self.writer.write_str("\n");
194 }
195
196 pub fn unordered_value(&mut self, value: &Value) {
198 value.pretty_print(&self.unordered, &mut self.writer);
199 self.writer.write_str("\n");
200 }
201
202 pub fn finish(self) -> String {
204 self.writer.finish()
205 }
206}