Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ opt-level = 0
# and is only used when debugging.
debug = 1

[profile.dev.package.backtrace]
# color-eyre: Improves performance for debug builds
opt-level = 3

[profile.release]
lto = "thin"
# Optimize for binary size. In this case also turns out to be the fastest to
Expand Down
20 changes: 20 additions & 0 deletions crates/punktf-lib-gsgh/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "punktf-lib-gsgh"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
keywords.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cfg-if = "1.0.0"
log.workspace = true
minijinja = { version = "1.0.6", default-features = false, features = ["builtins", "adjacent_loop_items", "debug", "deserialization"] }
semver = { version = "1.0.18", features = ["serde"] }
serde = { workspace = true, features = ["derive"] }
serde_yaml = "0.9.25"
thiserror.workspace = true
version-compare = "0.1.1"
versions = { version = "5.0.0", features = ["serde"] }
47 changes: 47 additions & 0 deletions crates/punktf-lib-gsgh/profile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
version: 1.0.0

aliases:
- Foo
- Bar

extends:
- Parent

env:
Foo: Bar
Bool: true
Number: 2.4

transformers:
- type: line_terminator
with: LF

target: /tmp

pre_hooks:
type: inline
with: |
set -eoux pipefail
echo 'Foo'

items:
- priority: 5
env:
Foo: Bar
Bool: true
pre_hook:
type: inline
with: |-
set -eoux pipefail
echo 'Foo'
path: /dev/null
merge:
type: hook
with:
type: inline
with: |
#!/usr/bin/env bash

set -eoux pipefail

echo "test"
80 changes: 80 additions & 0 deletions crates/punktf-lib-gsgh/src/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::{
collections::{btree_set, BTreeMap, BTreeSet},
};

use serde::{Deserialize, Serialize};

use crate::value::Value;

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Environment(pub BTreeMap<String, Value>);

impl Environment {
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct LayeredEnvironment(Vec<(&'static str, Environment)>);

impl LayeredEnvironment {
pub fn push(&mut self, name: &'static str, env: Environment) {
self.0.push((name, env));
}

pub fn pop(&mut self) -> Option<(&'static str, Environment)> {
self.0.pop()
}

pub fn keys(&self) -> BTreeSet<&str> {
self.0
.iter()
.flat_map(|(_, layer)| layer.0.keys())
.map(|key| key.as_str())
.collect()
}

pub fn get(&self, key: &str) -> Option<&Value> {
for (_, layer) in self.0.iter() {
if let Some(value) = layer.0.get(key) {
return Some(value);
}
}

None
}

pub fn iter(&self) -> LayeredIter<'_> {
LayeredIter::new(self)
}

pub fn as_str_map(&self) -> BTreeMap<&str, String> {
self.iter()
// TODO: Optimize
// `trim` to remove trailing `\n`
.map(|(k, v)| (k, serde_yaml::to_string(v).unwrap().trim().into()))
.collect()
}
}

pub struct LayeredIter<'a> {
env: &'a LayeredEnvironment,
keys: btree_set::IntoIter<&'a str>,
}

impl<'a> LayeredIter<'a> {
pub fn new(env: &'a LayeredEnvironment) -> Self {
let keys = env.keys().into_iter();
Self { env, keys }
}
}

impl<'a> Iterator for LayeredIter<'a> {
type Item = (&'a str, &'a Value);

fn next(&mut self) -> Option<Self::Item> {
let key = self.keys.next()?;
Some((key, self.env.get(key)?))
}
}
Loading