source_map_node/
lib.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5use std::fmt;
6use std::fmt::{Debug, Formatter};
7
8#[derive(Clone, Eq, PartialEq, Default, Hash)]
9pub struct Node {
10    pub span: Span,
11}
12
13impl Debug for Node {
14    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
15        if self.span.file_id == 0xffff {
16            write!(f, "<{}:{}>", self.span.offset, self.span.length)
17        } else {
18            write!(
19                f,
20                "<{}:{} ({})>",
21                self.span.offset, self.span.length, self.span.file_id
22            )
23        }
24    }
25}
26
27pub type FileId = u16;
28
29#[derive(PartialEq, Eq, Hash, Default, Clone)]
30pub struct Span {
31    pub file_id: FileId,
32    pub offset: u32,
33    pub length: u16,
34}
35
36impl Span {
37    pub fn dummy() -> Self {
38        Span {
39            offset: 0,
40            length: 0,
41            file_id: 0xffff,
42        }
43    }
44
45    // Helper method to get the end position
46    pub fn end(&self) -> u32 {
47        self.offset + self.length as u32
48    }
49}
50
51impl Debug for Span {
52    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
53        write!(f, "<{}:{} ({})>", self.offset, self.length, self.file_id)
54    }
55}
56
57impl Node {
58    pub fn new_unknown() -> Self {
59        Self {
60            span: Span {
61                file_id: 0xffff,
62                offset: 0,
63                length: 0,
64            },
65        }
66    }
67}