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
//! Provide a collection of attributes and a convenient way for querying them.
//!
//! These structures are all map-like.

use std::borrow::Borrow;
use std::collections::HashMap;
use std::hash::Hash;
use std::mem;
use std::vec;
use {Loc, RpValue, Span};

/// Iterator over unused positions.
pub struct Unused {
    iter: vec::IntoIter<Span>,
}

impl Iterator for Unused {
    type Item = Span;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct Selection {
    /// Storing words and their locations.
    words: Vec<Loc<RpValue>>,
    /// Storing values and their locations.
    values: HashMap<String, (Loc<String>, Loc<RpValue>)>,
}

impl Selection {
    pub fn new(
        words: Vec<Loc<RpValue>>,
        values: HashMap<String, (Loc<String>, Loc<RpValue>)>,
    ) -> Selection {
        Selection {
            words: words,
            values: values,
        }
    }

    /// Take the given value, removing it in the process.
    pub fn take<Q: ?Sized>(&mut self, key: &Q) -> Option<Loc<RpValue>>
    where
        String: Borrow<Q>,
        Q: Hash + Eq,
    {
        self.values.remove(key).map(|v| v.1)
    }

    /// Take the given value, removing it in the process.
    pub fn take_words(&mut self) -> Vec<Loc<RpValue>> {
        mem::replace(&mut self.words, vec![])
    }

    /// Take a single word.
    pub fn take_word(&mut self) -> Option<Loc<RpValue>> {
        self.words.pop()
    }

    /// Get an iterator over unused positions.
    pub fn unused(&self) -> Unused {
        let mut positions = Vec::new();
        positions.extend(self.words.iter().map(|v| Loc::span(v)));
        positions.extend(self.values.values().map(|v| Loc::span(&v.0)));
        Unused {
            iter: positions.into_iter(),
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct Attributes {
    words: HashMap<String, Span>,
    selections: HashMap<String, Loc<Selection>>,
}

impl Attributes {
    pub fn new(
        words: HashMap<String, Span>,
        selections: HashMap<String, Loc<Selection>>,
    ) -> Attributes {
        Attributes {
            words: words,
            selections: selections,
        }
    }

    /// Take the given selection, removing it in the process.
    pub fn take_word<Q: ?Sized>(&mut self, key: &Q) -> bool
    where
        String: Borrow<Q>,
        Q: Hash + Eq,
    {
        self.words.remove(key).is_some()
    }

    /// Take the given selection, removing it in the process.
    pub fn take_selection<Q: ?Sized>(&mut self, key: &Q) -> Option<Loc<Selection>>
    where
        String: Borrow<Q>,
        Q: Hash + Eq,
    {
        self.selections.remove(key)
    }

    /// Get an iterator over unused positions.
    pub fn unused(&self) -> Unused {
        let mut positions = Vec::new();
        positions.extend(self.words.values());
        positions.extend(self.selections.values().map(Loc::span));
        Unused {
            iter: positions.into_iter(),
        }
    }
}