rust-yaml 0.0.5

A fast, safe YAML 1.2 library for Rust
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
//! YAML tag resolution and handling system
//!
//! This module implements the full YAML 1.2 tag resolution mechanism,
//! including support for custom tag handlers and schema validation.

use crate::{Error, Result, Value};
use std::collections::HashMap;
use std::fmt;

/// Tag handle types as defined in YAML 1.2 spec
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TagHandle {
    /// Primary handle (!)
    Primary,
    /// Secondary handle (!!)
    Secondary,
    /// Named handle (e.g., !e!)
    Named(String),
    /// Verbatim tag (e.g., !<tag:example.com,2024:type>)
    Verbatim,
}

impl fmt::Display for TagHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Primary => write!(f, "!"),
            Self::Secondary => write!(f, "!!"),
            Self::Named(name) => write!(f, "!{}!", name),
            Self::Verbatim => write!(f, "!<>"),
        }
    }
}

/// A resolved YAML tag
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Tag {
    /// The fully resolved tag URI
    pub uri: String,
    /// The original tag representation (for round-trip)
    pub original: String,
    /// Tag kind for quick identification
    pub kind: TagKind,
}

/// Tag kinds for quick type identification
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[allow(missing_docs)]
pub enum TagKind {
    /// Core YAML types
    Null,
    Bool,
    Int,
    Float,
    Str,
    /// Collection types
    Seq,
    Map,
    /// Extended types
    Binary,
    Timestamp,
    Set,
    Omap,
    Pairs,
    /// Custom application type
    Custom(String),
}

/// Tag resolution context
pub struct TagResolver {
    /// Tag directives (handle -> prefix)
    directives: HashMap<String, String>,
    /// Custom tag handlers
    handlers: HashMap<String, Box<dyn TagHandler>>,
    /// Default schema
    schema: Schema,
}

impl fmt::Debug for TagResolver {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("TagResolver")
            .field("directives", &self.directives)
            .field("handlers_count", &self.handlers.len())
            .field("schema", &self.schema)
            .finish()
    }
}

impl TagResolver {
    /// Create a new tag resolver with default schema
    pub fn new() -> Self {
        Self::with_schema(Schema::Core)
    }

    /// Create a new tag resolver with specific schema
    pub fn with_schema(schema: Schema) -> Self {
        let mut resolver = Self {
            directives: HashMap::new(),
            handlers: HashMap::new(),
            schema,
        };

        // Initialize default tag directives
        resolver.directives.insert("!".to_string(), "!".to_string());
        resolver
            .directives
            .insert("!!".to_string(), "tag:yaml.org,2002:".to_string());

        resolver
    }

    /// Add a tag directive
    pub fn add_directive(&mut self, handle: String, prefix: String) {
        self.directives.insert(handle, prefix);
    }

    /// Clear all tag directives
    pub fn clear_directives(&mut self) {
        self.directives.clear();
        // Re-add defaults
        self.directives.insert("!".to_string(), "!".to_string());
        self.directives
            .insert("!!".to_string(), "tag:yaml.org,2002:".to_string());
    }

    /// Register a custom tag handler
    pub fn register_handler(&mut self, tag_uri: String, handler: Box<dyn TagHandler>) {
        self.handlers.insert(tag_uri, handler);
    }

    /// Resolve a tag string to a full Tag
    pub fn resolve(&self, tag_str: &str) -> Result<Tag> {
        let (uri, original) = if tag_str.starts_with("tag:") {
            // Already a full URI
            (tag_str.to_string(), tag_str.to_string())
        } else if tag_str.starts_with("!<") && tag_str.ends_with('>') {
            // Verbatim tag
            let uri = tag_str[2..tag_str.len() - 1].to_string();
            (uri, tag_str.to_string())
        } else if tag_str.starts_with("!!") {
            // Secondary handle
            let suffix = &tag_str[2..];
            let prefix = self
                .directives
                .get("!!")
                .cloned()
                .unwrap_or_else(|| "tag:yaml.org,2002:".to_string());
            (format!("{}{}", prefix, suffix), tag_str.to_string())
        } else if tag_str.starts_with('!') {
            // Check for named handle
            if let Some(end) = tag_str[1..].find('!') {
                let handle_name = &tag_str[1..end + 1];
                let handle = format!("!{}!", handle_name);
                let suffix = &tag_str[end + 2..];

                if let Some(prefix) = self.directives.get(&handle) {
                    (format!("{}{}", prefix, suffix), tag_str.to_string())
                } else {
                    // Unknown named handle, treat as primary
                    let prefix = self
                        .directives
                        .get("!")
                        .cloned()
                        .unwrap_or_else(|| "!".to_string());
                    (format!("{}{}", prefix, &tag_str[1..]), tag_str.to_string())
                }
            } else {
                // Primary handle
                let suffix = &tag_str[1..];
                let prefix = self
                    .directives
                    .get("!")
                    .cloned()
                    .unwrap_or_else(|| "!".to_string());
                (format!("{}{}", prefix, suffix), tag_str.to_string())
            }
        } else {
            // No tag prefix, use implicit tagging based on schema
            (
                self.schema.default_tag_for(tag_str),
                format!("!{}", tag_str),
            )
        };

        let kind = Self::identify_tag_kind(&uri);

        Ok(Tag {
            uri,
            original,
            kind,
        })
    }

    /// Identify the kind of tag from its URI
    fn identify_tag_kind(uri: &str) -> TagKind {
        match uri {
            "tag:yaml.org,2002:null" => TagKind::Null,
            "tag:yaml.org,2002:bool" => TagKind::Bool,
            "tag:yaml.org,2002:int" => TagKind::Int,
            "tag:yaml.org,2002:float" => TagKind::Float,
            "tag:yaml.org,2002:str" => TagKind::Str,
            "tag:yaml.org,2002:seq" => TagKind::Seq,
            "tag:yaml.org,2002:map" => TagKind::Map,
            "tag:yaml.org,2002:binary" => TagKind::Binary,
            "tag:yaml.org,2002:timestamp" => TagKind::Timestamp,
            "tag:yaml.org,2002:set" => TagKind::Set,
            "tag:yaml.org,2002:omap" => TagKind::Omap,
            "tag:yaml.org,2002:pairs" => TagKind::Pairs,
            _ => TagKind::Custom(uri.to_string()),
        }
    }

    /// Apply a tag to a value
    pub fn apply_tag(&self, tag: &Tag, value: &str) -> Result<Value> {
        // Check for custom handler first
        if let Some(handler) = self.handlers.get(&tag.uri) {
            return handler.construct(value);
        }

        // Use built-in tag handling
        match &tag.kind {
            TagKind::Null => Ok(Value::Null),
            TagKind::Bool => self.construct_bool(value),
            TagKind::Int => self.construct_int(value),
            TagKind::Float => self.construct_float(value),
            TagKind::Str => Ok(Value::String(value.to_string())),
            TagKind::Binary => self.construct_binary(value),
            TagKind::Timestamp => self.construct_timestamp(value),
            _ => Ok(Value::String(value.to_string())), // Default to string
        }
    }

    /// Construct a boolean from a tagged value
    fn construct_bool(&self, value: &str) -> Result<Value> {
        match value.to_lowercase().as_str() {
            "true" | "yes" | "on" => Ok(Value::Bool(true)),
            "false" | "no" | "off" => Ok(Value::Bool(false)),
            _ => Err(Error::Type {
                expected: "boolean".to_string(),
                found: format!("'{}'", value),
                position: crate::Position::start(),
                context: None,
            }),
        }
    }

    /// Construct an integer from a tagged value
    fn construct_int(&self, value: &str) -> Result<Value> {
        // Handle different integer formats
        let parsed = if value.starts_with("0x") || value.starts_with("0X") {
            // Hexadecimal
            i64::from_str_radix(&value[2..], 16)
        } else if value.starts_with("0o") || value.starts_with("0O") {
            // Octal
            i64::from_str_radix(&value[2..], 8)
        } else if value.starts_with("0b") || value.starts_with("0B") {
            // Binary
            i64::from_str_radix(&value[2..], 2)
        } else {
            // Decimal (with underscore support)
            value.replace('_', "").parse::<i64>()
        };

        parsed.map(Value::Int).map_err(|_| Error::Type {
            expected: "integer".to_string(),
            found: format!("'{}'", value),
            position: crate::Position::start(),
            context: None,
        })
    }

