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

Serde impls for Comparator #282

Merged
merged 1 commit into from Jun 29, 2022
Merged

Serde impls for Comparator #282

merged 1 commit into from Jun 29, 2022

Conversation

dtolnay
Copy link
Owner

@dtolnay dtolnay commented Jun 29, 2022

No description provided.

@dtolnay
Copy link
Owner Author

dtolnay commented Jun 29, 2022

I need this for serializing a single Version as an = version req.

use semver::{Comparator, Version};
use serde::{Serialize, Serializer};

#[derive(Serialize)]
struct DependencySpec {
    package: String,
    #[serde(serialize_with = "exact_version")]
    version: Version,
}

fn exact_version<S>(version: &Version, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    Serialize::serialize(&Comparator {
        op: semver::Op::Exact,
        major: version.major,
        minor: Some(version.minor),
        patch: Some(version.patch),
        pre: version.pre.clone(),
    }, serializer)
}

@dtolnay dtolnay merged commit 90e295a into master Jun 29, 2022
@dtolnay dtolnay deleted the sercomparator branch June 29, 2022 04:47
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.

None yet

1 participant