Move flake metadata specification to seperate file

This commit is contained in:
clerie 2025-02-09 21:17:13 +01:00
parent 070f002c88
commit fa489bf7bc
3 changed files with 159 additions and 146 deletions

View File

@ -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<String>,
repo: Option<String>,
r#ref: Option<String>,
rev: Option<String>,
r#type: String,
url: Option<String>,
}
fn assemble_flake_git_uri(flake_source: &FlakeSource) -> Result<String> {
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<String> = 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<String> {
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<String> {
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<String>;
}
impl FlakeUri for FlakeSource {
fn flake_uri(&self) -> Result<String> {
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<String>,
repo: Option<String>,
r#ref: Option<String>,
rev: Option<String>,
r#type: String,
url: Option<String>,
}
impl FlakeUri for FlakeLockedInfo {
fn flake_uri(&self) -> Result<String> {
Into::<FlakeSource>::into(self).flake_uri()
}
}
impl Into<FlakeSource> 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<String, FlakeLocksNode>,
root: String,
version: u64,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(untagged)]
enum FlakeLocksNodeInputs {
String(String),
Array(Vec<String>),
}
#[derive(Deserialize, Debug)]
struct FlakeLocksNode {
inputs: Option<HashMap<String, FlakeLocksNodeInputs>>,
locked: Option<FlakeLockedInfo>,
original: Option<FlakeSource>,
}
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {

152
src/flake.rs Normal file
View File

@ -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<String>,
pub repo: Option<String>,
pub r#ref: Option<String>,
pub rev: Option<String>,
pub r#type: String,
pub url: Option<String>,
}
pub fn assemble_flake_git_uri(flake_source: &FlakeSource) -> Result<String> {
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<String> = 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<String> {
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<String> {
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<String>;
}
impl FlakeUri for FlakeSource {
fn flake_uri(&self) -> Result<String> {
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<String>,
pub repo: Option<String>,
pub r#ref: Option<String>,
pub rev: Option<String>,
pub r#type: String,
pub url: Option<String>,
}
impl FlakeUri for FlakeLockedInfo {
fn flake_uri(&self) -> Result<String> {
Into::<FlakeSource>::into(self).flake_uri()
}
}
impl Into<FlakeSource> 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<String, FlakeLocksNode>,
pub root: String,
pub version: u64,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum FlakeLocksNodeInputs {
String(String),
Array(Vec<String>),
}
#[derive(Deserialize, Debug)]
pub struct FlakeLocksNode {
pub inputs: Option<HashMap<String, FlakeLocksNodeInputs>>,
pub locked: Option<FlakeLockedInfo>,
pub original: Option<FlakeSource>,
}

View File

@ -1,3 +1,4 @@
pub mod flake;
pub mod storage;
pub mod templates;
pub mod utils;