From fa489bf7bc8577a0f9a2412da6bf80ffba0c4394 Mon Sep 17 00:00:00 2001 From: clerie Date: Sun, 9 Feb 2025 21:17:13 +0100 Subject: [PATCH] Move flake metadata specification to seperate file --- src/bin/scan-flake.rs | 152 ++---------------------------------------- src/flake.rs | 152 ++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 3 files changed, 159 insertions(+), 146 deletions(-) create mode 100644 src/flake.rs diff --git a/src/bin/scan-flake.rs b/src/bin/scan-flake.rs index 5d03653..4ed7621 100644 --- a/src/bin/scan-flake.rs +++ b/src/bin/scan-flake.rs @@ -1,5 +1,4 @@ use anyhow::{ - anyhow, Context, Result, }; @@ -9,157 +8,18 @@ use chrono::{ use clap::{ Parser, }; -use serde::{ - Deserialize, +use flake_tracker::{ + flake::{ + FlakeLocksNodeInputs, + FlakeMetadata, + FlakeUri, + }, }; use sqlx::{ sqlite::SqlitePoolOptions, }; -use std::collections::HashMap; use std::process::Command; -#[derive(Deserialize, Debug)] -struct FlakeMetadata { - lastModified: i64, - locked: FlakeLockedInfo, - locks: FlakeLocks, - original: FlakeSource, - path: String, - resolved: FlakeSource, - revision: String, -} - -#[derive(Deserialize, Debug, Clone)] -struct FlakeSource { - owner: Option, - repo: Option, - r#ref: Option, - rev: Option, - r#type: String, - url: Option, -} - -fn assemble_flake_git_uri(flake_source: &FlakeSource) -> Result { - let mut out = String::new(); - out.push_str("git+"); - out.push_str(&flake_source.url.clone() - .context("Flake git uri does not contain an url")?); - - let mut query: Vec = Vec::new(); - - if let Some(r#ref) = &flake_source.r#ref { - query.push(format!("ref={}", r#ref)); - } - - if let Some(rev) = &flake_source.rev { - query.push(format!("rev={}", rev)); - } - - if query.len() > 0 { - out.push_str("?"); - out.push_str(&query.join("&")); - } - - Ok(out) -} - -fn assemble_flake_github_uri(flake_source: &FlakeSource) -> Result { - let mut out = String::new(); - out.push_str("github:"); - out.push_str(&flake_source.owner.clone() - .context("Flake github uri does not contain an owner")?); - out.push_str("/"); - out.push_str(&flake_source.repo.clone() - .context("Flake github uri does not contain an repo")?); - - if let Some(rev) = &flake_source.rev { - out.push_str("/"); - out.push_str(&rev); - } - - Ok(out) -} - -fn assemble_flake_tarball_uri(flake_source: &FlakeSource) -> Result { - let mut out = String::new(); - out.push_str(&flake_source.url.clone() - .context("Flake tarball uri does not contain an url")?); - - if let Some(rev) = &flake_source.rev { - out.push_str("?rev="); - out.push_str(rev); - } - - Ok(out) -} - -trait FlakeUri { - fn flake_uri(&self) -> Result; -} - -impl FlakeUri for FlakeSource { - fn flake_uri(&self) -> Result { - match self.r#type.as_str() { - "git" => assemble_flake_git_uri(self), - "github" => assemble_flake_github_uri(self), - "tarball" => assemble_flake_tarball_uri(self), - other => Err(anyhow!("Unsupported flake uri type {}", other)), - } - } -} - -#[derive(Deserialize, Debug, Clone)] -struct FlakeLockedInfo { - lastModified: i64, - narHash: String, - owner: Option, - repo: Option, - r#ref: Option, - rev: Option, - r#type: String, - url: Option, -} - -impl FlakeUri for FlakeLockedInfo { - fn flake_uri(&self) -> Result { - Into::::into(self).flake_uri() - } -} - -impl Into for &FlakeLockedInfo { - fn into(self) -> FlakeSource { - FlakeSource { - owner: self.owner.clone(), - repo: self.repo.clone(), - r#ref: self.r#ref.clone(), - rev: self.rev.clone(), - r#type: self.r#type.clone(), - url: self.url.clone(), - } - } -} - -#[derive(Deserialize, Debug)] -struct FlakeLocks { - nodes: HashMap, - root: String, - version: u64, -} - -#[derive(Deserialize, Debug, Clone)] -#[serde(untagged)] -enum FlakeLocksNodeInputs { - String(String), - Array(Vec), -} - -#[derive(Deserialize, Debug)] -struct FlakeLocksNode { - inputs: Option>, - locked: Option, - original: Option, -} - #[derive(Parser)] #[command(version, about, long_about = None)] struct Cli { diff --git a/src/flake.rs b/src/flake.rs new file mode 100644 index 0000000..d69edc6 --- /dev/null +++ b/src/flake.rs @@ -0,0 +1,152 @@ +use anyhow::{ + anyhow, + Context, + Result, +}; +use serde::{ + Deserialize, +}; +use std::collections::HashMap; + +#[derive(Deserialize, Debug)] +pub struct FlakeMetadata { + pub lastModified: i64, + pub locked: FlakeLockedInfo, + pub locks: FlakeLocks, + pub original: FlakeSource, + pub path: String, + pub resolved: FlakeSource, + pub revision: String, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct FlakeSource { + pub owner: Option, + pub repo: Option, + pub r#ref: Option, + pub rev: Option, + pub r#type: String, + pub url: Option, +} + +pub fn assemble_flake_git_uri(flake_source: &FlakeSource) -> Result { + let mut out = String::new(); + out.push_str("git+"); + out.push_str(&flake_source.url.clone() + .context("Flake git uri does not contain an url")?); + + let mut query: Vec = Vec::new(); + + if let Some(r#ref) = &flake_source.r#ref { + query.push(format!("ref={}", r#ref)); + } + + if let Some(rev) = &flake_source.rev { + query.push(format!("rev={}", rev)); + } + + if query.len() > 0 { + out.push_str("?"); + out.push_str(&query.join("&")); + } + + Ok(out) +} + +pub fn assemble_flake_github_uri(flake_source: &FlakeSource) -> Result { + let mut out = String::new(); + out.push_str("github:"); + out.push_str(&flake_source.owner.clone() + .context("Flake github uri does not contain an owner")?); + out.push_str("/"); + out.push_str(&flake_source.repo.clone() + .context("Flake github uri does not contain an repo")?); + + if let Some(rev) = &flake_source.rev { + out.push_str("/"); + out.push_str(&rev); + } + + Ok(out) +} + +pub fn assemble_flake_tarball_uri(flake_source: &FlakeSource) -> Result { + let mut out = String::new(); + out.push_str(&flake_source.url.clone() + .context("Flake tarball uri does not contain an url")?); + + if let Some(rev) = &flake_source.rev { + out.push_str("?rev="); + out.push_str(rev); + } + + Ok(out) +} + +pub trait FlakeUri { + fn flake_uri(&self) -> Result; +} + +impl FlakeUri for FlakeSource { + fn flake_uri(&self) -> Result { + match self.r#type.as_str() { + "git" => assemble_flake_git_uri(self), + "github" => assemble_flake_github_uri(self), + "tarball" => assemble_flake_tarball_uri(self), + other => Err(anyhow!("Unsupported flake uri type {}", other)), + } + } +} + +#[derive(Deserialize, Debug, Clone)] +pub struct FlakeLockedInfo { + pub lastModified: i64, + pub narHash: String, + pub owner: Option, + pub repo: Option, + pub r#ref: Option, + pub rev: Option, + pub r#type: String, + pub url: Option, +} + +impl FlakeUri for FlakeLockedInfo { + fn flake_uri(&self) -> Result { + Into::::into(self).flake_uri() + } +} + +impl Into for &FlakeLockedInfo { + fn into(self) -> FlakeSource { + FlakeSource { + owner: self.owner.clone(), + repo: self.repo.clone(), + r#ref: self.r#ref.clone(), + rev: self.rev.clone(), + r#type: self.r#type.clone(), + url: self.url.clone(), + } + } +} + +#[derive(Deserialize, Debug)] +pub struct FlakeLocks { + pub nodes: HashMap, + pub root: String, + pub version: u64, +} + +#[derive(Deserialize, Debug, Clone)] +#[serde(untagged)] +pub enum FlakeLocksNodeInputs { + String(String), + Array(Vec), +} + +#[derive(Deserialize, Debug)] +pub struct FlakeLocksNode { + pub inputs: Option>, + pub locked: Option, + pub original: Option, +} + diff --git a/src/lib.rs b/src/lib.rs index 656d81a..cdf31dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod flake; pub mod storage; pub mod templates; pub mod utils;