Closed jayvdb closed 2 months ago
This is sufficient for my needs as I only want Unix style multi-line strings parsed, but it would be nice if a cross-platform "new line" separator could be created. I suspect this would need a new interface.
Unfortunately this doesnt work:
#[serde_as]
#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[serde(untagged)]
enum NewlineSeparatedStringSet {
Unix(#[serde_as(as = "StringWithSeparator::<UnixLineSeparator, String>")] BTreeSet<String>),
Dos(#[serde_as(as = "StringWithSeparator::<DosLineSeparator, String>")] BTreeSet<String>),
}
It builds, but then tests fail because serde can always construct the wrong member of the enum, so it does, like
left: Dos({"bar", "foo"})
right: Unix({"bar", "bar\r", "foo\r"})
ping @jonasbb
Unfortunately this doesnt work:
#[serde_as] #[derive(Debug, Deserialize, PartialEq, Serialize)] #[serde(untagged)] enum NewlineSeparatedStringSet { Unix(#[serde_as(as = "StringWithSeparator::<UnixLineSeparator, String>")] BTreeSet<String>), Dos(#[serde_as(as = "StringWithSeparator::<DosLineSeparator, String>")] BTreeSet<String>), }
serde always processes untagged enums top down. Since the unix line ending is part of the dos line ending, this leads to the wrong matches that you reported. Switching the order, i.e., having dos first, should work. Assuming the individual strings do not contain any \r
or \n
.
This is sufficient for my needs as I only want Unix style multi-line strings parsed, but it would be nice if a cross-platform "new line" separator could be created. I suspect this would need a new interface.
Do you mean something that is always the native newline separator of the current platform? That should be doable. If you want something that can match any separator while splitting strings (i.e., automatically figuring out which separator is used) then this will not work with the current interface.
This looks nice, thank you. Newline separators seem a quite stable separator.
Codecov Report
Attention: Patch coverage is
0%
with4 lines
in your changes missing coverage. Please review.Additional details and impacted files
```diff @@ Coverage Diff @@ ## master #777 +/- ## ========================================== - Coverage 67.32% 67.13% -0.20% ========================================== Files 40 40 Lines 2464 2468 +4 ========================================== - Hits 1659 1657 -2 - Misses 805 811 +6 ```:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.