dfraw_json_parser 0.17.5

Library which parses Dwarf Fortress raw files into JSON
Documentation
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use crate::{creature::Creature, RawObject, VARIATION_ARGUMENT_RE};

use super::Requirements;

/// A variation rule for a creature.
#[derive(
    serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq, Eq, Default, specta::Type,
)]
pub enum CreatureVariationRule {
    /// An unknown rule.
    #[default]
    Unknown,
    /// Removes a tag from a creature.
    RemoveTag {
        /// The tag to remove.
        tag: String,
        /// The value to remove.
        value: Option<String>,
    },
    /// Adds a new tag to a creature.
    NewTag {
        /// The tag to add.
        tag: String,
        /// The value to add.
        value: Option<String>,
    },
    /// Adds a new tag to a creature.
    AddTag {
        /// The tag to add.
        tag: String,
        /// The value to add.
        value: Option<String>,
    },
    /// Converts a tag on a creature.
    ConvertTag {
        /// The tag to convert.
        tag: String,
        /// The target value to convert.
        target: Option<String>,
        /// The replacement value to convert to.
        replacement: Option<String>,
    },
    /// Adds a new tag to a creature if a condition is met.
    ConditionalNewTag {
        /// The tag to add.
        tag: String,
        /// The value to add.
        value: Option<String>,
        /// The index of the argument to check.
        argument_index: usize,
        /// The requirement for the argument.
        argument_requirement: String,
    },
    /// Adds a new tag to a creature if a condition is met.
    ConditionalAddTag {
        /// The tag to add.
        tag: String,
        /// The value to add.
        value: Option<String>,
        /// The index of the argument to check.
        argument_index: usize,
        /// The requirement for the argument.
        argument_requirement: String,
    },
    /// Removes a tag from a creature if a condition is met.
    ConditionalRemoveTag {
        /// The tag to remove.
        tag: String,
        /// The value to remove.
        value: Option<String>,
        /// The index of the argument to check.
        argument_index: usize,
        /// The requirement for the argument.
        argument_requirement: String,
    },
    /// Converts a tag on a creature if a condition is met.
    ConditionalConvertTag {
        /// The tag to convert.
        tag: String,
        /// The target value to convert.
        target: Option<String>,
        /// The replacement value to convert to.
        replacement: Option<String>,
        /// The index of the argument to check.
        argument_index: usize,
        /// The requirement for the argument.
        argument_requirement: String,
    },
}