    /// Construct a float from a tagged value
    fn construct_float(&self, value: &str) -> Result<Value> {
        match value.to_lowercase().as_str() {
            ".inf" | "+.inf" => Ok(Value::Float(f64::INFINITY)),
            "-.inf" => Ok(Value::Float(f64::NEG_INFINITY)),
            ".nan" => Ok(Value::Float(f64::NAN)),
            _ => value
                .replace('_', "")
                .parse::<f64>()
                .map(Value::Float)
                .map_err(|_| Error::Type {
                    expected: "float".to_string(),
                    found: format!("'{}'", value),
                    position: crate::Position::start(),
                    context: None,
                }),
        }
    }

    /// Construct binary data from a tagged value (base64)
    fn construct_binary(&self, value: &str) -> Result<Value> {
        use base64::{engine::general_purpose::STANDARD, Engine as _};

        // Remove whitespace from base64 string
        let clean = value
            .chars()
            .filter(|c| !c.is_whitespace())
            .collect::<String>();

        match STANDARD.decode(&clean) {
            Ok(bytes) => {
                // Try to convert to UTF-8 string, otherwise store as binary marker
                match String::from_utf8(bytes) {
                    Ok(s) => Ok(Value::String(s)),
                    Err(_) => Ok(Value::String(format!(
                        "[binary data: {} bytes]",
                        clean.len() / 4 * 3
                    ))),
                }
            }
            Err(_) => Err(Error::Type {
                expected: "base64-encoded binary".to_string(),
                found: format!("invalid base64: '{}'", value),
                position: crate::Position::start(),
                context: None,
            }),
        }
    }

    /// Construct a timestamp from a tagged value
    fn construct_timestamp(&self, value: &str) -> Result<Value> {
        // For now, just store as tagged string
        // A full implementation would parse ISO 8601 timestamps
        Ok(Value::String(format!("timestamp:{}", value)))
    }
}

impl Default for TagResolver {
    fn default() -> Self {
        Self::new()
    }
}

/// YAML schemas define tag resolution rules
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Schema {
    /// Core schema (YAML 1.2)
    Core,
    /// JSON schema (subset of YAML)
    Json,
    /// Failsafe schema (minimal)
    Failsafe,
}

impl Schema {
    /// Get the default tag for untagged values based on schema
    pub fn default_tag_for(&self, _value: &str) -> String {
        match self {
            Self::Core => "tag:yaml.org,2002:str".to_string(),
            Self::Json => "tag:yaml.org,2002:str".to_string(),
            Self::Failsafe => "tag:yaml.org,2002:str".to_string(),
        }
    }

    /// Check if implicit typing is allowed
    pub fn allows_implicit_typing(&self) -> bool {
        match self {
            Self::Core => true,
            Self::Json => true,
            Self::Failsafe => false,
        }
    }
}

/// Trait for custom tag handlers
pub trait TagHandler: Send + Sync {
    /// Construct a value from the tagged string
    fn construct(&self, value: &str) -> Result<Value>;

    /// Represent a value as a string for this tag
    fn represent(&self, value: &Value) -> Result<String>;
}

/// Example custom tag handler for a Point type
pub struct PointTagHandler;

impl TagHandler for PointTagHandler {
    fn construct(&self, value: &str) -> Result<Value> {
        // Parse "x,y" format
        let parts: Vec<&str> = value.split(',').collect();
        if parts.len() != 2 {
            return Err(Error::Type {
                expected: "point (x,y)".to_string(),
                found: value.to_string(),
                position: crate::Position::start(),
                context: None,
            });
        }

        let x = parts[0].trim().parse::<f64>().map_err(|_| Error::Type {
            expected: "number".to_string(),
            found: parts[0].to_string(),
            position: crate::Position::start(),
            context: None,
        })?;

        let y = parts[1].trim().parse::<f64>().map_err(|_| Error::Type {
            expected: "number".to_string(),
            found: parts[1].to_string(),
            position: crate::Position::start(),
            context: None,
        })?;

        // Store as a sequence for now
        Ok(Value::Sequence(vec![Value::Float(x), Value::Float(y)]))
    }

