feat: add parsing draft

This commit is contained in:
itsscb 2025-04-22 21:46:07 +02:00
parent abc1b205a9
commit 3fb7f0926e
2 changed files with 40 additions and 2 deletions

View File

@ -2,5 +2,8 @@
name = "befehlswerk"
version = "0.1.0"
edition = "2024"
description = "PowerShell Scripts for EVERYONE!"
repository = "https://git.itsscb.de/itsscb/BefehlsWerk"
[dependencies]
serde_json = "1.0.140"

View File

@ -1,3 +1,38 @@
fn main() {
println!("Hello, world!");
use std::process::Command;
#[allow(dead_code)]
const PARSE_FUNCTION_RAW: &str = include_str!("../powershell/get-metadata.ps1");
#[allow(dead_code)]
fn get_parse_function() -> String {
format!("function parse_ps() {{{PARSE_FUNCTION_RAW}}}")
}
#[allow(dead_code)]
fn parse_powershell_file(path: &str) -> Result<serde_json::Value, String> {
let func = get_parse_function();
let ps_runner = format!("{func}; parse_ps -Path '{path}' | ConvertTo-Json -Depth 10");
let output = Command::new("pwsh")
.args(["-NoProfile", "-Command", &ps_runner])
.output()
.map_err(|e| e.to_string())?;
if output.status.success() {
let stdout_str = String::from_utf8_lossy(&output.stdout);
Ok(serde_json::from_str(&stdout_str).map_err(|e| e.to_string())?)
} else {
Err(String::from("Failed to run PowerShell"))
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_parse_powershell_file() {
let got = parse_powershell_file("powershell/get-metadata.ps1");
assert!(got.is_ok());
}
}