impl CreatureVariationRule {
    /// Apply a set of arguments to the rule and get a rule that has the arguments applied.
    /// This will replace all instances of `!ARGn` with the corresponding argument.
    ///
    /// This returns a new rule with the arguments applied because we don't want to mutate the
    /// original rule (multiple creatures may use the same rule)
    ///
    /// ## Arguments
    ///
    /// * `args` - The arguments to apply to the rule.
    ///
    /// ## Returns
    ///
    /// * `CreatureVariationRule` - The rule with the arguments applied.
    #[must_use]
    #[allow(clippy::too_many_lines)]
    pub fn with_args(&self, args: &[&str]) -> Self {
        // Short circuit if there are no arguments to replace.
        if args.is_empty() {
            return self.clone();
        }
        // We simply replace all instances of `!ARGn` with the corresponding argument.
        match self {
            Self::RemoveTag { tag, value } => {
                // Only have the tag to replace.
                Self::RemoveTag {
                    tag: replace_args_in_string(tag, args),
                    value: value
                        .as_ref()
                        .map(|value| replace_args_in_string(value, args)),
                }
            }
            Self::NewTag { tag, value } | Self::AddTag { tag, value } => {
                // Have both the tag and the value to replace.
                Self::NewTag {
                    tag: replace_args_in_string(tag, args),
                    value: value
                        .as_ref()
                        .map(|value| replace_args_in_string(value, args)),
                }
            }
            Self::ConvertTag {
                tag,
                target,
                replacement,
            } => {
                // Have the tag, target, and replacement to replace.
                Self::ConvertTag {
                    tag: replace_args_in_string(tag, args),
                    target: target
                        .as_ref()
                        .map(|value| replace_args_in_string(value, args)),
                    replacement: replacement
                        .as_ref()
                        .map(|value| replace_args_in_string(value, args)),
                }
            }
            Self::ConditionalRemoveTag {
                tag,
                value,
                argument_requirement,
                argument_index,
            } => {
                // Have the tag and the argument requirement to replace.
                Self::ConditionalRemoveTag {
                    tag: replace_args_in_string(tag, args),
                    value: value
                        .as_ref()
                        .map(|value| replace_args_in_string(value, args)),
                    argument_requirement: String::from(
                        VARIATION_ARGUMENT_RE.replace_all(
                            argument_requirement.as_str(),
                            |caps: &regex::Captures| argument_as_string(caps, args),
                        ),
                    ),
                    argument_index: *argument_index,
                }
            }
            Self::ConditionalNewTag {
                tag,
                value,
                argument_requirement,
                argument_index,
            }
            | Self::ConditionalAddTag {
                tag,
                value,
                argument_requirement,
                argument_index,
            } => {
                // Have the tag, value, and argument requirement to replace.
                Self::ConditionalNewTag {
                    tag: replace_args_in_string(tag, args),
                    value: value
                        .as_ref()
                        .map(|value| replace_args_in_string(value, args)),
                    argument_requirement: String::from(
                        VARIATION_ARGUMENT_RE.replace_all(
                            argument_requirement.as_str(),
                            |caps: &regex::Captures| argument_as_string(caps, args),
                        ),
                    ),
                    argument_index: *argument_index,
                }
            }
            Self::ConditionalConvertTag {
                tag,
                target,
                replacement,
                argument_index,
                argument_requirement,
            } => {
                // Have the tag, target, replacement, and argument requirement to replace.
                Self::ConditionalConvertTag {
                    tag: replace_args_in_string(tag, args),
                    target: target
                        .as_ref()
                        .map(|value| replace_args_in_string(value, args)),
                    replacement: replacement
                        .as_ref()
                        .map(|value| replace_args_in_string(value, args)),
                    argument_requirement: String::from(
                        VARIATION_ARGUMENT_RE.replace_all(
                            argument_requirement.as_str(),
                            |caps: &regex::Captures| argument_as_string(caps, args),
                        ),
                    ),
                    argument_index: *argument_index,
                }
            }
            Self::Unknown => {
                // Unknown rules don't have anything to replace.
                Self::Unknown
            }
        }
    }
    /// Apply the rule to a creature. This will apply the rule to the creature based on the arguments
    /// provided.
    ///
    /// # Arguments
    ///
    /// * `creature` - The creature to apply the rule to.
    /// * `args` - The arguments to apply to the rule.
    ///
    /// # Side Effects
    ///
    /// This will modify the creature provided.
    pub fn apply(&self, creature: &mut Creature, args: &[&str]) {
        match self.with_args(args) {
            Self::RemoveTag { tag, .. } => {
                remove_tag(creature, &tag);
            }
            Self::NewTag { tag, value } | Self::AddTag { tag, value } => {
                apply_new_tag(creature, &tag, value.as_deref());
            }
            Self::ConvertTag {
                tag,
                target,
                replacement,
            } => convert_tag(creature, &tag, target.as_deref(), replacement.as_deref()),
            Self::ConditionalNewTag {
                tag,
                value,
                argument_index,
                argument_requirement,
            }
            | Self::ConditionalAddTag {
                tag,
                value,
                argument_index,
                argument_requirement,
            } => {
                // Guard against out of bounds arguments.
                if args.len() < argument_index {
                    tracing::warn!(
                        "Creature Variation Argument index {} is out of bounds for {:?}",
                        argument_index,
                        args
                    );
                    return;
                }
                // Check if the argument matches the requirement.
                if let Some(argument_value) = args.get(argument_index - 1) {
                    if argument_value == &argument_requirement {
                        apply_new_tag(creature, &tag, value.as_deref());
                    }
                }
            }
            Self::ConditionalRemoveTag {
                tag,
                argument_index,
                argument_requirement,
                ..
            } => {
                // Guard against out of bounds arguments.
                if args.len() < argument_index {
                    tracing::warn!(
                        "Creature Variation Argument index {} is out of bounds for {:?}",
                        argument_index,
                        args
                    );
                    return;
                }
                // Check if the argument matches the requirement.
                if let Some(argument_value) = args.get(argument_index - 1) {
                    if argument_value == &argument_requirement {
                        remove_tag(creature, &tag);
                    }
                }
            }
            Self::ConditionalConvertTag {
                tag,
                target,
                replacement,
                argument_index,
                argument_requirement,
            } => {
                // Guard against out of bounds arguments.
                if args.len() < argument_index {
                    tracing::warn!(
                        "Creature Variation Argument index {} is out of bounds for {:?}",
                        argument_index,
                        args
                    );
                    return;
                }
                // Check if the argument matches the requirement.
                if let Some(argument_value) = args.get(argument_index - 1) {
                    if argument_value == &argument_requirement {
                        convert_tag(creature, &tag, target.as_deref(), replacement.as_deref());
                    }
                }
            }
            Self::Unknown => {}
        }
    }
}

