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
/* 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 https://mozilla.org/MPL/2.0/. */

use std::borrow::ToOwned;
use std::collections::HashSet;
use std::{fmt, string};

use crate::dom::bindings::codegen::Bindings::NodeBinding::Node_Binding::NodeMethods;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::utils::AsVoidPtr;
use crate::dom::node::Node;

/// The primary types of values that an XPath expression returns as a result.
pub enum Value {
    Boolean(bool),
    /// A IEEE-754 double-precision floating point number
    Number(f64),
    String(String),
    /// A collection of not-necessarily-unique nodes
    Nodeset(Vec<DomRoot<Node>>),
}

impl fmt::Debug for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Value::Boolean(val) => write!(f, "{}", val),
            Value::Number(val) => write!(f, "{}", val),
            Value::String(ref val) => write!(f, "{}", val),
            Value::Nodeset(ref val) => write!(f, "Nodeset({:?})", val),
        }
    }
}

pub fn str_to_num(s: &str) -> f64 {
    s.trim().parse().unwrap_or(f64::NAN)
}

/// Helper for PartialEq<Value> implementations
fn str_vals(nodes: &[DomRoot<Node>]) -> HashSet<String> {
    nodes
        .iter()
        .map(|n| n.GetTextContent().unwrap_or_default().to_string())
        .collect()
}

/// Helper for PartialEq<Value> implementations
fn num_vals(nodes: &[DomRoot<Node>]) -> Vec<f64> {
    nodes
        .iter()
        .map(|n| Value::String(n.GetTextContent().unwrap_or_default().into()).number())
        .collect()
}

impl PartialEq<Value> for Value {
    fn eq(&self, other: &Value) -> bool {
        match (self, other) {
            (Value::Nodeset(left_nodes), Value::Nodeset(right_nodes)) => {
                let left_strings = str_vals(left_nodes);
                let right_strings = str_vals(right_nodes);
                !left_strings.is_disjoint(&right_strings)
            },
            (&Value::Nodeset(ref nodes), &Value::Number(val)) |
            (&Value::Number(val), &Value::Nodeset(ref nodes)) => {
                let numbers = num_vals(nodes);
                numbers.iter().any(|n| *n == val)
            },
            (&Value::Nodeset(ref nodes), &Value::String(ref val)) |
            (&Value::String(ref val), &Value::Nodeset(ref nodes)) => {
                let strings = str_vals(nodes);
                strings.contains(val)
            },
            (&Value::Boolean(_), _) | (_, &Value::Boolean(_)) => self.boolean() == other.boolean(),
            (&Value::Number(_), _) | (_, &Value::Number(_)) => self.number() == other.number(),
            _ => self.string() == other.string(),
        }
    }
}

impl Value {
    pub fn boolean(&self) -> bool {
        match *self {
            Value::Boolean(val) => val,
            Value::Number(n) => n != 0.0 && !n.is_nan(),
            Value::String(ref s) => !s.is_empty(),
            Value::Nodeset(ref nodeset) => !nodeset.is_empty(),
        }
    }

    pub fn into_boolean(self) -> bool {
        self.boolean()
    }

    pub fn number(&self) -> f64 {
        match *self {
            Value::Boolean(val) => {
                if val {
                    1.0
                } else {
                    0.0
                }
            },
            Value::Number(val) => val,
            Value::String(ref s) => str_to_num(s),
            Value::Nodeset(..) => str_to_num(&self.string()),
        }
    }

    pub fn into_number(self) -> f64 {
        self.number()
    }

    pub fn string(&self) -> string::String {
        match *self {
            Value::Boolean(v) => v.to_string(),
            Value::Number(n) => {
                if n.is_infinite() {
                    if n.signum() < 0.0 {
                        "-Infinity".to_owned()
                    } else {
                        "Infinity".to_owned()
                    }
                } else if n == 0.0 {
                    // catches -0.0 also
                    0.0.to_string()
                } else {
                    n.to_string()
                }
            },
            Value::String(ref val) => val.clone(),
            Value::Nodeset(ref nodes) => match nodes.document_order_first() {
                Some(n) => n.GetTextContent().unwrap_or_default().to_string(),
                None => "".to_owned(),
            },
        }
    }

    pub fn into_string(self) -> string::String {
        match self {
            Value::String(val) => val,
            other => other.string(),
        }
    }
}

macro_rules! from_impl {
    ($raw:ty, $variant:expr) => {
        impl From<$raw> for Value {
            fn from(other: $raw) -> Value {
                $variant(other)
            }
        }
    };
}

from_impl!(bool, Value::Boolean);
from_impl!(f64, Value::Number);
from_impl!(String, Value::String);
impl<'a> From<&'a str> for Value {
    fn from(other: &'a str) -> Value {
        Value::String(other.into())
    }
}
from_impl!(Vec<DomRoot<Node>>, Value::Nodeset);

macro_rules! partial_eq_impl {
    ($raw:ty, $variant:pat => $b:expr) => {
        impl PartialEq<$raw> for Value {
            fn eq(&self, other: &$raw) -> bool {
                match *self {
                    $variant => $b == other,
                    _ => false,
                }
            }
        }

        impl PartialEq<Value> for $raw {
            fn eq(&self, other: &Value) -> bool {
                match *other {
                    $variant => $b == self,
                    _ => false,
                }
            }
        }
    };
}

partial_eq_impl!(bool, Value::Boolean(ref v) => v);
partial_eq_impl!(f64, Value::Number(ref v) => v);
partial_eq_impl!(String, Value::String(ref v) => v);
partial_eq_impl!(&str, Value::String(ref v) => v);
partial_eq_impl!(Vec<DomRoot<Node>>, Value::Nodeset(ref v) => v);

pub trait NodesetHelpers {
    /// Returns the node that occurs first in [document order]
    ///
    /// [document order]: https://www.w3.org/TR/xpath/#dt-document-order
    fn document_order_first(&self) -> Option<DomRoot<Node>>;
    fn document_order(&self) -> Vec<DomRoot<Node>>;
    fn document_order_unique(&self) -> Vec<DomRoot<Node>>;
}

impl NodesetHelpers for Vec<DomRoot<Node>> {
    fn document_order_first(&self) -> Option<DomRoot<Node>> {
        self.iter()
            .min_by(|a, b| {
                if a == b {
                    std::cmp::Ordering::Equal
                } else if a.is_before(b) {
                    std::cmp::Ordering::Less
                } else {
                    std::cmp::Ordering::Greater
                }
            })
            .cloned()
    }
    fn document_order(&self) -> Vec<DomRoot<Node>> {
        let mut nodes: Vec<DomRoot<Node>> = self.clone();
        if nodes.len() == 1 {
            return nodes;
        }

        nodes.sort_by(|a, b| {
            if a == b {
                std::cmp::Ordering::Equal
            } else if a.is_before(b) {
                std::cmp::Ordering::Less
            } else {
                std::cmp::Ordering::Greater
            }
        });

        nodes
    }
    fn document_order_unique(&self) -> Vec<DomRoot<Node>> {
        let mut nodes: Vec<DomRoot<Node>> = self.document_order();

        nodes.dedup_by_key(|n| n.as_void_ptr());

        nodes
    }
}