    fn represent(&self, value: &Value) -> Result<String> {
        if let Value::Sequence(seq) = value {
            if seq.len() == 2 {
                if let (Some(Value::Float(x)), Some(Value::Float(y))) = (seq.get(0), seq.get(1)) {
                    return Ok(format!("{},{}", x, y));
                }
            }
        }
        Err(Error::Type {
            expected: "point sequence".to_string(),
            found: format!("{:?}", value),
            position: crate::Position::start(),
            context: None,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tag_resolution() {
        let mut resolver = TagResolver::new();

        // Test standard tags
        let tag = resolver.resolve("!!str").unwrap();
        assert_eq!(tag.uri, "tag:yaml.org,2002:str");
        assert_eq!(tag.kind, TagKind::Str);

        let tag = resolver.resolve("!!int").unwrap();
        assert_eq!(tag.uri, "tag:yaml.org,2002:int");
        assert_eq!(tag.kind, TagKind::Int);

        // Test primary handle
        resolver.add_directive("!".to_string(), "tag:example.com,2024:".to_string());
        let tag = resolver.resolve("!custom").unwrap();
        assert_eq!(tag.uri, "tag:example.com,2024:custom");

        // Test named handle
        resolver.add_directive("!e!".to_string(), "tag:example.com,2024:".to_string());
        let tag = resolver.resolve("!e!widget").unwrap();
        assert_eq!(tag.uri, "tag:example.com,2024:widget");

        // Test verbatim tag
        let tag = resolver.resolve("!<tag:explicit.com,2024:type>").unwrap();
        assert_eq!(tag.uri, "tag:explicit.com,2024:type");
    }

    #[test]
    fn test_tag_construction() {
        let resolver = TagResolver::new();

        // Test boolean construction
        let tag = Tag {
            uri: "tag:yaml.org,2002:bool".to_string(),
            original: "!!bool".to_string(),
            kind: TagKind::Bool,
        };

        assert_eq!(resolver.apply_tag(&tag, "true").unwrap(), Value::Bool(true));
        assert_eq!(
            resolver.apply_tag(&tag, "false").unwrap(),
            Value::Bool(false)
        );
        assert_eq!(resolver.apply_tag(&tag, "yes").unwrap(), Value::Bool(true));
        assert_eq!(resolver.apply_tag(&tag, "no").unwrap(), Value::Bool(false));

        // Test integer construction
        let tag = Tag {
            uri: "tag:yaml.org,2002:int".to_string(),
            original: "!!int".to_string(),
            kind: TagKind::Int,
        };

        assert_eq!(resolver.apply_tag(&tag, "42").unwrap(), Value::Int(42));
        assert_eq!(resolver.apply_tag(&tag, "0x2A").unwrap(), Value::Int(42));
        assert_eq!(resolver.apply_tag(&tag, "0o52").unwrap(), Value::Int(42));
        assert_eq!(
            resolver.apply_tag(&tag, "0b101010").unwrap(),
            Value::Int(42)
        );
        assert_eq!(resolver.apply_tag(&tag, "1_234").unwrap(), Value::Int(1234));

        // Test float construction
        let tag = Tag {
            uri: "tag:yaml.org,2002:float".to_string(),
            original: "!!float".to_string(),
            kind: TagKind::Float,
        };

        assert_eq!(
            resolver.apply_tag(&tag, "3.14").unwrap(),
            Value::Float(3.14)
        );
        assert_eq!(
            resolver.apply_tag(&tag, ".inf").unwrap(),
            Value::Float(f64::INFINITY)
        );
        assert_eq!(
            resolver.apply_tag(&tag, "-.inf").unwrap(),
            Value::Float(f64::NEG_INFINITY)
        );
        assert!(matches!(resolver.apply_tag(&tag, ".nan").unwrap(), Value::Float(f) if f.is_nan()));
    }

    #[test]
    fn test_custom_tag_handler() {
        let mut resolver = TagResolver::new();

        // Register custom point handler
        resolver.register_handler(
            "tag:example.com,2024:point".to_string(),
            Box::new(PointTagHandler),
        );

        // Resolve and apply custom tag
        resolver.add_directive("!".to_string(), "tag:example.com,2024:".to_string());
        let tag = resolver.resolve("!point").unwrap();

        let value = resolver.apply_tag(&tag, "3.5, 7.2").unwrap();
        assert_eq!(
            value,
            Value::Sequence(vec![Value::Float(3.5), Value::Float(7.2)])
        );
    }
}