/// Replaces all instances of `!ARGn` with the corresponding argument.
///
/// ## Arguments
///
/// * `string` - The string to replace the arguments in.
/// * `args` - The arguments to replace in the string.
///
/// ## Returns
///
/// * `String` - The string with the arguments replaced.
fn replace_args_in_string(string: &str, args: &[&str]) -> String {
    VARIATION_ARGUMENT_RE
        .replace_all(string, |caps: &regex::Captures| {
            argument_as_string(caps, args)
        })
        .to_string()
}

/// ADD or NEW tags can simply be applied by the parsing logic that already exists.
///
/// ## Arguments
///
/// * `creature` - The creature to apply the tag to.
/// * `tag` - The tag to apply.
/// * `value` - The value to apply to the tag.
fn apply_new_tag(creature: &mut Creature, tag: &str, value: Option<&str>) {
    (creature as &mut dyn Requirements).add_tag_and_value(tag, value.unwrap_or_default());
}

/// Removes a tag from a creature.
///
/// ## Arguments
///
/// * `creature` - The creature to remove the tag from.
/// * `tag` - The tag to remove.
fn remove_tag(creature: &mut Creature, tag: &str) {
    (creature as &mut dyn Requirements).remove_tag(tag);
}

/// Converts a tag on a creature.
fn convert_tag(
    creature: &mut Creature,
    tag: &str,
    target: Option<&str>,
    replacement: Option<&str>,
) {
    if let Some(target) = target {
        if let Some(replacement) = replacement {
            tracing::trace!(
                "Converting tag {}:{} to {}:{} on creature {}",
                tag,
                target,
                tag,
                replacement,
                creature.get_identifier()
            );
            // Convert the tag to the target value.
            (creature as &mut dyn Requirements).remove_tag_and_value(tag, target);
            (creature as &mut dyn Requirements).add_tag_and_value(tag, replacement);
        } else {
            tracing::trace!(
                "Converting tag {}:{} to {}:{} on creature {}",
                tag,
                target,
                replacement.unwrap_or_default(),
                target,
                creature.get_identifier(),
            );
            // Convert the tag to the target value.
            (creature as &mut dyn Requirements).remove_tag_and_value(tag, target);
            (creature as &mut dyn Requirements).add_tag_and_value(tag, target);
        }
    } else {
        tracing::trace!(
            "Converting tag {} to {} on creature {}",
            tag,
            replacement.unwrap_or_default(),
            creature.get_identifier()
        );
        // Convert the tag to the replacement value.
        (creature as &mut dyn Requirements).remove_tag(tag);
        (creature as &mut dyn Requirements).add_tag(replacement.unwrap_or_default());
    }
}

/// Returns the argument which matches the given capture group.
/// This expects you to be capturing based on the regex in `VARIATION_ARGUMENT_RE`.
///
/// That way it will match `!ARGn` and `!ARGnn` and `!ARGnnn` and replace with the corresponding
/// argument.
///
/// ## Arguments
///
/// * `caps` - The capture group to get the argument name from.
/// * `args` - The arguments to get the argument from.
///
/// ## Returns
///
/// * `String` - The argument which matches the given capture group.
fn argument_as_string(caps: &regex::Captures, args: &[&str]) -> String {
    if let Some(index) = caps.get(1) {
        let index = index.as_str().parse::<usize>().unwrap_or_default();
        if let Some(argument_value) = args.get(index - 1) {
            return (*argument_value).to_string();
        }
    }
    if let Some(arg) = caps.get(0) {
        tracing::warn!(
            "Creature Variation Argument is invalid. Argument captured: '{}'",
            arg.as_str()
        );
        return arg.as_str().to_string();
    }
    String::new()
}