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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright © 2018 Corporation for Digital Scholarship

use super::variables::*;
use roxmltree::Node;
use std::num::ParseIntError;
use std::ops::Range;

#[derive(Debug, PartialEq)]
pub struct UnknownAttributeValue {
    pub value: String,
}

impl UnknownAttributeValue {
    pub fn new(s: &str) -> Self {
        UnknownAttributeValue {
            value: s.to_owned(),
        }
    }
}

use serde::Serializer;

fn rox_error_serialize<S>(x: &roxmltree::Error, s: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    s.serialize_str(&ToString::to_string(x))
}

#[derive(Debug, Serialize)]
pub enum StyleError {
    Invalid(CslError),
    ParseError(#[serde(serialize_with = "rox_error_serialize")] roxmltree::Error),
}

#[derive(Debug, Serialize)]
pub struct CslError(pub Vec<InvalidCsl>);

#[derive(Debug, PartialEq, Copy, Clone, Serialize)]
pub enum Severity {
    Error,
    Warning,
}

#[derive(Debug, PartialEq, Clone, Serialize)]
pub struct InvalidCsl {
    pub severity: Severity,
    // TODO: serialize_with or otherwise get this into the output
    pub range: Range<usize>,
    pub message: String,
    pub hint: String,
}

#[derive(Debug, AsRefStr)]
pub enum NeedVarType {
    Any,
    TextVariable,
    NumberVariable,
    Date,
    // in condition matchers
    CondDate,
    CondType,
    CondPosition,
    CondLocator,
    CondIsPlural,
    // in <name variable="">
    Name,
}

// TODO: create a trait for producing hints
impl NeedVarType {
    pub fn hint(
        &self,
        attr: &str,
        var: &str,
        maybe_got: Option<AnyVariable>,
    ) -> (String, String, Severity) {
        use self::NeedVarType::*;
        let wrong_type_var = format!("Wrong variable type for `{}`: \"{}\"", attr, var);
        let empty = "".to_string();
        let unknown = (
            format!("Unknown variable \"{}\"", var),
            empty.clone(),
            Severity::Error,
        );
        match *self {

            Any => unknown,

            TextVariable => maybe_got.map(|got| {
                let wrong_type = "Wrong variable type for <text>".to_string();
                use crate::variables::AnyVariable::*;
                match got {
                    Name(_) => (wrong_type, "Hint: use <names> instead".to_string(), Severity::Error),
                    Date(_) => (wrong_type, "Hint: use <date> instead".to_string(), Severity::Error),
                    // this would be trying to print an error when the input was correct
                    _ => (empty, "???".to_string(), Severity::Warning),
                }
            }).unwrap_or(unknown),

            NumberVariable => maybe_got.map(|got| {
                let wrong_type = "Wrong variable type for <number>".to_string();
                use crate::variables::AnyVariable::*;
                match got {
                    Ordinary(_) => (wrong_type,
                                    format!("Hint: use <text variable=\"{}\" /> instead", var),
                                    Severity::Error),
                    Name(_) => (wrong_type, "Hint: use <names> instead".to_string(), Severity::Error),
                    Date(_) => (wrong_type, "Hint: use <date> instead".to_string(), Severity::Error),
                    // this would be trying to print an error when the input was correct
                    _ => (empty, "???".to_string(), Severity::Warning),
                }
            }).unwrap_or(unknown),

            CondDate => (wrong_type_var,
                                    format!("Hint: `{}` can only match date variables", attr),
                                    Severity::Error),

            CondType => (wrong_type_var,
                         "Hint: `type` can only match known types".to_string(),
                         Severity::Error),

            CondPosition => (wrong_type_var,
                             "Hint: `position` matches {{ first | ibid | ibid-with-locator | subsequent | near-note | far-note }}*".to_string(),
                             Severity::Error),

            CondLocator => (wrong_type_var, "Hint: `locator` only matches locator types".to_string(), Severity::Error),
            CondIsPlural => (wrong_type_var, "Hint: `is-plural` only matches name variables".to_string(), Severity::Error),
            Date => (wrong_type_var, "<date variable=\"...\"> can only render dates".to_string(), Severity::Error),
            Name => (wrong_type_var, "Hint: <names> can only render name variables".to_string(), Severity::Error),
        }
    }
}

impl InvalidCsl {
    pub fn new(node: &Node, message: &str) -> Self {
        let range = node.range();
        InvalidCsl {
            range,
            severity: Severity::Error,
            hint: "".to_string(),
            message: message.to_owned(),
        }
    }

    pub fn bad_int(node: &Node, attr: &str, uav: &ParseIntError) -> Self {
        let at = node.attribute_node(attr).unwrap();
        let range = at.range();
        InvalidCsl {
            range,
            message: format!("Invalid integer value for {}: {:?}", attr, uav),
            hint: "".to_string(),
            severity: Severity::Error,
        }
    }

    pub fn missing(node: &Node, attr: &str) -> Self {
        InvalidCsl::new(node, &format!("Must have `{}` attribute", attr))
    }

    pub fn attr_val(node: &Node, attr: &str, uav: &str) -> Self {
        let at = node.attribute_node(attr).unwrap();
        let range = at.range();
        InvalidCsl {
            range,
            message: format!("Unknown attribute value for `{}`: \"{}\"", attr, uav),
            hint: "".to_string(),
            severity: Severity::Error,
        }
    }

    pub fn wrong_var_type(
        node: &Node,
        attr: &str,
        uav: &str,
        needed: NeedVarType,
        got: Option<AnyVariable>,
    ) -> Self {
        let at = node.attribute_node(attr).unwrap();
        let range = at.range();
        let (message, hint, severity) = needed.hint(attr, uav, got);
        InvalidCsl {
            range,
            message,
            hint,
            severity,
        }
    }
}

impl From<roxmltree::Error> for StyleError {
    fn from(err: roxmltree::Error) -> StyleError {
        StyleError::ParseError(err)
    }
}

impl From<CslError> for StyleError {
    fn from(err: CslError) -> StyleError {
        StyleError::Invalid(err)
    }
}

impl From<Vec<CslError>> for CslError {
    fn from(errs: Vec<CslError>) -> CslError {
        // concat all of the sub-vecs into one
        let mut collect = Vec::with_capacity(errs.len());
        for err in errs {
            collect.extend_from_slice(&err.0);
        }
        CslError(collect)
    }
}

impl From<InvalidCsl> for CslError {
    fn from(err: InvalidCsl) -> CslError {
        CslError(vec![err])
    }
}

impl From<InvalidCsl> for StyleError {
    fn from(err: InvalidCsl) -> StyleError {
        StyleError::Invalid(CslError(vec![err]))
    }
}

impl Default for StyleError {
    fn default() -> Self {
        StyleError::Invalid(CslError(vec![InvalidCsl {
            severity: Severity::Error,
            range: 1usize..2usize,
            hint: "".to_string(),
            message: "".to_string(),
        }]))
    }
}

pub(crate) trait PartitionResults<O, E>: Iterator<Item = Result<O, E>>
where
    O: Sized,
    Self: Sized,
{
    fn partition_results<'a>(self) -> Result<Vec<O>, Vec<E>> {
        let mut errors = Vec::new();
        let oks = self
            .filter_map(|res| match res {
                Ok(ok) => Some(ok),
                Err(e) => {
                    errors.push(e);
                    None
                }
            })
            .collect();
        if errors.len() > 0 {
            Err(errors)
        } else {
            Ok(oks)
        }
    }
}

impl<O, E, I: Iterator<Item = Result<O, E>>> PartitionResults<O, E> for I {}