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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// (C) Copyright 2019 Hewlett Packard Enterprise Development LP

use crate::parser::{Pair, Rule};
use crate::util::*;
use crate::error::*;

use enquote::unquote;
use snafu::ResultExt;

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Label {
  pub name: String,
  pub value: String
}

impl Label {
  pub fn new<S>(name: S, value: S) -> Label
  where
    S: Into<String>
  {
    Label {
      name: name.into(), value: value.into()
    }
  }

  pub(crate) fn from_record(record: Pair) -> Result<Label> {
    let mut name = None;
    let mut value = None;

    for field in record.into_inner() {
      match field.as_rule() {
        Rule::label_name => name = Some(field.as_str().to_string()),
        Rule::label_quoted_name => {
          // label seems to be uniquely able to span multiple lines when quoted
          let v = unquote(&clean_escaped_breaks(field.as_str()))
            .context(UnescapeError)?;

          name = Some(v);
        },

        Rule::label_value => value = Some(field.as_str().to_string()),
        Rule::label_quoted_value => {
          let v = unquote(&clean_escaped_breaks(field.as_str()))
            .context(UnescapeError)?;

          value = Some(v);
        },
        _ => return Err(unexpected_token(field))
      }
    }

    let name = name.ok_or_else(|| Error::GenericParseError {
      message: "label name is required".into()
    })?.to_string();

    let value = value.ok_or_else(|| Error::GenericParseError {
      message: "label value is required".into()
    })?.to_string();

    Ok(Label::new(name, value))
  }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct LabelInstruction(pub Vec<Label>);

impl LabelInstruction {
  pub(crate) fn from_record(record: Pair) -> Result<LabelInstruction> {
    let mut labels = Vec::new();

    for field in record.into_inner() {
      match field.as_rule() {
        Rule::label_pair => labels.push(Label::from_record(field)?),
        _ => return Err(unexpected_token(field))
      }
    }

    Ok(LabelInstruction(labels))
  }
}

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

  #[test]
  fn label_basic() -> Result<()> {
    assert_eq!(
      parse_single("label foo=bar", Rule::label)?,
      LabelInstruction(vec![
        Label::new("foo", "bar")
      ]).into()
    );

    assert_eq!(
      parse_single("label foo.bar=baz", Rule::label)?,
      LabelInstruction(vec![
        Label::new("foo.bar", "baz")
      ]).into()
    );

    assert_eq!(
      parse_single(r#"label "foo.bar"="baz qux""#, Rule::label)?,
      LabelInstruction(vec![
        Label::new("foo.bar", "baz qux")
      ]).into()
    );

    Ok(())
  }

  #[test]
  fn label_multi() -> Result<()> {
    assert_eq!(
      parse_single(r#"label foo=bar baz="qux" "quux quuz"="corge grault""#, Rule::label)?,
      LabelInstruction(vec![
        Label::new("foo", "bar"),
        Label::new("baz", "qux"),
        Label::new("quux quuz", "corge grault")
      ]).into()
    );

    assert_eq!(
      parse_single(
        r#"label foo=bar \
          baz="qux" \
          "quux quuz"="corge grault""#,
        Rule::label
      )?,
      LabelInstruction(vec![
        Label::new("foo", "bar"),
        Label::new("baz", "qux"),
        Label::new("quux quuz", "corge grault")
      ]).into()
    );

    Ok(())
  }

  #[test]
  fn label_multiline() -> Result<()> {
    assert_eq!(
      parse_single(r#"label "foo.bar"="baz\n qux""#, Rule::label)?,
      LabelInstruction(vec![
        Label::new("foo.bar", "baz\n qux")
      ]).into()
    );

    assert_eq!(
      parse_single(r#"label "foo\nbar"="baz\n qux""#, Rule::label)?,
      LabelInstruction(vec![
        Label::new("foo\nbar", "baz\n qux")
      ]).into()
    );

    Ok(())
  }

  #[test]
  fn label_multi_multiline() -> Result<()> {
    assert_eq!(
      parse_single(
        r#"label foo=bar \
          "lorem ipsum
          dolor
          "="sit
          amet" \
          baz=qux"#,
        Rule::label
      )?,
      LabelInstruction(vec![
        Label::new("foo", "bar"),
        Label::new("lorem ipsum\n          dolor\n          ", "sit\n          amet"),
        Label::new("baz", "qux")
      ]).into()
    );

    Ok(())
  }
}