Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add newline separators #777

Merged
merged 1 commit into from
Sep 12, 2024
Merged

Add newline separators #777

merged 1 commit into from
Sep 12, 2024

Conversation

jayvdb
Copy link
Contributor

@jayvdb jayvdb commented Sep 5, 2024

No description provided.

Copy link

codecov bot commented Sep 5, 2024

Codecov Report

Attention: Patch coverage is 0% with 4 lines in your changes missing coverage. Please review.

Project coverage is 67.13%. Comparing base (aaa0a29) to head (b1ee949).
Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
serde_with/src/formats.rs 0.00% 4 Missing ⚠️
Additional details and impacted files
@@            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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jayvdb
Copy link
Contributor Author

jayvdb commented Sep 5, 2024

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"})

@jayvdb
Copy link
Contributor Author

jayvdb commented Sep 11, 2024

ping @jonasbb

@jonasbb
Copy link
Owner

jonasbb commented Sep 12, 2024

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.

@jonasbb
Copy link
Owner

jonasbb commented Sep 12, 2024

This looks nice, thank you. Newline separators seem a quite stable separator.

@jonasbb jonasbb merged commit aede9d5 into jonasbb:master Sep 12, 2024
22 checks passed
@jayvdb jayvdb deleted the newline-separator branch September 25, 2024 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants