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

Updates to NamelessMatchSpec to allow deserializing #299

Merged
merged 1 commit into from
Aug 29, 2023

Conversation

travishathaway
Copy link
Contributor

While working on this issue:

I realized that I was going to need to be able to deserialize the NamelessMatchSpec struct from a mapping. I believe the easiest way to allow this is by adding Deserialize to derive in the correct places.

On NamelessMatchSpec I also chose to use DisplayFromStr for version and build because I believe that's the easiest way to do it. I would be happy to change it if that's not right though.

Below is my simplified use case:

use std::fs;
use std::path::PathBuf;

use indexmap::IndexMap;
use clap::{Parser};
use serde::Deserialize;
use serde_with::{serde_as, DisplayFromStr, PickFirst};
use rattler_conda_types::NamelessMatchSpec;

#[derive(Parser)]
#[command(name = "tt - TOML test")]
#[command(author = "Travis Hathaway")]
#[command(version = "0.1.0")]
#[command(about = "Tests with TOML parsing")]
struct Cli {
    /// TOML File
    toml_file: PathBuf,
}

/// Describes the contents of a test project manifest.
#[serde_as]
#[derive(Debug, Clone, Deserialize)]
pub struct MyManifest {
    #[serde_as(as = "IndexMap<_, PickFirst<(_, DisplayFromStr)>>")]
    dependencies: IndexMap<String, NamelessMatchSpec>
}


fn main() -> Result<(), Box<dyn std::error::Error>>{
    let cli = Cli::parse();

    println!("{:?}\n", cli.toml_file);

    if cli.toml_file.exists() {
        let contents = fs::read_to_string(cli.toml_file)?;
        let manifest = toml_edit::de::from_str::<MyManifest>(&contents)?;

        println!("Dependencies:");
        println!("{:?}", manifest.dependencies);

    } else {
        println!("File does not exist");
    }

    Ok(())
}

@baszalmstra baszalmstra merged commit 6d8eaa3 into conda:main Aug 29, 2023
@baszalmstra
Copy link
Collaborator

Thanks @travishathaway!

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