wasmgdb_ddbug_parser/
source.rs

1use crate::unit::Unit;
2
3/// A source location.
4#[derive(Debug, Default, Clone)]
5pub struct Source<'input> {
6    pub(crate) directory: Option<&'input str>,
7    pub(crate) file: Option<&'input str>,
8    pub(crate) line: u32,
9    pub(crate) column: u32,
10}
11
12impl<'input> Source<'input> {
13    /// The directory.
14    ///
15    /// This may be absolute, or relative to the working directory of the unit.
16    #[inline]
17    pub fn directory(&self) -> Option<&str> {
18        self.directory
19    }
20
21    /// The file name.
22    #[inline]
23    pub fn file(&self) -> Option<&str> {
24        self.file
25    }
26
27    /// Return true if there is no file name.
28    #[inline]
29    pub fn is_none(&self) -> bool {
30        self.file.is_none()
31    }
32
33    /// Return true if there is a file name.
34    #[inline]
35    pub fn is_some(&self) -> bool {
36        self.file.is_some()
37    }
38
39    /// The complete file path.
40    pub fn path(&self, unit: &Unit) -> Option<String> {
41        fn is_absolute(directory: &str) -> bool {
42            directory.get(0..1) == Some("/") || directory.get(1..2) == Some(":")
43        }
44
45        self.file().map(|file| {
46            let mut path = String::new();
47            if let Some(directory) = self.directory() {
48                if let (false, Some(unit_dir)) = (is_absolute(directory), unit.dir()) {
49                    path.push_str(unit_dir);
50                    if !unit_dir.ends_with('/') {
51                        path.push('/');
52                    }
53                }
54                path.push_str(directory);
55                if !directory.ends_with('/') {
56                    path.push('/');
57                }
58            }
59            path.push_str(file);
60            path
61        })
62    }
63
64    /// The source line number.
65    ///
66    /// 0 means unknown line number.
67    #[inline]
68    pub fn line(&self) -> u32 {
69        self.line
70    }
71
72    /// The source column number.
73    ///
74    /// 0 means unknown column number.
75    #[inline]
76    pub fn column(&self) -> u32 {
77        self.column
78    }
79}