map equality is independent of order

This commit is contained in:
Connor Skees 2020-07-07 01:06:22 -04:00
parent 6138efc059
commit 57562b52d3
10 changed files with 44 additions and 14 deletions

View File

@ -1,3 +1,5 @@
use std::hash::{Hash, Hasher};
use codemap::Span; use codemap::Span;
use crate::{args::FuncArgs, scope::Scope, Token}; use crate::{args::FuncArgs, scope::Scope, Token};
@ -10,6 +12,12 @@ pub(crate) struct Function {
pos: Span, pos: Span,
} }
impl Hash for Function {
fn hash<H: Hasher>(&self, state: &mut H) {
self.pos.hash(state)
}
}
impl PartialEq for Function { impl PartialEq for Function {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.pos == other.pos self.pos == other.pos

View File

@ -1,5 +1,6 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
hash::{Hash, Hasher},
sync::atomic::{AtomicUsize, Ordering}, sync::atomic::{AtomicUsize, Ordering},
}; };
@ -29,6 +30,12 @@ pub(crate) struct Builtin(
usize, usize,
); );
impl Hash for Builtin {
fn hash<H: Hasher>(&self, state: &mut H) {
self.1.hash(state)
}
}
impl Builtin { impl Builtin {
pub fn new(body: fn(CallArgs, &mut Parser<'_>) -> SassResult<Value>) -> Builtin { pub fn new(body: fn(CallArgs, &mut Parser<'_>) -> SassResult<Value>) -> Builtin {
let count = FUNCTION_COUNT.fetch_add(1, Ordering::Relaxed); let count = FUNCTION_COUNT.fetch_add(1, Ordering::Relaxed);

View File

@ -27,7 +27,7 @@ use num_traits::{One, Signed, ToPrimitive, Zero};
mod name; mod name;
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub(crate) struct Color { pub(crate) struct Color {
rgba: Rgba, rgba: Rgba,
hsla: Option<Hsla>, hsla: Option<Hsla>,
@ -65,7 +65,7 @@ impl Color {
} }
} }
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq, Hash)]
struct Rgba { struct Rgba {
red: Number, red: Number,
green: Number, green: Number,
@ -88,7 +88,7 @@ impl Rgba {
} }
} }
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq, Hash)]
struct Hsla { struct Hsla {
hue: Number, hue: Number,
saturation: Number, saturation: Number,

View File

@ -61,7 +61,7 @@ impl Op {
} }
} }
#[derive(Debug, Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub(crate) enum QuoteKind { pub(crate) enum QuoteKind {
Quoted, Quoted,
None, None,
@ -77,13 +77,13 @@ impl Display for QuoteKind {
} }
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum Brackets { pub(crate) enum Brackets {
None, None,
Bracketed, Bracketed,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum ListSeparator { pub(crate) enum ListSeparator {
Space, Space,
Comma, Comma,

View File

@ -7,7 +7,7 @@ pub(crate) use conversion::UNIT_CONVERSION_TABLE;
mod conversion; mod conversion;
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub(crate) enum Unit { pub(crate) enum Unit {
// Absolute units // Absolute units
/// Pixels /// Pixels
@ -122,7 +122,7 @@ pub(crate) enum UnitKind {
None, None,
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub(crate) struct DivUnit { pub(crate) struct DivUnit {
numer: Unit, numer: Unit,
denom: Unit, denom: Unit,

View File

@ -1,4 +1,4 @@
use std::{slice::Iter, vec::IntoIter}; use std::{collections::HashSet, slice::Iter, vec::IntoIter};
use crate::{ use crate::{
common::{Brackets, ListSeparator}, common::{Brackets, ListSeparator},
@ -6,9 +6,19 @@ use crate::{
value::Value, value::Value,
}; };
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, Hash)]
pub(crate) struct SassMap(Vec<(Value, Value)>); pub(crate) struct SassMap(Vec<(Value, Value)>);
impl PartialEq for SassMap {
fn eq(&self, other: &Self) -> bool {
let set_one: HashSet<&Value> = self.0.iter().map(|(v1, _)| v1).collect();
let set_two: HashSet<&Value> = other.0.iter().map(|(v1, _)| v1).collect();
set_one == set_two
}
}
impl Eq for SassMap {}
impl SassMap { impl SassMap {
pub const fn new() -> SassMap { pub const fn new() -> SassMap {
SassMap(Vec::new()) SassMap(Vec::new())

View File

@ -24,7 +24,7 @@ mod number;
mod sass_function; mod sass_function;
// todo: remove Eq and PartialEq impls // todo: remove Eq and PartialEq impls
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) enum Value { pub(crate) enum Value {
Important, Important,
True, True,

View File

@ -16,7 +16,7 @@ mod integer;
const PRECISION: usize = 10; const PRECISION: usize = 10;
#[derive(Clone, Eq, PartialEq, Ord)] #[derive(Clone, Eq, PartialEq, Ord, Hash)]
pub(crate) enum Number { pub(crate) enum Number {
Small(Rational64), Small(Rational64),
Big(Box<BigRational>), Big(Box<BigRational>),

View File

@ -22,7 +22,7 @@ use crate::{
/// ///
/// The function name is stored in addition to the body /// The function name is stored in addition to the body
/// for use in the builtin function `inspect()` /// for use in the builtin function `inspect()`
#[derive(Clone)] #[derive(Clone, Hash)]
pub(crate) enum SassFunction { pub(crate) enum SassFunction {
Builtin(Builtin, Identifier), Builtin(Builtin, Identifier),
UserDefined(Box<Function>, Identifier), UserDefined(Box<Function>, Identifier),

View File

@ -184,6 +184,11 @@ test!(
); );
test!( test!(
map_merge_not_exactly_equal, map_merge_not_exactly_equal,
"a {\n color: inspect(map-merge((0cm: a), (0mm: b)));;\n}\n", "a {\n color: inspect(map-merge((0cm: a), (0mm: b)));\n}\n",
"a {\n color: (0cm: b);\n}\n" "a {\n color: (0cm: b);\n}\n"
); );
test!(
map_equality_is_independent_of_order,
"a {\n color: (c: d, a: b)==(a: b, c: d);\n}\n",
"a {\n color: true;\n}\n"
);