1use std::cell::UnsafeCell;
2use std::mem;
3
4use super::Arena;
5use super::condition_resolvers;
6use super::printer::Printer;
7use super::thread_state;
8
9pub struct PrintItems<'a> {
11 arena: &'a Arena,
12 pub(super) first_node: Option<PrintItemPath<'a>>,
13 last_node: Option<PrintItemPath<'a>>,
14}
15
16impl<'a> PrintItems<'a> {
17 pub fn new(arena: &'a Arena) -> Self {
18 Self {
19 arena,
20 first_node: None,
21 last_node: None,
22 }
23 }
24
25 #[inline]
27 pub fn arena(&self) -> &'a Arena {
28 self.arena
29 }
30
31 pub fn into_rc_path(self) -> Option<PrintItemPath<'a>> {
32 self.first_node
33 }
34
35 pub fn push_item(&mut self, item: PrintItem<'a>) {
36 self.push_item_internal(item);
37 }
38
39 #[inline]
40 fn push_item_internal(&mut self, item: PrintItem<'a>) {
41 let node = self.arena.alloc_print_node_cell(PrintNodeCell::new(item));
42 if let Some(first_node) = &self.first_node {
43 let new_last_node = node.get_last_next().unwrap_or(node);
44 self.last_node.as_ref().unwrap_or(first_node).set_next(Some(node));
45 self.last_node = Some(new_last_node);
46 } else {
47 self.last_node = node.get_last_next();
48 self.first_node = Some(node);
49 }
50 }
51
52 pub fn extend(&mut self, items: PrintItems<'a>) {
53 if let Some(first_node) = items.first_node {
54 if let Some(current_first_node) = &self.first_node {
55 self.last_node.as_ref().unwrap_or(current_first_node).set_next(Some(first_node));
56
57 if items.last_node.is_some() {
58 self.last_node = items.last_node;
59 } else if items.first_node.is_some() {
60 self.last_node = items.first_node;
61 }
62 } else {
63 self.first_node = items.first_node;
64 self.last_node = items.last_node;
65 }
66 }
67 }
68
69 pub fn push_str_runtime_width_computed(&mut self, item: &'a str) {
77 let string_container = self.arena.alloc_string_container(item);
78 self.push_item_internal(PrintItem::String(string_container));
79 }
80
81 pub fn push_force_current_line_indentation(&mut self) {
82 const STR_EMPTY: StringContainer = StringContainer { text: "", char_count: 0 };
83 self.push_item_internal(PrintItem::String(&STR_EMPTY))
84 }
85
86 pub fn push_space(&mut self) {
87 const STR_SPACE: StringContainer = StringContainer { text: " ", char_count: 1 };
88 self.push_item_internal(PrintItem::String(&STR_SPACE))
89 }
90
91 pub fn push_sc(&mut self, item: &'a StringContainer<'a>) {
93 self.push_item_internal(PrintItem::String(item))
94 }
95
96 pub fn push_string(&mut self, item: String) {
98 self.push_str(&item);
99 }
100
101 pub fn push_str(&mut self, item: &str) {
109 let string_container = self.arena.alloc_str(item);
110 self.push_item_internal(PrintItem::String(string_container));
111 }
112
113 pub fn push_condition(&mut self, condition: Condition<'a>) {
114 let condition = self.arena.alloc_condition(condition);
115 self.push_item_internal(PrintItem::Condition(condition));
116 }
117
118 pub fn push_info(&mut self, info: impl Into<Info>) {
119 self.push_item_internal(PrintItem::Info(info.into()));
120 }
121
122 pub fn push_line_and_column(&mut self, line_and_col: LineAndColumn) {
123 self.push_info(line_and_col.line);
124 self.push_info(line_and_col.column);
125 }
126
127 pub fn push_anchor(&mut self, anchor: impl Into<Anchor>) {
128 self.push_item_internal(PrintItem::Anchor(anchor.into()));
129 }
130
131 pub fn push_reevaluation(&mut self, condition_reevaluation: ConditionReevaluation) {
132 self.push_item_internal(PrintItem::ConditionReevaluation(condition_reevaluation));
133 }
134
135 pub fn push_signal(&mut self, signal: Signal) {
136 self.push_item_internal(PrintItem::Signal(signal));
137 }
138
139 pub fn push_path(&mut self, path: PrintItemPath<'a>) {
140 self.push_item_internal(PrintItem::RcPath(path))
141 }
142
143 pub fn push_optional_path(&mut self, path: Option<PrintItemPath<'a>>) {
144 if let Some(path) = path {
145 self.push_path(path);
146 }
147 }
148
149 pub fn is_empty(&self) -> bool {
150 self.first_node.is_none()
151 }
152
153 #[cfg(debug_assertions)]
155 pub fn get_as_text(&self) -> String {
156 return if let Some(first_node) = &self.first_node {
157 get_items_as_text(first_node, String::from(""))
158 } else {
159 String::new()
160 };
161
162 fn get_items_as_text(items: PrintItemPath, indent_text: String) -> String {
163 let mut text = String::new();
164 for item in PrintItemsIterator::new(items) {
165 match item {
166 PrintItem::Signal(signal) => text.push_str(&get_line(format!("Signal::{:?}", signal), &indent_text)),
167 PrintItem::Condition(condition) => {
168 text.push_str(&get_line(format!("Condition: {}", condition.name), &indent_text));
169 if let Some(true_path) = &condition.true_path {
170 text.push_str(&get_line(String::from(" true:"), &indent_text));
171 text.push_str(&get_items_as_text(true_path, format!("{} ", indent_text)));
172 }
173 if let Some(false_path) = &condition.false_path {
174 text.push_str(&get_line(String::from(" false:"), &indent_text));
175 text.push_str(&get_items_as_text(false_path, format!("{} ", indent_text)));
176 }
177 }
178 PrintItem::String(str_text) => text.push_str(&get_line(format!("`{}`", str_text.text), &indent_text)),
179 PrintItem::RcPath(path) => text.push_str(&get_items_as_text(path, indent_text.clone())),
180 PrintItem::Anchor(Anchor::LineNumber(line_number_anchor)) => {
181 text.push_str(&get_line(format!("Line number anchor: {}", line_number_anchor.name()), &indent_text))
182 }
183 PrintItem::Info(info) => {
184 let (desc, name) = match info {
185 Info::LineNumber(info) => ("Line number", info.name()),
186 Info::ColumnNumber(info) => ("Column number", info.name()),
187 Info::IsStartOfLine(info) => ("Is start of line", info.name()),
188 Info::IndentLevel(info) => ("Indent level", info.name()),
189 Info::LineStartColumnNumber(info) => ("Line start column number", info.name()),
190 Info::LineStartIndentLevel(info) => ("Line start indent level", info.name()),
191 };
192 text.push_str(&get_line(format!("{}: {}", desc, name), &indent_text))
193 }
194 PrintItem::ConditionReevaluation(reevaluation) => text.push_str(&get_line(format!("Condition reevaluation: {}", reevaluation.name()), &indent_text)),
195 }
196 }
197
198 return text;
199
200 fn get_line(text: String, indent_text: &str) -> String {
201 format!("{}{}\n", indent_text, text)
202 }
203 }
204 }
205
206 pub fn iter(&self) -> PrintItemsIterator<'a> {
207 PrintItemsIterator { node: self.first_node }
208 }
209}
210
211pub struct PrintItemsIterator<'a> {
212 node: Option<PrintItemPath<'a>>,
213}
214
215impl<'a> PrintItemsIterator<'a> {
216 pub fn new(path: PrintItemPath<'a>) -> Self {
217 Self { node: Some(path) }
218 }
219}
220
221impl<'a> Iterator for PrintItemsIterator<'a> {
222 type Item = PrintItem<'a>;
223
224 fn next(&mut self) -> Option<PrintItem<'a>> {
225 let node = self.node.take();
226
227 match node {
228 Some(node) => {
229 self.node = node.get_next();
230 Some(node.get_item())
231 }
232 None => None,
233 }
234 }
235}
236
237pub trait IntoPrintItems<'a> {
239 fn into_print_items(self, arena: &'a Arena) -> PrintItems<'a>;
240}
241
242impl<'a> IntoPrintItems<'a> for PrintItems<'a> {
243 fn into_print_items(self, _arena: &'a Arena) -> PrintItems<'a> {
244 self
245 }
246}
247
248impl<'a> IntoPrintItems<'a> for &'a str {
249 fn into_print_items(self, arena: &'a Arena) -> PrintItems<'a> {
250 let mut items = PrintItems::new(arena);
251 items.push_str_runtime_width_computed(self);
252 items
253 }
254}
255
256impl<'a> IntoPrintItems<'a> for &'a StringContainer<'a> {
257 fn into_print_items(self, arena: &'a Arena) -> PrintItems<'a> {
258 let mut items = PrintItems::new(arena);
259 items.push_sc(self);
260 items
261 }
262}
263
264impl<'a> IntoPrintItems<'a> for String {
265 fn into_print_items(self, arena: &'a Arena) -> PrintItems<'a> {
266 let mut items = PrintItems::new(arena);
267 items.push_string(self);
268 items
269 }
270}
271
272impl<'a> IntoPrintItems<'a> for Condition<'a> {
273 fn into_print_items(self, arena: &'a Arena) -> PrintItems<'a> {
274 let mut items = PrintItems::new(arena);
275 items.push_condition(self);
276 items
277 }
278}
279
280impl<'a> IntoPrintItems<'a> for Signal {
281 fn into_print_items(self, arena: &'a Arena) -> PrintItems<'a> {
282 let mut items = PrintItems::new(arena);
283 items.push_signal(self);
284 items
285 }
286}
287
288impl<'a> IntoPrintItems<'a> for PrintItemPath<'a> {
289 fn into_print_items(self, arena: &'a Arena) -> PrintItems<'a> {
290 let mut items = PrintItems::new(arena);
291 items.push_path(self);
292 items
293 }
294}
295
296impl<'a, T: IntoPrintItems<'a>> IntoPrintItems<'a> for Option<T> {
297 fn into_print_items(self, arena: &'a Arena) -> PrintItems<'a> {
298 match self {
299 Some(value) => value.into_print_items(arena),
300 None => PrintItems::new(arena),
301 }
302 }
303}
304
305#[cfg(feature = "tracing")]
308#[derive(serde::Serialize)]
309#[serde(rename_all = "camelCase")]
310pub struct Trace {
311 pub nanos: u128,
313 pub print_node_id: u32,
314 #[serde(skip_serializing_if = "Option::is_none")]
315 pub writer_node_id: Option<u32>,
316}
317
318#[cfg(feature = "tracing")]
319#[derive(serde::Serialize)]
320#[serde(rename_all = "camelCase")]
321pub struct TraceWriterNode {
322 pub writer_node_id: u32,
323 #[serde(skip_serializing_if = "Option::is_none")]
324 pub previous_node_id: Option<u32>,
325 pub text: String,
326}
327
328#[cfg(feature = "tracing")]
329#[derive(serde::Serialize)]
330#[serde(rename_all = "camelCase")]
331pub struct TracePrintNode {
332 pub print_node_id: u32,
333 #[serde(skip_serializing_if = "Option::is_none")]
334 pub next_print_node_id: Option<u32>,
335 pub print_item: TracePrintItem,
336}
337
338#[cfg(feature = "tracing")]
339#[derive(serde::Serialize)]
340#[serde(tag = "kind", content = "content", rename_all = "camelCase")]
341pub enum TracePrintItem {
342 String(String),
343 Condition(TraceCondition),
344 Info(TraceInfo),
345 Signal(Signal),
346 RcPath(u32),
348 Anchor(TraceLineNumberAnchor),
349 ConditionReevaluation(TraceConditionReevaluation),
350}
351
352#[cfg(feature = "tracing")]
353#[derive(serde::Serialize)]
354#[serde(tag = "kind", content = "content", rename_all = "camelCase")]
355pub enum TraceInfo {
356 LineNumber(TraceInfoInner),
357 ColumnNumber(TraceInfoInner),
358 IsStartOfLine(TraceInfoInner),
359 IndentLevel(TraceInfoInner),
360 LineStartColumnNumber(TraceInfoInner),
361 LineStartIndentLevel(TraceInfoInner),
362}
363
364#[cfg(feature = "tracing")]
365#[derive(serde::Serialize)]
366#[serde(rename_all = "camelCase")]
367pub struct TraceInfoInner {
368 pub info_id: u32,
369 pub name: String,
370}
371
372#[cfg(feature = "tracing")]
373impl TraceInfoInner {
374 pub fn new(info_id: u32, name: &str) -> Self {
375 Self {
376 info_id,
377 name: name.to_string(),
378 }
379 }
380}
381
382#[cfg(feature = "tracing")]
383#[derive(serde::Serialize)]
384#[serde(rename_all = "camelCase")]
385pub struct TraceLineNumberAnchor {
386 pub anchor_id: u32,
387 pub name: String,
388}
389
390#[cfg(feature = "tracing")]
391#[derive(serde::Serialize)]
392#[serde(rename_all = "camelCase")]
393pub struct TraceConditionReevaluation {
394 pub condition_id: u32,
395 pub name: String,
396}
397
398#[cfg(feature = "tracing")]
399#[derive(serde::Serialize)]
400#[serde(rename_all = "camelCase")]
401pub struct TraceCondition {
402 pub condition_id: u32,
403 pub name: String,
404 pub is_stored: bool,
405 pub store_save_point: bool,
406 #[serde(skip_serializing_if = "Option::is_none")]
407 pub true_path: Option<u32>,
409 #[serde(skip_serializing_if = "Option::is_none")]
410 pub false_path: Option<u32>,
412}
413
414pub struct PrintNode<'a> {
415 pub(super) next: Option<PrintItemPath<'a>>,
416 pub(super) item: PrintItem<'a>,
417 #[cfg(feature = "tracing")]
418 pub print_node_id: u32,
419}
420
421impl<'a> PrintNode<'a> {
422 fn new(item: PrintItem<'a>) -> PrintNode<'a> {
423 PrintNode {
424 item,
425 next: None,
426 #[cfg(feature = "tracing")]
427 print_node_id: thread_state::next_print_node_id(),
428 }
429 }
430
431 fn set_next(&mut self, new_next: Option<PrintItemPath<'a>>) {
432 let past_next = mem::replace(&mut self.next, new_next);
433
434 if let Some(past_next) = past_next
435 && let Some(new_next) = new_next
436 {
437 new_next.get_last_next().unwrap_or(new_next).set_next(Some(past_next));
438 }
439 }
440}
441
442pub struct PrintNodeCell<'a> {
444 value: UnsafeCell<PrintNode<'a>>,
445}
446
447impl<'a> PrintNodeCell<'a> {
448 pub(super) fn new(item: PrintItem<'a>) -> PrintNodeCell<'a> {
449 PrintNodeCell {
450 value: UnsafeCell::new(PrintNode::new(item)),
451 }
452 }
453
454 #[inline]
455 pub(super) fn get_item(&self) -> PrintItem<'a> {
456 unsafe { (*self.value.get()).item.clone() }
457 }
458
459 #[inline]
460 pub(super) fn get_next(&self) -> Option<PrintItemPath<'a>> {
461 unsafe { (*self.value.get()).next }
462 }
463
464 #[inline]
465 pub(super) fn set_next(&self, new_next: Option<PrintItemPath<'a>>) {
466 unsafe {
467 (*self.value.get()).set_next(new_next);
468 }
469 }
470
471 #[inline]
472 pub(super) fn get_last_next(&self) -> Option<PrintItemPath<'a>> {
473 let mut current = self.get_next();
474 loop {
475 if let Some(last) = ¤t
476 && let Some(next) = last.get_next()
477 {
478 current.replace(next);
479 continue;
480 }
481 break;
482 }
483
484 current
485 }
486
487 #[cfg(feature = "tracing")]
488 pub(super) fn get_node_id(&self) -> u32 {
489 unsafe { (*self.get_node()).print_node_id }
490 }
491
492 #[inline]
495 pub(super) unsafe fn get_node(&self) -> *mut PrintNode<'a> {
496 self.value.get()
497 }
498
499 #[inline]
500 pub fn take_next(self) -> Option<PrintItemPath<'a>> {
501 self.value.into_inner().next.take()
502 }
503}
504
505pub type PrintItemPath<'a> = &'a PrintNodeCell<'a>;
506
507#[derive(Clone)]
511pub enum PrintItem<'a> {
512 String(&'a StringContainer<'a>),
513 Condition(&'a Condition<'a>),
514 Signal(Signal),
515 RcPath(PrintItemPath<'a>),
516 Anchor(Anchor),
517 Info(Info),
518 ConditionReevaluation(ConditionReevaluation),
519}
520
521#[derive(Clone, PartialEq, Eq, Copy, Debug, serde::Serialize)]
522pub enum Signal {
523 NewLine,
525 Tab,
527 PossibleNewLine,
530 SpaceOrNewLine,
533 ExpectNewLine,
535 QueueStartIndent,
537 StartIndent,
539 FinishIndent,
541 StartNewLineGroup,
544 FinishNewLineGroup,
546 SingleIndent,
548 StartIgnoringIndent,
550 FinishIgnoringIndent,
552 StartResettingIndent,
561 FinishResettingIndent,
565 StartAlign(u8),
572 FinishAlign,
574 StartForceNoNewLines,
576 FinishForceNoNewLines,
578 SpaceIfNotTrailing,
580}
581
582#[derive(Clone)]
583pub enum Anchor {
584 LineNumber(LineNumberAnchor),
585}
586
587impl From<LineNumberAnchor> for Anchor {
588 fn from(anchor: LineNumberAnchor) -> Self {
589 Anchor::LineNumber(anchor)
590 }
591}
592
593#[derive(Clone)]
596pub struct LineNumberAnchor {
597 id: u32,
598 line_number: LineNumber,
599}
600
601impl LineNumberAnchor {
602 pub fn new(line_number: LineNumber) -> Self {
603 Self {
604 id: thread_state::next_line_number_anchor_id(),
605 line_number,
606 }
607 }
608
609 #[inline]
610 pub fn unique_id(&self) -> u32 {
611 self.id
612 }
613
614 #[inline]
615 pub fn line_number_id(&self) -> u32 {
616 self.line_number.id
617 }
618
619 #[inline]
620 pub fn name(&self) -> &'static str {
621 self.line_number.name()
622 }
623}
624
625#[derive(Clone, PartialEq, Eq, Copy, Debug)]
626pub enum Info {
627 LineNumber(LineNumber),
628 ColumnNumber(ColumnNumber),
629 IsStartOfLine(IsStartOfLine),
630 IndentLevel(IndentLevel),
631 LineStartColumnNumber(LineStartColumnNumber),
632 LineStartIndentLevel(LineStartIndentLevel),
633}
634
635impl From<LineNumber> for Info {
636 fn from(info: LineNumber) -> Self {
637 Info::LineNumber(info)
638 }
639}
640
641impl From<ColumnNumber> for Info {
642 fn from(info: ColumnNumber) -> Self {
643 Info::ColumnNumber(info)
644 }
645}
646
647impl From<IsStartOfLine> for Info {
648 fn from(info: IsStartOfLine) -> Self {
649 Info::IsStartOfLine(info)
650 }
651}
652
653impl From<IndentLevel> for Info {
654 fn from(info: IndentLevel) -> Self {
655 Info::IndentLevel(info)
656 }
657}
658
659impl From<LineStartColumnNumber> for Info {
660 fn from(info: LineStartColumnNumber) -> Self {
661 Info::LineStartColumnNumber(info)
662 }
663}
664
665impl From<LineStartIndentLevel> for Info {
666 fn from(info: LineStartIndentLevel) -> Self {
667 Info::LineStartIndentLevel(info)
668 }
669}
670
671#[derive(Clone, PartialEq, Eq, Copy, Debug)]
673pub struct LineAndColumn {
674 pub line: LineNumber,
675 pub column: ColumnNumber,
676}
677
678impl LineAndColumn {
679 pub fn new(name: &'static str) -> Self {
680 Self {
681 line: LineNumber::new(name),
682 column: ColumnNumber::new(name),
683 }
684 }
685}
686
687#[derive(Clone, PartialEq, Eq, Copy, Debug)]
688pub struct LineNumber {
689 id: u32,
690 #[cfg(debug_assertions)]
692 name: &'static str,
693}
694
695impl LineNumber {
696 pub fn new(_name: &'static str) -> Self {
697 Self {
698 id: thread_state::next_line_number_id(),
699 #[cfg(debug_assertions)]
700 name: _name,
701 }
702 }
703
704 #[inline]
705 pub fn unique_id(&self) -> u32 {
706 self.id
707 }
708
709 #[inline]
710 pub fn name(&self) -> &'static str {
711 #[cfg(debug_assertions)]
712 return self.name;
713 #[cfg(not(debug_assertions))]
714 return "line_number";
715 }
716}
717
718#[derive(Clone, PartialEq, Eq, Copy, Debug)]
719pub struct ColumnNumber {
720 id: u32,
721 #[cfg(debug_assertions)]
723 name: &'static str,
724}
725
726impl ColumnNumber {
727 pub fn new(_name: &'static str) -> Self {
728 Self {
729 id: thread_state::next_column_number_id(),
730 #[cfg(debug_assertions)]
731 name: _name,
732 }
733 }
734
735 #[inline]
736 pub fn unique_id(&self) -> u32 {
737 self.id
738 }
739
740 #[inline]
741 pub fn name(&self) -> &'static str {
742 #[cfg(debug_assertions)]
743 return self.name;
744 #[cfg(not(debug_assertions))]
745 return "column_number";
746 }
747}
748
749#[derive(Clone, PartialEq, Eq, Copy, Debug)]
750pub struct IsStartOfLine {
751 id: u32,
752 #[cfg(debug_assertions)]
754 name: &'static str,
755}
756
757impl IsStartOfLine {
758 pub fn new(_name: &'static str) -> Self {
759 Self {
760 id: thread_state::next_is_start_of_line_id(),
761 #[cfg(debug_assertions)]
762 name: _name,
763 }
764 }
765
766 #[inline]
767 pub fn unique_id(&self) -> u32 {
768 self.id
769 }
770
771 #[inline]
772 pub fn name(&self) -> &'static str {
773 #[cfg(debug_assertions)]
774 return self.name;
775 #[cfg(not(debug_assertions))]
776 return "is_start_of_line";
777 }
778}
779
780#[derive(Clone, PartialEq, Eq, Copy, Debug)]
781pub struct LineStartColumnNumber {
782 id: u32,
783 #[cfg(debug_assertions)]
785 name: &'static str,
786}
787
788impl LineStartColumnNumber {
789 pub fn new(_name: &'static str) -> Self {
790 Self {
791 id: thread_state::next_line_start_column_number_id(),
792 #[cfg(debug_assertions)]
793 name: _name,
794 }
795 }
796
797 #[inline]
798 pub fn unique_id(&self) -> u32 {
799 self.id
800 }
801
802 #[inline]
803 pub fn name(&self) -> &'static str {
804 #[cfg(debug_assertions)]
805 return self.name;
806 #[cfg(not(debug_assertions))]
807 return "line_start_column_number";
808 }
809}
810
811#[derive(Clone, PartialEq, Eq, Copy, Debug)]
812pub struct IndentLevel {
813 id: u32,
814 #[cfg(debug_assertions)]
816 name: &'static str,
817}
818
819impl IndentLevel {
820 pub fn new(_name: &'static str) -> Self {
821 Self {
822 id: thread_state::next_indent_level_id(),
823 #[cfg(debug_assertions)]
824 name: _name,
825 }
826 }
827
828 #[inline]
829 pub fn unique_id(&self) -> u32 {
830 self.id
831 }
832
833 #[inline]
834 pub fn name(&self) -> &'static str {
835 #[cfg(debug_assertions)]
836 return self.name;
837 #[cfg(not(debug_assertions))]
838 return "indent_level";
839 }
840}
841
842#[derive(Clone, PartialEq, Eq, Copy, Debug)]
843pub struct LineStartIndentLevel {
844 id: u32,
845 #[cfg(debug_assertions)]
847 name: &'static str,
848}
849
850impl LineStartIndentLevel {
851 pub fn new(_name: &'static str) -> Self {
852 Self {
853 id: thread_state::next_line_start_indent_level_id(),
854 #[cfg(debug_assertions)]
855 name: _name,
856 }
857 }
858
859 #[inline]
860 pub fn unique_id(&self) -> u32 {
861 self.id
862 }
863
864 #[inline]
865 pub fn name(&self) -> &'static str {
866 #[cfg(debug_assertions)]
867 return self.name;
868 #[cfg(not(debug_assertions))]
869 return "line_start_indent_level";
870 }
871}
872
873#[derive(Clone, Copy, PartialEq, Eq, Debug)]
875pub struct ConditionReevaluation {
876 pub(crate) condition_reevaluation_id: u32,
877 pub(crate) condition_id: u32,
878 #[cfg(debug_assertions)]
880 name: &'static str,
881}
882
883impl ConditionReevaluation {
884 pub(crate) fn new(_name: &'static str, condition_id: u32) -> Self {
885 ConditionReevaluation {
886 condition_reevaluation_id: thread_state::next_condition_reevaluation_id(),
887 condition_id,
888 #[cfg(debug_assertions)]
889 name: _name,
890 }
891 }
892
893 pub fn name(&self) -> &'static str {
894 #[cfg(debug_assertions)]
895 return self.name;
896 #[cfg(not(debug_assertions))]
897 return "condition_reevaluation";
898 }
899}
900
901#[derive(Clone)]
906pub struct Condition<'a> {
907 id: u32,
909 #[cfg(debug_assertions)]
911 name: &'static str,
912 pub(super) is_stored: bool,
915 pub(super) store_save_point: bool,
916 pub(super) condition: ConditionResolver<'a>,
918 pub(super) true_path: Option<PrintItemPath<'a>>,
920 pub(super) false_path: Option<PrintItemPath<'a>>,
922}
923
924impl<'a> Condition<'a> {
925 pub fn new(name: &'static str, properties: ConditionProperties<'a>) -> Self {
926 Self::new_internal(name, properties)
927 }
928
929 pub fn new_true() -> Self {
930 Self::new_internal(
931 "trueCondition",
932 ConditionProperties {
933 condition: condition_resolvers::true_resolver(),
934 true_path: None,
935 false_path: None,
936 },
937 )
938 }
939
940 pub fn new_false() -> Self {
941 Self::new_internal(
942 "falseCondition",
943 ConditionProperties {
944 condition: condition_resolvers::false_resolver(),
945 true_path: None,
946 false_path: None,
947 },
948 )
949 }
950
951 fn new_internal(_name: &'static str, properties: ConditionProperties<'a>) -> Self {
952 Self {
953 id: thread_state::next_condition_id(),
954 is_stored: false,
955 store_save_point: false,
956 #[cfg(debug_assertions)]
957 name: _name,
958 condition: properties.condition,
959 true_path: properties.true_path.and_then(|x| x.first_node),
960 false_path: properties.false_path.and_then(|x| x.first_node),
961 }
962 }
963
964 #[inline]
965 pub fn unique_id(&self) -> u32 {
966 self.id
967 }
968
969 #[inline]
970 pub fn name(&self) -> &'static str {
971 #[cfg(debug_assertions)]
972 return self.name;
973 #[cfg(not(debug_assertions))]
974 return "condition";
975 }
976
977 #[inline]
978 pub fn true_path(&self) -> &Option<PrintItemPath<'a>> {
979 &self.true_path
980 }
981
982 #[inline]
983 pub fn false_path(&self) -> &Option<PrintItemPath<'a>> {
984 &self.false_path
985 }
986
987 #[inline]
988 pub(super) fn resolve(&self, context: &mut ConditionResolverContext) -> Option<bool> {
989 (self.condition)(context)
990 }
991
992 pub fn create_reference(&mut self) -> ConditionReference {
993 self.is_stored = true;
994 ConditionReference::new(self.name(), self.id)
995 }
996
997 pub fn create_reevaluation(&mut self) -> ConditionReevaluation {
998 self.store_save_point = true;
999 self.is_stored = true;
1000 ConditionReevaluation::new(self.name(), self.id)
1001 }
1002}
1003
1004#[derive(Clone, PartialEq, Eq, Copy, Debug)]
1005pub struct ConditionReference {
1006 #[cfg(debug_assertions)]
1007 pub(super) name: &'static str,
1008 pub(super) id: u32,
1009}
1010
1011impl ConditionReference {
1012 pub(super) fn new(_name: &'static str, id: u32) -> ConditionReference {
1013 ConditionReference {
1014 #[cfg(debug_assertions)]
1015 name: _name,
1016 id,
1017 }
1018 }
1019
1020 #[inline]
1021 pub(super) fn name(&self) -> &'static str {
1022 #[cfg(debug_assertions)]
1023 return self.name;
1024 #[cfg(not(debug_assertions))]
1025 return "conditionRef";
1026 }
1027
1028 pub fn create_resolver<'a>(&self, arena: &'a Arena) -> ConditionResolver<'a> {
1030 let captured_self = *self;
1031 arena.alloc_resolver(move |condition_context| condition_context.resolved_condition(&captured_self))
1032 }
1033}
1034
1035pub struct ConditionProperties<'a> {
1037 pub condition: ConditionResolver<'a>,
1039 pub true_path: Option<PrintItems<'a>>,
1041 pub false_path: Option<PrintItems<'a>>,
1043}
1044
1045pub type ConditionResolver<'a> = &'a dyn Fn(&mut ConditionResolverContext) -> Option<bool>;
1048
1049pub struct ConditionResolverContext<'a, 'b> {
1051 printer: &'a mut Printer<'b>,
1052 pub writer_info: WriterInfo,
1054}
1055
1056impl<'a, 'b> ConditionResolverContext<'a, 'b> {
1057 pub(super) fn new(printer: &'a mut Printer<'b>, writer_info: WriterInfo) -> Self {
1058 ConditionResolverContext { printer, writer_info }
1059 }
1060
1061 pub fn resolved_condition(&mut self, condition_reference: &ConditionReference) -> Option<bool> {
1064 self.printer.resolved_condition(condition_reference)
1065 }
1066
1067 pub fn resolved_line_and_column(&mut self, line_and_column: LineAndColumn) -> Option<(u32, u32)> {
1069 let line = self.printer.resolved_line_number(line_and_column.line)?;
1070 let column = self.printer.resolved_column_number(line_and_column.column)?;
1071 Some((line, column))
1072 }
1073
1074 pub fn resolved_line_number(&mut self, line_number: LineNumber) -> Option<u32> {
1076 self.printer.resolved_line_number(line_number)
1077 }
1078
1079 pub fn resolved_column_number(&mut self, column_number: ColumnNumber) -> Option<u32> {
1081 self.printer.resolved_column_number(column_number)
1082 }
1083
1084 pub fn resolved_is_start_of_line(&mut self, is_start_of_line: IsStartOfLine) -> Option<bool> {
1086 self.printer.resolved_is_start_of_line(is_start_of_line)
1087 }
1088
1089 pub fn resolved_indent_level(&mut self, indent_level: IndentLevel) -> Option<u8> {
1091 self.printer.resolved_indent_level(indent_level)
1092 }
1093
1094 pub fn resolved_line_start_column_number(&mut self, line_start_column_number: LineStartColumnNumber) -> Option<u32> {
1096 self.printer.resolved_line_start_column_number(line_start_column_number)
1097 }
1098
1099 pub fn resolved_line_start_indent_level(&mut self, line_start_indent_level: LineStartIndentLevel) -> Option<u8> {
1101 self.printer.resolved_line_start_indent_level(line_start_indent_level)
1102 }
1103
1104 pub fn clear_line_and_column(&mut self, lc: LineAndColumn) {
1106 self.clear_info(lc.line);
1107 self.clear_info(lc.column);
1108 }
1109
1110 pub fn clear_info(&mut self, info: impl Into<Info>) {
1112 self.printer.clear_info(info.into())
1113 }
1114
1115 pub fn shift_line_number(&mut self, info: LineNumber, delta: isize) {
1120 self.printer.shift_line_number(info, delta)
1121 }
1122
1123 pub fn is_forcing_no_newlines(&self) -> bool {
1125 self.printer.is_forcing_no_newlines()
1126 }
1127}
1128
1129#[derive(Clone)]
1131pub struct StringContainer<'a> {
1132 pub text: &'a str,
1134 pub(super) char_count: u32,
1137}
1138
1139impl<'a> StringContainer<'a> {
1140 pub fn new(text: &'a str) -> Self {
1142 let char_count = unicode_width::UnicodeWidthStr::width(text) as u32;
1143 Self { text, char_count }
1144 }
1145
1146 pub const fn proc_macro_new_with_char_count(text: &'a str, char_count: u32) -> Self {
1149 Self { text, char_count }
1150 }
1151}
1152
1153#[derive(Clone, Debug)]
1155pub struct WriterInfo {
1156 pub line_number: u32,
1157 pub column_number: u32,
1158 pub indent_level: u8,
1159 pub line_start_indent_level: u8,
1160 pub line_start_align_columns: u32,
1162 pub indent_width: u8,
1163 pub expect_newline_next: bool,
1164}
1165
1166impl WriterInfo {
1167 pub fn is_start_of_line(&self) -> bool {
1170 self.expect_newline_next || self.is_column_number_at_line_start()
1171 }
1172
1173 pub fn is_start_of_line_indented(&self) -> bool {
1175 self.line_start_indent_level > self.indent_level
1176 }
1177
1178 pub fn is_column_number_at_line_start(&self) -> bool {
1180 self.column_number == self.line_start_column_number()
1181 }
1182
1183 pub fn line_start_column_number(&self) -> u32 {
1184 (self.line_start_indent_level as u32) * (self.indent_width as u32) + self.line_start_align_columns
1185 }
1186
1187 pub fn line_and_column(&self) -> (u32, u32) {
1189 (self.line_number, self.column_number)
1190 }
1191}