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
use crate::context::{Position, FullContext};

/// A Context that holds only an optional file name and 1-indexed start position
///
/// Intended to be constructed only from a [`FullContext`] as a way of
/// discarding additional, unwanted information.
///
/// [`FullContext`]: struct.FullContext.html
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PartialContext {
    file_name: Option<String>,
    start_position: Position,
}

impl PartialContext {
    /// Returns a reference to the optional file name
    pub fn get_file_name(&self) -> &Option<String> {
        &self.file_name
    }

    /// Returns a reference to the 1-indexed start position
    pub fn get_start_position(&self) -> &Position {
        &self.start_position
    }
}

impl std::convert::From<FullContext> for PartialContext {
    fn from(full: FullContext) -> PartialContext {
        PartialContext {
            file_name: full.get_file_name().clone(),
            start_position: *full.get_start_position(),
        }
    }
}

impl std::fmt::Display for PartialContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}: {}", self.file_name, self.start_position)
    }
}

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

    #[test]
    fn test_conversion() {
        let contents = "hail eris".to_string();
        let c = FullContext::from(None, contents);
        let partial: PartialContext = c.into();
        assert_eq!(*partial.get_file_name(), None);
        assert_eq!(*partial.get_start_position(), Position::abs(1, 1));
    }

    #[test]
    fn from_subcontext() {
        let name = "name.ext".to_string();
        let contents = "hail eris".to_string();
        let c = FullContext::from(Some(name), contents);
        let sub = c.subcontext(Position::rel(1, 6)..=Position::rel(2, 3));
        let partial: PartialContext = sub.into();
        assert_eq!(*partial.get_file_name(), Some("name.ext".to_string()));
        assert_eq!(*partial.get_start_position(), Position::abs(1, 6));
    }
}