Skip to content

Commit

Permalink
Duplicates in sets is now an Error.
Browse files Browse the repository at this point in the history
Clojure's behavior:
(clojure.edn/read-string "#{1 2 2 3}")
java.lang.IllegalArgumentException: Duplicate key: 2 [at <repl>:1:1]
  • Loading branch information
Grinkers committed Feb 26, 2024
1 parent 73324d4 commit 19196dd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/deserialize/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,14 @@ fn parse_set(walker: &mut Walker<'_>) -> Result<Edn, Error> {
}
Some(_) => {
let next = parse_internal(walker)?;
if next != Edn::Empty {
set.insert(next);
}
if next != Edn::Empty && !set.insert(next) {
return Err(Error {
code: Code::SetDuplicateKey,
line: Some(walker.line),
column: Some(walker.column),
ptr: Some(walker.ptr),
});
};
}
_ => {
return Err(Error {
Expand Down
1 change: 1 addition & 0 deletions src/edn/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub enum Code {

/// Parse errors
HashMapDuplicateKey,
SetDuplicateKey,
InvalidChar,
InvalidEscape,
InvalidKeyword,
Expand Down
17 changes: 17 additions & 0 deletions tests/error_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,21 @@ world!"#
let small = edn_rs::from_edn::<u8>(&Edn::UInt(9876123));
assert_eq!(format!("{small:?}"), "Err(EdnError { code: TryFromInt(TryFromIntError(())), line: None, column: None, ptr: None })");
}

#[test]
#[cfg(feature = "sets")]
fn duplicate_in_set() {
assert_eq!(
err_as_string("#{1 42 42 3}"),
"EdnError { code: SetDuplicateKey, line: Some(1), column: Some(10), ptr: Some(9) }"
);
}

#[test]
fn duplicate_in_map() {
assert_eq!(
err_as_string("{:foo 42 :bar 43 :foo \"cat\"}"),
"EdnError { code: HashMapDuplicateKey, line: Some(1), column: Some(28), ptr: Some(27) }"
);
}
}

0 comments on commit 19196dd

Please sign in to comment.