1pub mod binary;
2pub mod comments;
3pub mod cst;
4pub mod format_config;
5mod nested_format;
6pub use nested_format::{format_nested, format_nested_with_indent};
7pub mod parser;
8pub mod parser_config;
9mod quotes;
10pub mod reference_literal;
11pub use reference_literal::{decode_reference_literal, encode_reference_literal};
12pub mod stream_parser;
13pub mod syntax;
14
15use comments::strip_comments;
16pub use cst::{CstKind, CstNode, parse_lino_to_cst, parse_lino_to_cst_with_config};
17use format_config::FormatConfig;
18pub use parser_config::ParserConfig;
19use std::borrow::Cow;
20pub use stream_parser::{
21 ErrorLocation, StreamIterator, StreamParseError, StreamParser, StreamPosition, StreamReference,
22};
23pub use syntax::{SyntaxReference, reference_quote};
24
25#[cfg(feature = "macro")]
27pub use links_notation_macro::lino;
28use std::error::Error as StdError;
29use std::fmt;
30
31pub const VERSION: &str = env!("CARGO_PKG_VERSION");
42
43#[derive(Debug)]
45pub enum ParseError {
46 EmptyInput,
48 SyntaxError(SyntaxError),
50 NestingTooDeep(NestingTooDeep),
53 InternalError(String),
55}
56
57impl fmt::Display for ParseError {
58 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59 match self {
60 ParseError::EmptyInput => write!(f, "Empty input"),
61 ParseError::SyntaxError(error) => write!(f, "Syntax error at {}", error),
62 ParseError::NestingTooDeep(error) => write!(f, "Nesting too deep at {}", error),
63 ParseError::InternalError(msg) => write!(f, "Internal error: {}", msg),
64 }
65 }
66}
67
68impl StdError for ParseError {}
69
70const QUOTED_LINE_WIDTH: usize = 80;
75
76const ELLIPSIS: &str = "...";
78
79#[derive(Debug, Clone, PartialEq, Eq)]
95pub struct SyntaxError {
96 pub offset: usize,
98 pub line: usize,
100 pub column: usize,
102 pub expected: Vec<String>,
105 pub found: Option<char>,
107 pub line_text: String,
109}
110
111impl SyntaxError {
112 pub fn summary(&self) -> String {
128 let found = match self.found {
129 Some(character) => format!("\"{}\"", character.escape_debug()),
130 None => "end of input".to_string(),
131 };
132 match join_alternatives(&self.expected) {
133 Some(expected) => format!(
134 "line {}, column {}: expected {}, found {}",
135 self.line, self.column, expected, found
136 ),
137 None => format!(
138 "line {}, column {}: unexpected {}",
139 self.line, self.column, found
140 ),
141 }
142 }
143
144 pub fn snippet(&self) -> String {
160 snippet(self.line, &self.line_text, self.column)
161 }
162}
163
164impl fmt::Display for SyntaxError {
165 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
166 write!(f, "{}\n{}", self.summary(), self.snippet())
167 }
168}
169
170impl StdError for SyntaxError {}
171
172#[derive(Debug, Clone, PartialEq, Eq)]
195pub struct NestingTooDeep {
196 pub max_depth: usize,
198 pub offset: usize,
200 pub line: usize,
202 pub column: usize,
204 pub line_text: String,
206}
207
208impl NestingTooDeep {
209 pub fn summary(&self) -> String {
211 format!(
212 "line {}, column {}: nesting depth exceeds the maximum of {}",
213 self.line, self.column, self.max_depth
214 )
215 }
216
217 pub fn snippet(&self) -> String {
219 snippet(self.line, &self.line_text, self.column)
220 }
221}
222
223impl fmt::Display for NestingTooDeep {
224 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
225 write!(f, "{}\n{}", self.summary(), self.snippet())
226 }
227}
228
229impl StdError for NestingTooDeep {}
230
231fn snippet(number: usize, line_text: &str, column: usize) -> String {
234 let (quoted, column) = quote_line(line_text, column);
235 let number = number.to_string();
236 let gutter = " ".repeat(number.len());
237 format!(
238 "{} | {}\n{} | {}^",
239 number,
240 quoted,
241 gutter,
242 " ".repeat(column - 1)
243 )
244}
245
246fn join_alternatives(alternatives: &[String]) -> Option<String> {
248 match alternatives {
249 [] => None,
250 [only] => Some(only.clone()),
251 [rest @ .., last] => Some(format!("{} or {}", rest.join(", "), last)),
252 }
253}
254
255fn quote_line(line: &str, column: usize) -> (String, usize) {
258 let characters: Vec<char> = line.chars().collect();
259 if characters.len() <= QUOTED_LINE_WIDTH {
260 return (line.to_string(), column);
261 }
262
263 let target = column - 1;
264 let last_start = characters.len() - QUOTED_LINE_WIDTH;
265 let start = target.saturating_sub(QUOTED_LINE_WIDTH / 2).min(last_start);
266 let end = start + QUOTED_LINE_WIDTH;
267
268 let mut quoted = String::new();
269 if start > 0 {
270 quoted.push_str(ELLIPSIS);
271 }
272 quoted.extend(&characters[start..end]);
273 if end < characters.len() {
274 quoted.push_str(ELLIPSIS);
275 }
276
277 let shift = if start > 0 {
278 ELLIPSIS.chars().count()
279 } else {
280 0
281 };
282 (quoted, target - start + shift + 1)
283}
284
285fn locate(document: &str, failure: parser::ParseFailure) -> SyntaxError {
289 let offset = failure.offset.min(document.len());
290 let mut line = 1;
291 let mut line_start = 0;
292 let bytes = document.as_bytes();
293 let mut cursor = 0;
294 while cursor < offset {
295 match bytes[cursor] {
296 b'\r' => {
297 line += 1;
298 cursor += 1;
299 if cursor < offset && bytes[cursor] == b'\n' {
300 cursor += 1;
301 }
302 line_start = cursor;
303 }
304 b'\n' => {
305 line += 1;
306 cursor += 1;
307 line_start = cursor;
308 }
309 _ => cursor += 1,
310 }
311 }
312 let column = document[line_start..offset].chars().count() + 1;
313 let line_end = document[line_start..]
314 .find(['\r', '\n'])
315 .map_or(document.len(), |position| line_start + position);
316 let line_text = &document[line_start..line_end];
317
318 SyntaxError {
319 offset,
320 line,
321 column,
322 expected: failure.expected.iter().map(|s| s.to_string()).collect(),
323 found: document[offset..].chars().next(),
324 line_text: line_text.to_string(),
325 }
326}
327
328fn parse_error(document: &str, failure: parser::ParseFailure) -> ParseError {
330 let max_depth = failure.max_depth_exceeded;
331 let located = locate(document, failure);
332 match max_depth {
333 Some(max_depth) => ParseError::NestingTooDeep(NestingTooDeep {
334 max_depth,
335 offset: located.offset,
336 line: located.line,
337 column: located.column,
338 line_text: located.line_text,
339 }),
340 None => ParseError::SyntaxError(located),
341 }
342}
343
344#[derive(Debug, Clone, PartialEq)]
345pub enum LiNo<T> {
346 Link { id: Option<T>, values: Vec<Self> },
347 Ref(T),
348}
349
350impl<T> LiNo<T> {
351 pub fn is_ref(&self) -> bool {
352 matches!(self, LiNo::Ref(_))
353 }
354
355 pub fn is_link(&self) -> bool {
356 matches!(self, LiNo::Link { .. })
357 }
358
359 pub fn new(id: Option<T>, values: Vec<Self>) -> Self {
376 LiNo::Link { id, values }
377 }
378
379 pub fn anonymous(values: Vec<Self>) -> Self {
390 LiNo::Link { id: None, values }
391 }
392
393 pub fn reference(value: T) -> Self {
403 LiNo::Ref(value)
404 }
405}
406
407#[derive(Debug, Clone, Default)]
442pub struct LiNoBuilder {
443 id: Option<String>,
444 values: Vec<LiNo<String>>,
445}
446
447impl LiNoBuilder {
448 pub fn new() -> Self {
450 Self::default()
451 }
452
453 pub fn id(mut self, id: &str) -> Self {
457 self.id = Some(id.to_string());
458 self
459 }
460
461 pub fn value(mut self, value: &str) -> Self {
463 self.values.push(LiNo::Ref(value.to_string()));
464 self
465 }
466
467 pub fn lino(mut self, value: LiNo<String>) -> Self {
469 self.values.push(value);
470 self
471 }
472
473 pub fn values<I, S>(mut self, values: I) -> Self
475 where
476 I: IntoIterator<Item = S>,
477 S: AsRef<str>,
478 {
479 for v in values {
480 self.values.push(LiNo::Ref(v.as_ref().to_string()));
481 }
482 self
483 }
484
485 pub fn linos<I>(mut self, values: I) -> Self
487 where
488 I: IntoIterator<Item = LiNo<String>>,
489 {
490 self.values.extend(values);
491 self
492 }
493
494 pub fn build(self) -> LiNo<String> {
496 LiNo::Link {
497 id: self.id,
498 values: self.values,
499 }
500 }
501}
502
503#[deprecated(since = "0.3.0", note = "Use LiNoBuilder instead")]
505pub type LinkBuilder = LiNoBuilder;
506
507impl<T: ToString + Clone> LiNo<T> {
508 pub fn format_with_config(&self, config: &FormatConfig) -> String {
516 match self {
517 LiNo::Ref(value) => {
518 let escaped = escape_reference(&value.to_string());
519 if config.less_parentheses {
520 escaped
521 } else {
522 format!("({})", escaped)
523 }
524 }
525 LiNo::Link { id, values } => {
526 if id.is_none() && values.is_empty() {
528 return "()".to_string();
529 }
530
531 if values.is_empty() {
533 if let Some(id_val) = id {
534 let escaped_id = escape_reference(&id_val.to_string());
535 return if config.less_parentheses && !needs_parentheses(&id_val.to_string())
536 {
537 escaped_id
538 } else {
539 format!("({})", escaped_id)
540 };
541 }
542 return "()".to_string();
543 }
544
545 let mut should_indent = false;
547 if config.should_indent_by_ref_count(values.len()) {
548 should_indent = true;
549 } else {
550 let values_str = values
552 .iter()
553 .map(|v| format_value(v))
554 .collect::<Vec<_>>()
555 .join(" ");
556
557 let test_line = if let Some(id_val) = id {
558 let id_str = escape_reference(&id_val.to_string());
559 if config.less_parentheses {
560 format!("{}: {}", id_str, values_str)
561 } else {
562 format!("({}: {})", id_str, values_str)
563 }
564 } else if config.less_parentheses && !has_lone_nested_value(values) {
565 values_str.clone()
566 } else {
567 format!("({})", values_str)
568 };
569
570 if config.should_indent_by_length(&test_line) {
571 should_indent = true;
572 }
573 }
574
575 if should_indent && !config.prefer_inline {
577 return self.format_indented(config);
578 }
579
580 let values_str = values
582 .iter()
583 .map(|v| format_value(v))
584 .collect::<Vec<_>>()
585 .join(" ");
586
587 if id.is_none() {
589 if config.less_parentheses && !has_lone_nested_value(values) {
590 let all_simple = values.iter().all(|v| matches!(v, LiNo::Ref(_)));
592 if all_simple {
593 return values
594 .iter()
595 .map(|v| match v {
596 LiNo::Ref(r) => escape_reference(&r.to_string()),
597 _ => format_value(v),
598 })
599 .collect::<Vec<_>>()
600 .join(" ");
601 }
602 return values_str;
603 }
604 return format!("({})", values_str);
605 }
606
607 let id_str = escape_reference(&id.as_ref().unwrap().to_string());
609 let with_colon = format!("{}: {}", id_str, values_str);
610 if config.less_parentheses && !needs_parentheses(&id.as_ref().unwrap().to_string())
611 {
612 with_colon
613 } else {
614 format!("({})", with_colon)
615 }
616 }
617 }
618 }
619
620 fn format_indented(&self, config: &FormatConfig) -> String {
622 match self {
623 LiNo::Ref(value) => {
624 let escaped = escape_reference(&value.to_string());
625 format!("({})", escaped)
626 }
627 LiNo::Link { id, values } => {
628 if id.is_none() {
629 self.format_with_config(&FormatConfig {
630 less_parentheses: config.less_parentheses,
631 ..FormatConfig::default()
632 })
633 } else {
634 let id_str = escape_reference(&id.as_ref().unwrap().to_string());
636 let mut lines = vec![format!("{}:", id_str)];
637 for v in values {
638 lines.push(format!("{}{}", config.indent_string, format_value(v)));
639 }
640 lines.join("\n")
641 }
642 }
643 }
644 }
645}
646
647impl<T: ToString> fmt::Display for LiNo<T> {
648 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
649 match self {
650 LiNo::Ref(value) => write!(f, "{}", escape_reference(&value.to_string())),
651 LiNo::Link { id, values } => {
652 let id_str = id
653 .as_ref()
654 .map(|id| format!("{}: ", escape_reference(&id.to_string())))
655 .unwrap_or_default();
656
657 if f.alternate() {
658 let lines = values
660 .iter()
661 .map(|value| {
662 match value {
665 LiNo::Ref(_) => format!("{}({})", id_str, value),
666 _ => format!("{}{}", id_str, value),
667 }
668 })
669 .collect::<Vec<_>>()
670 .join("\n");
671 write!(f, "{}", lines)
672 } else {
673 let values_str = values
674 .iter()
675 .map(|value| value.to_string())
676 .collect::<Vec<_>>()
677 .join(" ");
678 write!(f, "({}{})", id_str, values_str)
679 }
680 }
681 }
682 }
683}
684
685impl LiNo<SyntaxReference> {
686 pub fn into_values(self) -> LiNo<String> {
699 match self {
700 LiNo::Ref(reference) => LiNo::Ref(reference.value),
701 LiNo::Link { id, values } => LiNo::Link {
702 id: id.map(|id| id.value),
703 values: values.into_iter().map(LiNo::into_values).collect(),
704 },
705 }
706 }
707
708 pub fn raw_id(&self) -> Option<&str> {
722 match self {
723 LiNo::Ref(reference) => reference.raw.as_deref(),
724 LiNo::Link { id, .. } => id.as_ref().and_then(|id| id.raw.as_deref()),
725 }
726 }
727
728 pub fn quote(&self) -> Option<char> {
731 self.raw_id().and_then(reference_quote)
732 }
733}
734
735impl From<LiNo<SyntaxReference>> for LiNo<String> {
737 fn from(link: LiNo<SyntaxReference>) -> Self {
738 link.into_values()
739 }
740}
741
742impl From<parser::Link> for LiNo<String> {
744 fn from(link: parser::Link) -> Self {
745 if let Some(body) = &link.nested {
746 return transform_nested(body, &plain_id);
747 }
748 if link.values.is_empty() && link.children.is_empty() {
749 if let Some(id) = link.id {
750 LiNo::Ref(id)
751 } else {
752 LiNo::Link {
753 id: None,
754 values: vec![],
755 }
756 }
757 } else {
758 let values: Vec<LiNo<String>> = link.values.into_iter().map(|v| v.into()).collect();
759 LiNo::Link {
760 id: link.id,
761 values,
762 }
763 }
764 }
765}
766
767trait IdReader<T>: Fn(&parser::Link) -> Option<T> {}
770
771impl<T, F: Fn(&parser::Link) -> Option<T>> IdReader<T> for F {}
772
773fn plain_id(link: &parser::Link) -> Option<String> {
775 link.id.clone()
776}
777
778fn syntax_id(link: &parser::Link, document: &str) -> Option<SyntaxReference> {
782 let value = link.id.clone()?;
783 let raw = link
784 .id_span
785 .as_ref()
786 .and_then(|span| document.get(span.clone()))
787 .map(str::to_owned);
788 Some(SyntaxReference { value, raw })
789}
790
791fn transform_nested<T: Clone, F: IdReader<T>>(body: &[parser::Link], id: &F) -> LiNo<T> {
799 let links = flatten_links(body, id);
800 let wraps_single_group =
801 body.len() == 1 && body[0].nested.is_some() && body[0].children.is_empty();
802 if links.len() == 1 && !wraps_single_group {
803 return links.into_iter().next().unwrap();
804 }
805 LiNo::Link {
806 id: None,
807 values: links,
808 }
809}
810
811fn flatten_links<T: Clone, F: IdReader<T>>(links: &[parser::Link], id: &F) -> Vec<LiNo<T>> {
813 let mut result = vec![];
814
815 for link in links {
816 flatten_link_recursive(link, None, &mut result, id);
817 }
818
819 result
820}
821
822fn flatten_link_recursive<T: Clone, F: IdReader<T>>(
823 link: &parser::Link,
824 parent: Option<&LiNo<T>>,
825 result: &mut Vec<LiNo<T>>,
826 id: &F,
827) {
828 if link.is_indented_id
831 && link.id.is_some()
832 && link.values.is_empty()
833 && !link.children.is_empty()
834 {
835 let child_values: Vec<LiNo<T>> = link
836 .children
837 .iter()
838 .map(|child| transform_indented_value(child, id))
839 .collect();
840
841 let current = LiNo::Link {
842 id: id(link),
843 values: child_values,
844 };
845
846 let combined = if let Some(parent) = parent {
847 let wrapped_parent = match parent {
849 LiNo::Ref(ref_id) => LiNo::Link {
850 id: None,
851 values: vec![LiNo::Ref(ref_id.clone())],
852 },
853 link => link.clone(),
854 };
855
856 LiNo::Link {
857 id: None,
858 values: vec![wrapped_parent, current],
859 }
860 } else {
861 current
862 };
863
864 result.push(combined);
865 return; }
867
868 let current = if let Some(body) = &link.nested {
870 transform_nested(body, id)
871 } else if link.values.is_empty() {
872 if let Some(id) = id(link) {
873 LiNo::Ref(id)
874 } else {
875 LiNo::Link {
876 id: None,
877 values: vec![],
878 }
879 }
880 } else {
881 LiNo::Link {
882 id: id(link),
883 values: link
884 .values
885 .iter()
886 .map(|value| transform_value(value, id))
887 .collect(),
888 }
889 };
890
891 let combined = if let Some(parent) = parent {
893 let wrapped_parent = match parent {
895 LiNo::Ref(ref_id) => LiNo::Link {
896 id: None,
897 values: vec![LiNo::Ref(ref_id.clone())],
898 },
899 link => link.clone(),
900 };
901
902 let wrapped_current = match current {
904 LiNo::Ref(ref_id) => LiNo::Link {
905 id: None,
906 values: vec![LiNo::Ref(ref_id)],
907 },
908 link => link,
909 };
910
911 LiNo::Link {
912 id: None,
913 values: vec![wrapped_parent, wrapped_current],
914 }
915 } else {
916 current
917 };
918
919 if link.children.is_empty() {
923 result.push(combined);
924 return;
925 }
926 result.push(combined.clone());
927
928 for child in &link.children {
930 flatten_link_recursive(child, Some(&combined), result, id);
931 }
932}
933
934fn transform_value<T: Clone, F: IdReader<T>>(link: &parser::Link, id: &F) -> LiNo<T> {
936 if let Some(body) = &link.nested {
937 return transform_nested(body, id);
938 }
939 if link.values.is_empty() {
940 return match id(link) {
941 Some(id) => LiNo::Ref(id),
942 None => LiNo::Link {
943 id: None,
944 values: vec![],
945 },
946 };
947 }
948 LiNo::Link {
949 id: id(link),
950 values: link
951 .values
952 .iter()
953 .map(|value| transform_value(value, id))
954 .collect(),
955 }
956}
957
958fn transform_indented_value<T: Clone, F: IdReader<T>>(link: &parser::Link, id: &F) -> LiNo<T> {
960 let children: Vec<_> = link
961 .children
962 .iter()
963 .map(|child| transform_indented_value(child, id))
964 .collect();
965 if !children.is_empty() && link.is_indented_id && link.id.is_some() && link.values.is_empty() {
966 return LiNo::Link {
967 id: id(link),
968 values: children,
969 };
970 }
971 let current = transform_value(link, id);
972
973 if !children.is_empty() {
974 match current {
975 LiNo::Link { id, mut values } => {
976 values.extend(children);
977 LiNo::Link { id, values }
978 }
979 LiNo::Ref(id) => {
980 if link.nested.is_some() {
981 return LiNo::Link {
982 id: Some(id),
983 values: children,
984 };
985 }
986 let mut values = vec![LiNo::Ref(id)];
987 values.extend(children);
988 LiNo::Link { id: None, values }
989 }
990 }
991 } else if link.id.is_none() && link.nested.is_none() && link.values.len() == 1 {
992 match current {
993 LiNo::Link { id: None, values } => values.into_iter().next().unwrap(),
994 value => value,
995 }
996 } else {
997 current
998 }
999}
1000
1001fn prepare<'a>(document: &'a str, config: &ParserConfig) -> Cow<'a, str> {
1007 if config.comments {
1008 Cow::Owned(strip_comments(document))
1009 } else {
1010 Cow::Borrowed(document)
1011 }
1012}
1013
1014pub fn parse_lino(document: &str) -> Result<LiNo<String>, ParseError> {
1033 parse_lino_with_config(document, &ParserConfig::default())
1034}
1035
1036pub fn parse_lino_with_config(
1050 document: &str,
1051 config: &ParserConfig,
1052) -> Result<LiNo<String>, ParseError> {
1053 if document.trim_matches(parser::is_whitespace_char).is_empty() {
1055 return Ok(LiNo::Link {
1056 id: None,
1057 values: vec![],
1058 });
1059 }
1060
1061 let prepared = prepare(document, config);
1062 match parser::parse_prepared_document(&prepared, document, config.max_depth) {
1063 Ok(links) => {
1064 if links.is_empty() {
1065 Ok(LiNo::Link {
1066 id: None,
1067 values: vec![],
1068 })
1069 } else {
1070 let flattened = flatten_links(&links, &plain_id);
1072 Ok(LiNo::Link {
1073 id: None,
1074 values: flattened,
1075 })
1076 }
1077 }
1078 Err(failure) => Err(parse_error(document, failure)),
1079 }
1080}
1081
1082pub fn parse_lino_to_links(document: &str) -> Result<Vec<LiNo<String>>, ParseError> {
1096 parse_lino_to_links_with_config(document, &ParserConfig::default())
1097}
1098
1099pub fn parse_lino_to_links_with_config(
1110 document: &str,
1111 config: &ParserConfig,
1112) -> Result<Vec<LiNo<String>>, ParseError> {
1113 read_links(document, config, &plain_id)
1114}
1115
1116pub fn parse_lino_to_syntax_links(
1150 document: &str,
1151) -> Result<Vec<LiNo<SyntaxReference>>, ParseError> {
1152 parse_lino_to_syntax_links_with_config(document, &ParserConfig::default())
1153}
1154
1155pub fn parse_lino_to_syntax_links_with_config(
1168 document: &str,
1169 config: &ParserConfig,
1170) -> Result<Vec<LiNo<SyntaxReference>>, ParseError> {
1171 read_links(document, config, &|link| syntax_id(link, document))
1172}
1173
1174fn read_links<T: Clone, F: IdReader<T>>(
1177 document: &str,
1178 config: &ParserConfig,
1179 id: &F,
1180) -> Result<Vec<LiNo<T>>, ParseError> {
1181 if document.trim_matches(parser::is_whitespace_char).is_empty() {
1183 return Ok(vec![]);
1184 }
1185
1186 let prepared = prepare(document, config);
1187 match parser::parse_prepared_document(&prepared, document, config.max_depth) {
1188 Ok(links) => {
1189 if links.is_empty() {
1190 Ok(vec![])
1191 } else {
1192 let flattened = flatten_links(&links, id);
1194 Ok(flattened)
1195 }
1196 }
1197 Err(failure) => Err(parse_error(document, failure)),
1198 }
1199}
1200
1201pub(crate) fn check_document<'a>(
1205 document: &'a str,
1206 config: &ParserConfig,
1207) -> Result<Cow<'a, str>, ParseError> {
1208 let prepared = prepare(document, config);
1209 if document.trim_matches(parser::is_whitespace_char).is_empty() {
1210 return Ok(prepared);
1211 }
1212 match parser::parse_prepared_document(&prepared, document, config.max_depth) {
1213 Ok(_) => Ok(prepared),
1214 Err(failure) => Err(parse_error(document, failure)),
1215 }
1216}
1217
1218pub fn format_links(links: &[LiNo<String>]) -> String {
1221 links
1222 .iter()
1223 .map(|link| format!("{}", link))
1224 .collect::<Vec<_>>()
1225 .join("\n")
1226}
1227
1228pub fn format_links_with_config(links: &[LiNo<String>], config: &FormatConfig) -> String {
1238 if links.is_empty() {
1239 return String::new();
1240 }
1241
1242 let links_to_format = if config.group_consecutive {
1244 group_consecutive_links(links)
1245 } else {
1246 links.to_vec()
1247 };
1248
1249 links_to_format
1250 .iter()
1251 .map(|link| link.format_with_config(config))
1252 .collect::<Vec<_>>()
1253 .join("\n")
1254}
1255
1256fn group_consecutive_links(links: &[LiNo<String>]) -> Vec<LiNo<String>> {
1272 if links.is_empty() {
1273 return vec![];
1274 }
1275
1276 let mut grouped = vec![];
1277 let mut i = 0;
1278
1279 while i < links.len() {
1280 let current = &links[i];
1281
1282 if let LiNo::Link {
1284 id: Some(current_id),
1285 values: current_values,
1286 } = current
1287 {
1288 if !current_values.is_empty() {
1289 let mut same_id_values = current_values.clone();
1291 let mut j = i + 1;
1292
1293 while j < links.len() {
1294 if let LiNo::Link {
1295 id: Some(next_id),
1296 values: next_values,
1297 } = &links[j]
1298 {
1299 if next_id == current_id && !next_values.is_empty() {
1300 same_id_values.extend(next_values.clone());
1301 j += 1;
1302 } else {
1303 break;
1304 }
1305 } else {
1306 break;
1307 }
1308 }
1309
1310 if j > i + 1 {
1312 grouped.push(LiNo::Link {
1313 id: Some(current_id.clone()),
1314 values: same_id_values,
1315 });
1316 i = j;
1317 continue;
1318 }
1319 }
1320 }
1321
1322 grouped.push(current.clone());
1323 i += 1;
1324 }
1325
1326 grouped
1327}
1328
1329fn escape_reference(reference: &str) -> String {
1331 quote_reference(reference)
1332}
1333
1334pub fn quote_reference(reference: &str) -> String {
1358 reference_literal::format_reference(reference)
1359}
1360
1361pub fn needs_quoting(reference: &str) -> bool {
1374 quote_reference(reference) != reference
1375}
1376
1377fn has_lone_nested_value<T>(values: &[LiNo<T>]) -> bool {
1381 matches!(values, [LiNo::Link { values: nested, .. }] if !nested.is_empty())
1382}
1383
1384fn needs_parentheses(s: &str) -> bool {
1386 s.contains(' ') || s.contains(':') || s.contains('(') || s.contains(')')
1387}
1388
1389fn format_value<T: ToString>(value: &LiNo<T>) -> String {
1391 match value {
1392 LiNo::Ref(r) => escape_reference(&r.to_string()),
1393 LiNo::Link { id, values } => {
1394 if values.is_empty() {
1396 if let Some(id_val) = id {
1397 return escape_reference(&id_val.to_string());
1398 }
1399 return String::new();
1400 }
1401 format!("{}", value)
1403 }
1404 }
1405}
1406
1407macro_rules! impl_tuple_from {
1444 (@str_tuple 2, $t0:tt, $t1:tt) => {
1446 impl From<(&str, &str)> for LiNo<String> {
1447 fn from(tuple: (&str, &str)) -> Self {
1448 LiNo::Link {
1449 id: Some(tuple.$t0.to_string()),
1450 values: vec![LiNo::Ref(tuple.$t1.to_string())],
1451 }
1452 }
1453 }
1454 };
1455 (@string_tuple 2, $t0:tt, $t1:tt) => {
1456 impl From<(String, String)> for LiNo<String> {
1457 fn from(tuple: (String, String)) -> Self {
1458 LiNo::Link {
1459 id: Some(tuple.$t0),
1460 values: vec![LiNo::Ref(tuple.$t1)],
1461 }
1462 }
1463 }
1464 };
1465 (@str_lino_tuple 2, $t0:tt, $t1:tt) => {
1466 impl From<(&str, LiNo<String>)> for LiNo<String> {
1467 fn from(tuple: (&str, LiNo<String>)) -> Self {
1468 LiNo::Link {
1469 id: Some(tuple.$t0.to_string()),
1470 values: vec![tuple.$t1],
1471 }
1472 }
1473 }
1474 };
1475 (@lino_tuple 2, $t0:tt, $t1:tt) => {
1476 impl From<(LiNo<String>, LiNo<String>)> for LiNo<String> {
1477 fn from(tuple: (LiNo<String>, LiNo<String>)) -> Self {
1478 LiNo::Link {
1479 id: None,
1480 values: vec![tuple.$t0, tuple.$t1],
1481 }
1482 }
1483 }
1484 };
1485
1486 (@str_tuple 3, $t0:tt, $t1:tt, $t2:tt) => {
1488 impl From<(&str, &str, &str)> for LiNo<String> {
1489 fn from(tuple: (&str, &str, &str)) -> Self {
1490 LiNo::Link {
1491 id: Some(tuple.$t0.to_string()),
1492 values: vec![LiNo::Ref(tuple.$t1.to_string()), LiNo::Ref(tuple.$t2.to_string())],
1493 }
1494 }
1495 }
1496 };
1497 (@string_tuple 3, $t0:tt, $t1:tt, $t2:tt) => {
1498 impl From<(String, String, String)> for LiNo<String> {
1499 fn from(tuple: (String, String, String)) -> Self {
1500 LiNo::Link {
1501 id: Some(tuple.$t0),
1502 values: vec![LiNo::Ref(tuple.$t1), LiNo::Ref(tuple.$t2)],
1503 }
1504 }
1505 }
1506 };
1507 (@str_lino_tuple 3, $t0:tt, $t1:tt, $t2:tt) => {
1508 impl From<(&str, LiNo<String>, LiNo<String>)> for LiNo<String> {
1509 fn from(tuple: (&str, LiNo<String>, LiNo<String>)) -> Self {
1510 LiNo::Link {
1511 id: Some(tuple.$t0.to_string()),
1512 values: vec![tuple.$t1, tuple.$t2],
1513 }
1514 }
1515 }
1516 };
1517 (@lino_tuple 3, $t0:tt, $t1:tt, $t2:tt) => {
1518 impl From<(LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
1519 fn from(tuple: (LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
1520 LiNo::Link {
1521 id: None,
1522 values: vec![tuple.$t0, tuple.$t1, tuple.$t2],
1523 }
1524 }
1525 }
1526 };
1527
1528 (@str_tuple 4, $t0:tt, $t1:tt, $t2:tt, $t3:tt) => {
1530 impl From<(&str, &str, &str, &str)> for LiNo<String> {
1531 fn from(tuple: (&str, &str, &str, &str)) -> Self {
1532 LiNo::Link {
1533 id: Some(tuple.$t0.to_string()),
1534 values: vec![
1535 LiNo::Ref(tuple.$t1.to_string()),
1536 LiNo::Ref(tuple.$t2.to_string()),
1537 LiNo::Ref(tuple.$t3.to_string()),
1538 ],
1539 }
1540 }
1541 }
1542 };
1543 (@string_tuple 4, $t0:tt, $t1:tt, $t2:tt, $t3:tt) => {
1544 impl From<(String, String, String, String)> for LiNo<String> {
1545 fn from(tuple: (String, String, String, String)) -> Self {
1546 LiNo::Link {
1547 id: Some(tuple.$t0),
1548 values: vec![LiNo::Ref(tuple.$t1), LiNo::Ref(tuple.$t2), LiNo::Ref(tuple.$t3)],
1549 }
1550 }
1551 }
1552 };
1553 (@str_lino_tuple 4, $t0:tt, $t1:tt, $t2:tt, $t3:tt) => {
1554 impl From<(&str, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
1555 fn from(tuple: (&str, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
1556 LiNo::Link {
1557 id: Some(tuple.$t0.to_string()),
1558 values: vec![tuple.$t1, tuple.$t2, tuple.$t3],
1559 }
1560 }
1561 }
1562 };
1563 (@lino_tuple 4, $t0:tt, $t1:tt, $t2:tt, $t3:tt) => {
1564 impl From<(LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
1565 fn from(tuple: (LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
1566 LiNo::Link {
1567 id: None,
1568 values: vec![tuple.$t0, tuple.$t1, tuple.$t2, tuple.$t3],
1569 }
1570 }
1571 }
1572 };
1573
1574 (@str_tuple 5, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt) => {
1576 impl From<(&str, &str, &str, &str, &str)> for LiNo<String> {
1577 fn from(tuple: (&str, &str, &str, &str, &str)) -> Self {
1578 LiNo::Link {
1579 id: Some(tuple.$t0.to_string()),
1580 values: vec![
1581 LiNo::Ref(tuple.$t1.to_string()),
1582 LiNo::Ref(tuple.$t2.to_string()),
1583 LiNo::Ref(tuple.$t3.to_string()),
1584 LiNo::Ref(tuple.$t4.to_string()),
1585 ],
1586 }
1587 }
1588 }
1589 };
1590 (@string_tuple 5, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt) => {
1591 impl From<(String, String, String, String, String)> for LiNo<String> {
1592 fn from(tuple: (String, String, String, String, String)) -> Self {
1593 LiNo::Link {
1594 id: Some(tuple.$t0),
1595 values: vec![
1596 LiNo::Ref(tuple.$t1),
1597 LiNo::Ref(tuple.$t2),
1598 LiNo::Ref(tuple.$t3),
1599 LiNo::Ref(tuple.$t4),
1600 ],
1601 }
1602 }
1603 }
1604 };
1605 (@str_lino_tuple 5, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt) => {
1606 impl From<(&str, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
1607 fn from(tuple: (&str, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
1608 LiNo::Link {
1609 id: Some(tuple.$t0.to_string()),
1610 values: vec![tuple.$t1, tuple.$t2, tuple.$t3, tuple.$t4],
1611 }
1612 }
1613 }
1614 };
1615 (@lino_tuple 5, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt) => {
1616 impl From<(LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
1617 fn from(tuple: (LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
1618 LiNo::Link {
1619 id: None,
1620 values: vec![tuple.$t0, tuple.$t1, tuple.$t2, tuple.$t3, tuple.$t4],
1621 }
1622 }
1623 }
1624 };
1625
1626 (@str_tuple 6, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt) => {
1628 impl From<(&str, &str, &str, &str, &str, &str)> for LiNo<String> {
1629 fn from(tuple: (&str, &str, &str, &str, &str, &str)) -> Self {
1630 LiNo::Link {
1631 id: Some(tuple.$t0.to_string()),
1632 values: vec![
1633 LiNo::Ref(tuple.$t1.to_string()),
1634 LiNo::Ref(tuple.$t2.to_string()),
1635 LiNo::Ref(tuple.$t3.to_string()),
1636 LiNo::Ref(tuple.$t4.to_string()),
1637 LiNo::Ref(tuple.$t5.to_string()),
1638 ],
1639 }
1640 }
1641 }
1642 };
1643 (@string_tuple 6, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt) => {
1644 impl From<(String, String, String, String, String, String)> for LiNo<String> {
1645 fn from(tuple: (String, String, String, String, String, String)) -> Self {
1646 LiNo::Link {
1647 id: Some(tuple.$t0),
1648 values: vec![
1649 LiNo::Ref(tuple.$t1),
1650 LiNo::Ref(tuple.$t2),
1651 LiNo::Ref(tuple.$t3),
1652 LiNo::Ref(tuple.$t4),
1653 LiNo::Ref(tuple.$t5),
1654 ],
1655 }
1656 }
1657 }
1658 };
1659 (@str_lino_tuple 6, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt) => {
1660 impl From<(&str, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
1661 fn from(tuple: (&str, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
1662 LiNo::Link {
1663 id: Some(tuple.$t0.to_string()),
1664 values: vec![tuple.$t1, tuple.$t2, tuple.$t3, tuple.$t4, tuple.$t5],
1665 }
1666 }
1667 }
1668 };
1669 (@lino_tuple 6, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt) => {
1670 impl From<(LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
1671 fn from(tuple: (LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
1672 LiNo::Link {
1673 id: None,
1674 values: vec![tuple.$t0, tuple.$t1, tuple.$t2, tuple.$t3, tuple.$t4, tuple.$t5],
1675 }
1676 }
1677 }
1678 };
1679
1680 (@str_tuple 7, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt) => {
1682 impl From<(&str, &str, &str, &str, &str, &str, &str)> for LiNo<String> {
1683 fn from(tuple: (&str, &str, &str, &str, &str, &str, &str)) -> Self {
1684 LiNo::Link {
1685 id: Some(tuple.$t0.to_string()),
1686 values: vec![
1687 LiNo::Ref(tuple.$t1.to_string()),
1688 LiNo::Ref(tuple.$t2.to_string()),
1689 LiNo::Ref(tuple.$t3.to_string()),
1690 LiNo::Ref(tuple.$t4.to_string()),
1691 LiNo::Ref(tuple.$t5.to_string()),
1692 LiNo::Ref(tuple.$t6.to_string()),
1693 ],
1694 }
1695 }
1696 }
1697 };
1698 (@string_tuple 7, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt) => {
1699 impl From<(String, String, String, String, String, String, String)> for LiNo<String> {
1700 fn from(tuple: (String, String, String, String, String, String, String)) -> Self {
1701 LiNo::Link {
1702 id: Some(tuple.$t0),
1703 values: vec![
1704 LiNo::Ref(tuple.$t1),
1705 LiNo::Ref(tuple.$t2),
1706 LiNo::Ref(tuple.$t3),
1707 LiNo::Ref(tuple.$t4),
1708 LiNo::Ref(tuple.$t5),
1709 LiNo::Ref(tuple.$t6),
1710 ],
1711 }
1712 }
1713 }
1714 };
1715 (@str_lino_tuple 7, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt) => {
1716 impl From<(&str, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
1717 fn from(tuple: (&str, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
1718 LiNo::Link {
1719 id: Some(tuple.$t0.to_string()),
1720 values: vec![tuple.$t1, tuple.$t2, tuple.$t3, tuple.$t4, tuple.$t5, tuple.$t6],
1721 }
1722 }
1723 }
1724 };
1725 (@lino_tuple 7, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt) => {
1726 impl From<(LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
1727 fn from(tuple: (LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
1728 LiNo::Link {
1729 id: None,
1730 values: vec![tuple.$t0, tuple.$t1, tuple.$t2, tuple.$t3, tuple.$t4, tuple.$t5, tuple.$t6],
1731 }
1732 }
1733 }
1734 };
1735
1736 (@str_tuple 8, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt) => {
1738 impl From<(&str, &str, &str, &str, &str, &str, &str, &str)> for LiNo<String> {
1739 fn from(tuple: (&str, &str, &str, &str, &str, &str, &str, &str)) -> Self {
1740 LiNo::Link {
1741 id: Some(tuple.$t0.to_string()),
1742 values: vec![
1743 LiNo::Ref(tuple.$t1.to_string()),
1744 LiNo::Ref(tuple.$t2.to_string()),
1745 LiNo::Ref(tuple.$t3.to_string()),
1746 LiNo::Ref(tuple.$t4.to_string()),
1747 LiNo::Ref(tuple.$t5.to_string()),
1748 LiNo::Ref(tuple.$t6.to_string()),
1749 LiNo::Ref(tuple.$t7.to_string()),
1750 ],
1751 }
1752 }
1753 }
1754 };
1755 (@string_tuple 8, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt) => {
1756 impl From<(String, String, String, String, String, String, String, String)> for LiNo<String> {
1757 fn from(tuple: (String, String, String, String, String, String, String, String)) -> Self {
1758 LiNo::Link {
1759 id: Some(tuple.$t0),
1760 values: vec![
1761 LiNo::Ref(tuple.$t1),
1762 LiNo::Ref(tuple.$t2),
1763 LiNo::Ref(tuple.$t3),
1764 LiNo::Ref(tuple.$t4),
1765 LiNo::Ref(tuple.$t5),
1766 LiNo::Ref(tuple.$t6),
1767 LiNo::Ref(tuple.$t7),
1768 ],
1769 }
1770 }
1771 }
1772 };
1773 (@str_lino_tuple 8, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt) => {
1774 impl From<(&str, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
1775 fn from(tuple: (&str, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
1776 LiNo::Link {
1777 id: Some(tuple.$t0.to_string()),
1778 values: vec![tuple.$t1, tuple.$t2, tuple.$t3, tuple.$t4, tuple.$t5, tuple.$t6, tuple.$t7],
1779 }
1780 }
1781 }
1782 };
1783 (@lino_tuple 8, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt) => {
1784 impl From<(LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
1785 fn from(tuple: (LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
1786 LiNo::Link {
1787 id: None,
1788 values: vec![tuple.$t0, tuple.$t1, tuple.$t2, tuple.$t3, tuple.$t4, tuple.$t5, tuple.$t6, tuple.$t7],
1789 }
1790 }
1791 }
1792 };
1793
1794 (@str_tuple 9, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt, $t8:tt) => {
1796 impl From<(&str, &str, &str, &str, &str, &str, &str, &str, &str)> for LiNo<String> {
1797 fn from(tuple: (&str, &str, &str, &str, &str, &str, &str, &str, &str)) -> Self {
1798 LiNo::Link {
1799 id: Some(tuple.$t0.to_string()),
1800 values: vec![
1801 LiNo::Ref(tuple.$t1.to_string()),
1802 LiNo::Ref(tuple.$t2.to_string()),
1803 LiNo::Ref(tuple.$t3.to_string()),
1804 LiNo::Ref(tuple.$t4.to_string()),
1805 LiNo::Ref(tuple.$t5.to_string()),
1806 LiNo::Ref(tuple.$t6.to_string()),
1807 LiNo::Ref(tuple.$t7.to_string()),
1808 LiNo::Ref(tuple.$t8.to_string()),
1809 ],
1810 }
1811 }
1812 }
1813 };
1814 (@string_tuple 9, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt, $t8:tt) => {
1815 impl From<(String, String, String, String, String, String, String, String, String)> for LiNo<String> {
1816 fn from(tuple: (String, String, String, String, String, String, String, String, String)) -> Self {
1817 LiNo::Link {
1818 id: Some(tuple.$t0),
1819 values: vec![
1820 LiNo::Ref(tuple.$t1),
1821 LiNo::Ref(tuple.$t2),
1822 LiNo::Ref(tuple.$t3),
1823 LiNo::Ref(tuple.$t4),
1824 LiNo::Ref(tuple.$t5),
1825 LiNo::Ref(tuple.$t6),
1826 LiNo::Ref(tuple.$t7),
1827 LiNo::Ref(tuple.$t8),
1828 ],
1829 }
1830 }
1831 }
1832 };
1833 (@str_lino_tuple 9, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt, $t8:tt) => {
1834 impl From<(&str, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
1835 fn from(tuple: (&str, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
1836 LiNo::Link {
1837 id: Some(tuple.$t0.to_string()),
1838 values: vec![tuple.$t1, tuple.$t2, tuple.$t3, tuple.$t4, tuple.$t5, tuple.$t6, tuple.$t7, tuple.$t8],
1839 }
1840 }
1841 }
1842 };
1843 (@lino_tuple 9, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt, $t8:tt) => {
1844 impl From<(LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
1845 fn from(tuple: (LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
1846 LiNo::Link {
1847 id: None,
1848 values: vec![tuple.$t0, tuple.$t1, tuple.$t2, tuple.$t3, tuple.$t4, tuple.$t5, tuple.$t6, tuple.$t7, tuple.$t8],
1849 }
1850 }
1851 }
1852 };
1853
1854 (@str_tuple 10, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt, $t8:tt, $t9:tt) => {
1856 impl From<(&str, &str, &str, &str, &str, &str, &str, &str, &str, &str)> for LiNo<String> {
1857 fn from(tuple: (&str, &str, &str, &str, &str, &str, &str, &str, &str, &str)) -> Self {
1858 LiNo::Link {
1859 id: Some(tuple.$t0.to_string()),
1860 values: vec![
1861 LiNo::Ref(tuple.$t1.to_string()),
1862 LiNo::Ref(tuple.$t2.to_string()),
1863 LiNo::Ref(tuple.$t3.to_string()),
1864 LiNo::Ref(tuple.$t4.to_string()),
1865 LiNo::Ref(tuple.$t5.to_string()),
1866 LiNo::Ref(tuple.$t6.to_string()),
1867 LiNo::Ref(tuple.$t7.to_string()),
1868 LiNo::Ref(tuple.$t8.to_string()),
1869 LiNo::Ref(tuple.$t9.to_string()),
1870 ],
1871 }
1872 }
1873 }
1874 };
1875 (@string_tuple 10, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt, $t8:tt, $t9:tt) => {
1876 impl From<(String, String, String, String, String, String, String, String, String, String)> for LiNo<String> {
1877 fn from(tuple: (String, String, String, String, String, String, String, String, String, String)) -> Self {
1878 LiNo::Link {
1879 id: Some(tuple.$t0),
1880 values: vec![
1881 LiNo::Ref(tuple.$t1),
1882 LiNo::Ref(tuple.$t2),
1883 LiNo::Ref(tuple.$t3),
1884 LiNo::Ref(tuple.$t4),
1885 LiNo::Ref(tuple.$t5),
1886 LiNo::Ref(tuple.$t6),
1887 LiNo::Ref(tuple.$t7),
1888 LiNo::Ref(tuple.$t8),
1889 LiNo::Ref(tuple.$t9),
1890 ],
1891 }
1892 }
1893 }
1894 };
1895 (@str_lino_tuple 10, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt, $t8:tt, $t9:tt) => {
1896 impl From<(&str, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
1897 fn from(tuple: (&str, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
1898 LiNo::Link {
1899 id: Some(tuple.$t0.to_string()),
1900 values: vec![tuple.$t1, tuple.$t2, tuple.$t3, tuple.$t4, tuple.$t5, tuple.$t6, tuple.$t7, tuple.$t8, tuple.$t9],
1901 }
1902 }
1903 }
1904 };
1905 (@lino_tuple 10, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt, $t8:tt, $t9:tt) => {
1906 impl From<(LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
1907 fn from(tuple: (LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
1908 LiNo::Link {
1909 id: None,
1910 values: vec![tuple.$t0, tuple.$t1, tuple.$t2, tuple.$t3, tuple.$t4, tuple.$t5, tuple.$t6, tuple.$t7, tuple.$t8, tuple.$t9],
1911 }
1912 }
1913 }
1914 };
1915
1916 (@str_tuple 11, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt, $t8:tt, $t9:tt, $t10:tt) => {
1918 impl From<(&str, &str, &str, &str, &str, &str, &str, &str, &str, &str, &str)> for LiNo<String> {
1919 fn from(tuple: (&str, &str, &str, &str, &str, &str, &str, &str, &str, &str, &str)) -> Self {
1920 LiNo::Link {
1921 id: Some(tuple.$t0.to_string()),
1922 values: vec![
1923 LiNo::Ref(tuple.$t1.to_string()),
1924 LiNo::Ref(tuple.$t2.to_string()),
1925 LiNo::Ref(tuple.$t3.to_string()),
1926 LiNo::Ref(tuple.$t4.to_string()),
1927 LiNo::Ref(tuple.$t5.to_string()),
1928 LiNo::Ref(tuple.$t6.to_string()),
1929 LiNo::Ref(tuple.$t7.to_string()),
1930 LiNo::Ref(tuple.$t8.to_string()),
1931 LiNo::Ref(tuple.$t9.to_string()),
1932 LiNo::Ref(tuple.$t10.to_string()),
1933 ],
1934 }
1935 }
1936 }
1937 };
1938 (@string_tuple 11, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt, $t8:tt, $t9:tt, $t10:tt) => {
1939 impl From<(String, String, String, String, String, String, String, String, String, String, String)> for LiNo<String> {
1940 fn from(tuple: (String, String, String, String, String, String, String, String, String, String, String)) -> Self {
1941 LiNo::Link {
1942 id: Some(tuple.$t0),
1943 values: vec![
1944 LiNo::Ref(tuple.$t1),
1945 LiNo::Ref(tuple.$t2),
1946 LiNo::Ref(tuple.$t3),
1947 LiNo::Ref(tuple.$t4),
1948 LiNo::Ref(tuple.$t5),
1949 LiNo::Ref(tuple.$t6),
1950 LiNo::Ref(tuple.$t7),
1951 LiNo::Ref(tuple.$t8),
1952 LiNo::Ref(tuple.$t9),
1953 LiNo::Ref(tuple.$t10),
1954 ],
1955 }
1956 }
1957 }
1958 };
1959 (@str_lino_tuple 11, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt, $t8:tt, $t9:tt, $t10:tt) => {
1960 impl From<(&str, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
1961 fn from(tuple: (&str, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
1962 LiNo::Link {
1963 id: Some(tuple.$t0.to_string()),
1964 values: vec![tuple.$t1, tuple.$t2, tuple.$t3, tuple.$t4, tuple.$t5, tuple.$t6, tuple.$t7, tuple.$t8, tuple.$t9, tuple.$t10],
1965 }
1966 }
1967 }
1968 };
1969 (@lino_tuple 11, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt, $t8:tt, $t9:tt, $t10:tt) => {
1970 impl From<(LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
1971 fn from(tuple: (LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
1972 LiNo::Link {
1973 id: None,
1974 values: vec![tuple.$t0, tuple.$t1, tuple.$t2, tuple.$t3, tuple.$t4, tuple.$t5, tuple.$t6, tuple.$t7, tuple.$t8, tuple.$t9, tuple.$t10],
1975 }
1976 }
1977 }
1978 };
1979
1980 (@str_tuple 12, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt, $t8:tt, $t9:tt, $t10:tt, $t11:tt) => {
1982 impl From<(&str, &str, &str, &str, &str, &str, &str, &str, &str, &str, &str, &str)> for LiNo<String> {
1983 fn from(tuple: (&str, &str, &str, &str, &str, &str, &str, &str, &str, &str, &str, &str)) -> Self {
1984 LiNo::Link {
1985 id: Some(tuple.$t0.to_string()),
1986 values: vec![
1987 LiNo::Ref(tuple.$t1.to_string()),
1988 LiNo::Ref(tuple.$t2.to_string()),
1989 LiNo::Ref(tuple.$t3.to_string()),
1990 LiNo::Ref(tuple.$t4.to_string()),
1991 LiNo::Ref(tuple.$t5.to_string()),
1992 LiNo::Ref(tuple.$t6.to_string()),
1993 LiNo::Ref(tuple.$t7.to_string()),
1994 LiNo::Ref(tuple.$t8.to_string()),
1995 LiNo::Ref(tuple.$t9.to_string()),
1996 LiNo::Ref(tuple.$t10.to_string()),
1997 LiNo::Ref(tuple.$t11.to_string()),
1998 ],
1999 }
2000 }
2001 }
2002 };
2003 (@string_tuple 12, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt, $t8:tt, $t9:tt, $t10:tt, $t11:tt) => {
2004 impl From<(String, String, String, String, String, String, String, String, String, String, String, String)> for LiNo<String> {
2005 fn from(tuple: (String, String, String, String, String, String, String, String, String, String, String, String)) -> Self {
2006 LiNo::Link {
2007 id: Some(tuple.$t0),
2008 values: vec![
2009 LiNo::Ref(tuple.$t1),
2010 LiNo::Ref(tuple.$t2),
2011 LiNo::Ref(tuple.$t3),
2012 LiNo::Ref(tuple.$t4),
2013 LiNo::Ref(tuple.$t5),
2014 LiNo::Ref(tuple.$t6),
2015 LiNo::Ref(tuple.$t7),
2016 LiNo::Ref(tuple.$t8),
2017 LiNo::Ref(tuple.$t9),
2018 LiNo::Ref(tuple.$t10),
2019 LiNo::Ref(tuple.$t11),
2020 ],
2021 }
2022 }
2023 }
2024 };
2025 (@str_lino_tuple 12, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt, $t8:tt, $t9:tt, $t10:tt, $t11:tt) => {
2026 impl From<(&str, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
2027 fn from(tuple: (&str, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
2028 LiNo::Link {
2029 id: Some(tuple.$t0.to_string()),
2030 values: vec![tuple.$t1, tuple.$t2, tuple.$t3, tuple.$t4, tuple.$t5, tuple.$t6, tuple.$t7, tuple.$t8, tuple.$t9, tuple.$t10, tuple.$t11],
2031 }
2032 }
2033 }
2034 };
2035 (@lino_tuple 12, $t0:tt, $t1:tt, $t2:tt, $t3:tt, $t4:tt, $t5:tt, $t6:tt, $t7:tt, $t8:tt, $t9:tt, $t10:tt, $t11:tt) => {
2036 impl From<(LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)> for LiNo<String> {
2037 fn from(tuple: (LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>, LiNo<String>)) -> Self {
2038 LiNo::Link {
2039 id: None,
2040 values: vec![tuple.$t0, tuple.$t1, tuple.$t2, tuple.$t3, tuple.$t4, tuple.$t5, tuple.$t6, tuple.$t7, tuple.$t8, tuple.$t9, tuple.$t10, tuple.$t11],
2041 }
2042 }
2043 }
2044 };
2045
2046 (2) => {
2048 impl_tuple_from!(@str_tuple 2, 0, 1);
2049 impl_tuple_from!(@string_tuple 2, 0, 1);
2050 impl_tuple_from!(@str_lino_tuple 2, 0, 1);
2051 impl_tuple_from!(@lino_tuple 2, 0, 1);
2052 };
2053 (3) => {
2054 impl_tuple_from!(@str_tuple 3, 0, 1, 2);
2055 impl_tuple_from!(@string_tuple 3, 0, 1, 2);
2056 impl_tuple_from!(@str_lino_tuple 3, 0, 1, 2);
2057 impl_tuple_from!(@lino_tuple 3, 0, 1, 2);
2058 };
2059 (4) => {
2060 impl_tuple_from!(@str_tuple 4, 0, 1, 2, 3);
2061 impl_tuple_from!(@string_tuple 4, 0, 1, 2, 3);
2062 impl_tuple_from!(@str_lino_tuple 4, 0, 1, 2, 3);
2063 impl_tuple_from!(@lino_tuple 4, 0, 1, 2, 3);
2064 };
2065 (5) => {
2066 impl_tuple_from!(@str_tuple 5, 0, 1, 2, 3, 4);
2067 impl_tuple_from!(@string_tuple 5, 0, 1, 2, 3, 4);
2068 impl_tuple_from!(@str_lino_tuple 5, 0, 1, 2, 3, 4);
2069 impl_tuple_from!(@lino_tuple 5, 0, 1, 2, 3, 4);
2070 };
2071 (6) => {
2072 impl_tuple_from!(@str_tuple 6, 0, 1, 2, 3, 4, 5);
2073 impl_tuple_from!(@string_tuple 6, 0, 1, 2, 3, 4, 5);
2074 impl_tuple_from!(@str_lino_tuple 6, 0, 1, 2, 3, 4, 5);
2075 impl_tuple_from!(@lino_tuple 6, 0, 1, 2, 3, 4, 5);
2076 };
2077 (7) => {
2078 impl_tuple_from!(@str_tuple 7, 0, 1, 2, 3, 4, 5, 6);
2079 impl_tuple_from!(@string_tuple 7, 0, 1, 2, 3, 4, 5, 6);
2080 impl_tuple_from!(@str_lino_tuple 7, 0, 1, 2, 3, 4, 5, 6);
2081 impl_tuple_from!(@lino_tuple 7, 0, 1, 2, 3, 4, 5, 6);
2082 };
2083 (8) => {
2084 impl_tuple_from!(@str_tuple 8, 0, 1, 2, 3, 4, 5, 6, 7);
2085 impl_tuple_from!(@string_tuple 8, 0, 1, 2, 3, 4, 5, 6, 7);
2086 impl_tuple_from!(@str_lino_tuple 8, 0, 1, 2, 3, 4, 5, 6, 7);
2087 impl_tuple_from!(@lino_tuple 8, 0, 1, 2, 3, 4, 5, 6, 7);
2088 };
2089 (9) => {
2090 impl_tuple_from!(@str_tuple 9, 0, 1, 2, 3, 4, 5, 6, 7, 8);
2091 impl_tuple_from!(@string_tuple 9, 0, 1, 2, 3, 4, 5, 6, 7, 8);
2092 impl_tuple_from!(@str_lino_tuple 9, 0, 1, 2, 3, 4, 5, 6, 7, 8);
2093 impl_tuple_from!(@lino_tuple 9, 0, 1, 2, 3, 4, 5, 6, 7, 8);
2094 };
2095 (10) => {
2096 impl_tuple_from!(@str_tuple 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
2097 impl_tuple_from!(@string_tuple 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
2098 impl_tuple_from!(@str_lino_tuple 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
2099 impl_tuple_from!(@lino_tuple 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
2100 };
2101 (11) => {
2102 impl_tuple_from!(@str_tuple 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
2103 impl_tuple_from!(@string_tuple 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
2104 impl_tuple_from!(@str_lino_tuple 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
2105 impl_tuple_from!(@lino_tuple 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
2106 };
2107 (12) => {
2108 impl_tuple_from!(@str_tuple 12, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2109 impl_tuple_from!(@string_tuple 12, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2110 impl_tuple_from!(@str_lino_tuple 12, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2111 impl_tuple_from!(@lino_tuple 12, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2112 };
2113}
2114
2115impl_tuple_from!(2);
2118impl_tuple_from!(3);
2119impl_tuple_from!(4);
2120impl_tuple_from!(5);
2121impl_tuple_from!(6);
2122impl_tuple_from!(7);
2123impl_tuple_from!(8);
2124impl_tuple_from!(9);
2125impl_tuple_from!(10);
2126impl_tuple_from!(11);
2127impl_tuple_from!(12);
2128
2129impl From<Vec<&str>> for LiNo<String> {
2160 fn from(values: Vec<&str>) -> Self {
2161 LiNo::Link {
2162 id: None,
2163 values: values
2164 .into_iter()
2165 .map(|s| LiNo::Ref(s.to_string()))
2166 .collect(),
2167 }
2168 }
2169}
2170
2171impl From<Vec<String>> for LiNo<String> {
2173 fn from(values: Vec<String>) -> Self {
2174 LiNo::Link {
2175 id: None,
2176 values: values.into_iter().map(LiNo::Ref).collect(),
2177 }
2178 }
2179}
2180
2181impl From<Vec<LiNo<String>>> for LiNo<String> {
2183 fn from(values: Vec<LiNo<String>>) -> Self {
2184 LiNo::Link { id: None, values }
2185 }
2186}
2187
2188impl From<(&str, Vec<&str>)> for LiNo<String> {
2200 fn from((id, values): (&str, Vec<&str>)) -> Self {
2201 LiNo::Link {
2202 id: Some(id.to_string()),
2203 values: values
2204 .into_iter()
2205 .map(|s| LiNo::Ref(s.to_string()))
2206 .collect(),
2207 }
2208 }
2209}
2210
2211impl From<(String, Vec<String>)> for LiNo<String> {
2213 fn from((id, values): (String, Vec<String>)) -> Self {
2214 LiNo::Link {
2215 id: Some(id),
2216 values: values.into_iter().map(LiNo::Ref).collect(),
2217 }
2218 }
2219}
2220
2221impl From<(&str, Vec<LiNo<String>>)> for LiNo<String> {
2223 fn from((id, values): (&str, Vec<LiNo<String>>)) -> Self {
2224 LiNo::Link {
2225 id: Some(id.to_string()),
2226 values,
2227 }
2228 }
2229}
2230
2231impl From<(String, Vec<LiNo<String>>)> for LiNo<String> {
2233 fn from((id, values): (String, Vec<LiNo<String>>)) -> Self {
2234 LiNo::Link {
2235 id: Some(id),
2236 values,
2237 }
2238 }
2239}