Skip to main content

json_syntax/
lexical.rs

1use core::hash::{Hash, Hasher};
2
3use crate::JsonValue;
4
5/// Wrapper around a JSON value to consider its lexical representation without
6/// regard for semantical equivalences.
7#[derive(Debug)]
8#[repr(transparent)]
9pub struct JsonLexical<T: ?Sized = JsonValue>(pub T);
10
11pub trait JsonBorrowLexical {
12	fn as_lexical(&self) -> &JsonLexical<Self> {
13		unsafe { core::mem::transmute(self) }
14	}
15}
16
17pub trait JsonLexicalPartialEq {
18	fn lexical_eq(&self, other: &Self) -> bool;
19}
20
21impl<T: JsonLexicalPartialEq> JsonLexicalPartialEq for Vec<T> {
22	fn lexical_eq(&self, other: &Self) -> bool {
23		self.len() == other.len() && self.iter().zip(other).all(|(a, b)| a.lexical_eq(b))
24	}
25}
26
27impl<T: JsonLexicalPartialEq> PartialEq for JsonLexical<T> {
28	fn eq(&self, other: &Self) -> bool {
29		self.0.lexical_eq(&other.0)
30	}
31}
32
33pub trait JsonLexicalEq: JsonLexicalPartialEq {}
34
35impl<T: JsonLexicalEq> Eq for JsonLexical<T> {}
36
37pub trait JsonLexicalHash {
38	fn lexical_hash<H: Hasher>(&self, state: &mut H);
39}
40
41impl<T: JsonLexicalHash> Hash for JsonLexical<T> {
42	fn hash<H: Hasher>(&self, state: &mut H) {
43		self.0.lexical_hash(state)
44	}
45}