Skip to content

Commit

Permalink
Create two identical enum types, one of them untagged
Browse files Browse the repository at this point in the history
To workaround DioxusLabs/sdk#20
  • Loading branch information
srid committed Nov 8, 2023
1 parent 95da088 commit c3b64f2
Showing 1 changed file with 46 additions and 15 deletions.
61 changes: 46 additions & 15 deletions crates/nix_rs/src/flake/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,21 @@ use std::{

/// Represents the "outputs" of a flake
///
/// This structure is currently produced by `nix flake show`
/// This structure is currently produced by `nix flake show`, thus to parse it we must toggle serde untagged.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum FlakeOutputs {
Val(Val),
Attrset(BTreeMap<String, FlakeOutputs>),
}

impl FlakeOutputs {
/// Run `nix flake show` on the given flake url

#[tracing::instrument(name = "flake-show")]
pub async fn from_nix(
nix_cmd: &crate::command::NixCmd,
flake_url: &super::url::FlakeUrl,
) -> Result<Self, crate::command::NixCmdError> {
let v = nix_cmd
.run_with_args_expecting_json(&[
"flake",
"show",
"--legacy", // for showing nixpkgs legacyPackages
"--allow-import-from-derivation",
"--json",
&flake_url.to_string(),
])
.await?;
Ok(v)
let v = FlakeOutputsUntagged::from_nix(nix_cmd, flake_url).await?;
Ok(v.into_flake_outputs())
}

/// Get the non-attrset value
Expand Down Expand Up @@ -122,3 +110,46 @@ impl Display for Type {
f.write_str(&format!("{:?}", self))
}
}

/// This type is identical to [FlakeOutputs] except for the serde untagged attribute, which enables parsing the JSON output of `nix flake show`.
///
/// This separation exists to workaround https://github.com/DioxusLabs/dioxus-std/issues/20
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
enum FlakeOutputsUntagged {
UVal(Val),
UAttrset(BTreeMap<String, FlakeOutputsUntagged>),
}

impl FlakeOutputsUntagged {
/// Run `nix flake show` on the given flake url
#[tracing::instrument(name = "flake-show")]
async fn from_nix(
nix_cmd: &crate::command::NixCmd,
flake_url: &super::url::FlakeUrl,
) -> Result<Self, crate::command::NixCmdError> {
let v = nix_cmd
.run_with_args_expecting_json(&[
"flake",
"show",
"--legacy", // for showing nixpkgs legacyPackages
"--allow-import-from-derivation",
"--json",
&flake_url.to_string(),
])
.await?;
Ok(v)
}

/// Convert to [FlakeOutputs]
fn into_flake_outputs(self) -> FlakeOutputs {
match self {
Self::UVal(v) => FlakeOutputs::Val(v),
Self::UAttrset(v) => FlakeOutputs::Attrset(
v.into_iter()
.map(|(k, v)| (k, v.into_flake_outputs()))
.collect(),
),
}
}
}

0 comments on commit c3b64f2

Please sign in to comment.