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
//! Provides proguard support.

#![warn(missing_docs)]

use std::io;

use proguard::MappingView;

use symbolic_common::{AsSelf, ByteView, SelfCell, Uuid};

struct Inner<'a>(MappingView<'a>);

impl<'slf, 'a: 'slf> AsSelf<'slf> for Inner<'a> {
    type Ref = MappingView<'slf>;

    fn as_self(&'slf self) -> &Self::Ref {
        &self.0
    }
}

/// A view over a proguard mapping text file.
pub struct ProguardMappingView<'a> {
    inner: SelfCell<ByteView<'a>, Inner<'a>>,
}

impl<'a> ProguardMappingView<'a> {
    /// Creates a new proguard mapping view from a byte slice.
    pub fn parse(byteview: ByteView<'a>) -> Result<Self, io::Error> {
        // NB: Since ByteView does not expose its inner data structure, we need to use a `SelfCell`
        // to construct a `proguard::MappingView`. Ideally, we would pass the ByteView's backing to
        // the MappingView constructor directly, instead.
        let inner = SelfCell::try_new(byteview, |data| {
            MappingView::from_slice(unsafe { &*data }).map(Inner)
        })?;

        Ok(ProguardMappingView { inner })
    }

    /// Returns the mapping UUID.
    pub fn uuid(&self) -> Uuid {
        self.inner.get().uuid()
    }

    /// Returns true if this file has line infos.
    pub fn has_line_info(&self) -> bool {
        self.inner.get().has_line_info()
    }

    /// Converts a dotted path.
    pub fn convert_dotted_path(&self, path: &str, lineno: u32) -> String {
        let mut iter = path.splitn(2, ':');
        let cls_name = iter.next().unwrap_or("");
        let meth_name = iter.next();
        if let Some(cls) = self.inner.get().find_class(cls_name) {
            let class_name = cls.class_name();
            if let Some(meth_name) = meth_name {
                let lineno = if lineno == 0 {
                    None
                } else {
                    Some(lineno as u32)
                };

                let methods = cls.get_methods(meth_name, lineno);
                if !methods.is_empty() {
                    format!("{}:{}", class_name, methods[0].name())
                } else {
                    format!("{}:{}", class_name, meth_name)
                }
            } else {
                class_name.to_string()
            }
        } else {
            path.to_string()
        }
    }
}