1use crate::{error::Error, join, raw::raw_value::RawValue};
2use std::fmt::Display;
3
4#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5pub struct Concat {
6 values: Vec<RawValue>,
7 spaces: Vec<Option<String>>,
8}
9
10impl Concat {
11 pub fn new(values: Vec<RawValue>, spaces: Vec<Option<String>>) -> crate::Result<Self> {
12 if values.len() != spaces.len() + 1 {
13 return Err(Error::InvalidConcat(values.len(), spaces.len()));
14 }
15 let concat = Self { values, spaces };
16 for v in &concat.values {
17 if matches!(v, RawValue::Concat(_)) || matches!(v, RawValue::AddAssign(_)) {
18 return Err(Error::InvalidValue {
19 val: v.ty(),
20 ty: "concat",
21 });
22 }
23 }
24 Ok(concat)
25 }
26
27 pub fn into_inner(self) -> (Vec<RawValue>, Vec<Option<String>>) {
28 (self.values, self.spaces)
29 }
30
31 pub fn get_values(&self) -> &Vec<RawValue> {
32 &self.values
33 }
34
35 pub fn get_spaces(&self) -> &Vec<Option<String>> {
36 &self.spaces
37 }
38}
39
40impl Display for Concat {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 join(self.values.iter(), " ", f)
43 }
44}