1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
mod kind;
mod index;
mod parser;

pub use kind::*;
pub use index::*;
pub use parser::*;

#[cfg(feature = "preserve-order")]
pub use indexmap::IndexMap as Map;
#[cfg(not(feature = "preserve-order"))]
pub use std::collections::HashMap as Map;

use regex::Regex;
use std::fmt;

lazy_static! {
    static ref SIMPLE_PATTERN: Regex = Regex::new("^[A-Za-z0-9._+-]+$").unwrap();
}

#[derive(Debug, PartialEq, Clone)]
pub enum Tag {
    End,
    Byte(i8),
    Short(i16),
    Int(i32),
    Long(i64),
    Float(f32),
    Double(f64),
    ByteArray(Vec<i8>),
    String(String),
    List(Vec<Self>),
    Compound(Map<String, Self>),
    IntArray(Vec<i32>),
    LongArray(Vec<i64>),
}

macro_rules! into_tag {
    ($input:ty, $output:ident) => {
        impl Into<Tag> for $input {
            fn into(self) -> Tag {
                Tag::$output(self)
            }
        }

        impl Into<$input> for Tag {
            fn into(self) -> $input {
                match self {
                    Tag::$output(value) => value,
                    _ => panic!("cannot convert tag"),
                }
            }
        }

        impl<'b> Into<$input> for &'b Tag {
            fn into(self) -> $input {
                match self {
                    Tag::$output(value) => value.to_owned(),
                    _ => panic!("cannot convert tag"),
                }
            }
        }
    };
}

macro_rules! into_tags {
    { $({ $input:ty, $output:ident }),* $(,)* } => {
        $(into_tag!($input, $output);)*
    };
}

into_tags! {
    { i8, Byte },
    { i16, Short },
    { i32, Int },
    { i64, Long },

    { f32, Float },
    { f64, Double },

    { String, String },

    { Vec<i8>, ByteArray },
    { Vec<i32>, IntArray },
    { Vec<i64>, LongArray },
}

impl Tag {
    pub fn kind(&self) -> Kind {
        match self {
            Self::End => Kind::End,
            Self::Byte(_) => Kind::Byte,
            Self::Short(_) => Kind::Short,
            Self::Int(_) => Kind::Int,
            Self::Long(_) => Kind::Long,
            Self::Float(_) => Kind::Float,
            Self::Double(_) => Kind::Double,
            Self::ByteArray(_) => Kind::ByteArray,
            Self::String(_) => Kind::String,
            Self::List(v) => Kind::List(
                v.get(0)
                    .map(|tag| tag.kind().id())
                    .unwrap_or(Kind::End.id()),
            ),
            Self::Compound(_) => Kind::Compound,
            Self::IntArray(_) => Kind::IntArray,
            Self::LongArray(_) => Kind::LongArray,
        }
    }

    pub fn get<I: Index>(&self, index: I) -> Option<&Self> {
        index.index_into(self)
    }

    pub fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Self> {
        index.index_into_mut(self)
    }

    pub fn insert<I: Index>(&mut self, index: I, value: Self) {
        *index.index_or_insert(self) = value;
    }
}

fn quote_and_escape(s: &str) -> String {
    let mut builder = String::new();
    let mut quote_chr = None;

    for chr in s.chars() {
        if chr == '\\' {
            builder.push('\\');
        } else if chr == '"' || chr == '\'' {
            if quote_chr.is_none() {
                quote_chr = Some(if chr == '"' { '\'' } else { '"' });
            }

            if quote_chr.is_some() && quote_chr.unwrap() == chr {
                builder.push('\\');
            }
        }

        builder.push(chr);
    }

    let quote_chr = quote_chr.unwrap_or('"');

    builder.insert(0, quote_chr);
    builder.push(quote_chr);
    builder
}

impl fmt::Display for Tag {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", match self {
            Self::End => panic!("cannot convert end to string"),

            Self::Byte(value) => format!("{}b", *value),
            Self::Short(value) => format!("{}s", *value),
            Self::Int(value) => format!("{}", *value),
            Self::Long(value) => format!("{}L", *value),

            Self::Float(value) => format!("{}f", *value),
            Self::Double(value) => format!("{}d", *value),

            Self::String(s) => quote_and_escape(&s),

            Self::List(v) => {
                let mut items = vec![];

                for tag in v {
                    items.push(tag.to_string());
                }

                format!("[{}]", items.join(","))
            },

            Self::Compound(m) => {
                let mut items = vec![];

                for (name, tag) in m {
                    let value = tag.to_string();
                    let key = if SIMPLE_PATTERN.is_match(&name) {
                        name.clone()
                    } else {
                        quote_and_escape(&name)
                    };

                    items.push(format!("{}:{}", key, value));
                }

                format!("{{{}}}", items.join(","))
            },

            Self::ByteArray(v) => {
                let mut items = vec![];

                for value in v {
                    items.push(Tag::Byte(*value).to_string());
                }

                format!("[B;{}]", items.join(","))
            },
            Self::IntArray(v) => {
                let mut items = vec![];

                for value in v {
                    items.push(Tag::Int(*value).to_string());
                }

                format!("[I;{}]", items.join(","))
            },
            Self::LongArray(v) => {
                let mut items = vec![];

                for value in v {
                    items.push(Tag::Long(*value).to_string());
                }

                format!("[L;{}]", items.join(","))
            },
        })
    }
}

#[macro_export]
macro_rules! tag {
    ($s:expr) => {
        $crate::Nbt::parse(format!("{}", $s))
            .expect("failed to parse tag")
    };
}