Skip to content

Commit

Permalink
Add more public functions
Browse files Browse the repository at this point in the history
Mainly just added a function that collects all attributes in a nix configuration and outputs them as a hashmap
  • Loading branch information
vlinkz committed May 31, 2022
1 parent da182e9 commit 954e4a7
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 34 deletions.
135 changes: 124 additions & 11 deletions Cargo.lock

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

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "nix-editor"
version = "0.2.8"
version = "0.2.9"
edition = "2021"
license = "GPL-3.0-or-later"
license = "MIT"
description = "Some functions to modify NixOS configuration files"
repository = "https://github.com/vlinkz/nix-editor/"
readme = "README.md"
Expand All @@ -14,6 +14,7 @@ include = [
]

[dependencies]
clap = { version = "3.1.8", features = ["derive"] }
clap = { version = "3.1.18", features = ["derive"] }
rnix = "0.10.1"
owo-colors = "3.3.0"
owo-colors = "3.4.0"
failure = { version = "0.1.8", features = ["derive"] }
30 changes: 15 additions & 15 deletions flake.lock

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

23 changes: 19 additions & 4 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::collections::HashMap;

use rnix::{self, SyntaxKind, SyntaxNode};

use crate::read::ReadError;

enum AttrTypes {
String,
Int,
Expand Down Expand Up @@ -43,7 +45,20 @@ pub fn findattr(configbase: &SyntaxNode, name: &str) -> Option<SyntaxNode> {
return None;
}

pub fn collectattrs(configbase: &SyntaxNode, map: &mut HashMap<String, SyntaxNode>) /*-> HashMap<String, String>*/
pub fn get_collection(f: String) -> Result<HashMap<String, String>, ReadError> {
let mut map = HashMap::new();
let ast = rnix::parse(&f);
let configbase = match getcfgbase(&ast.node()) {
Some(x) => x,
None => {
return Err(ReadError::ParseError);
}
};
collectattrs(&configbase, &mut map);
Ok(map)
}

pub fn collectattrs(configbase: &SyntaxNode, map: &mut HashMap<String, String>) /*-> HashMap<String, String>*/
{
for child in configbase.children() {
if child.kind() == SyntaxKind::NODE_KEY_VALUE {
Expand All @@ -58,7 +73,7 @@ pub fn collectattrs(configbase: &SyntaxNode, map: &mut HashMap<String, SyntaxNod
map.insert(format!("{}.{}", nodekey.to_string(), nk),v.clone());
}
} else {
map.insert(nodekey.to_string(), value.clone());
map.insert(nodekey.to_string(), value.to_string());
}
}
}
Expand All @@ -72,7 +87,7 @@ pub fn getkey(node: &SyntaxNode) -> Vec<String> {
key.push(child.text().to_string());
}
}
return key;
key
}

pub fn getcfgbase(node: &SyntaxNode) -> Option<SyntaxNode> {
Expand All @@ -94,5 +109,5 @@ pub fn getcfgbase(node: &SyntaxNode) -> Option<SyntaxNode> {
None => {}
}
}
return None;
None
}
6 changes: 6 additions & 0 deletions src/read.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
use crate::parse::{findattr, getcfgbase};
use failure::Fail;
use rnix::{SyntaxKind, SyntaxNode};

#[derive(Fail, Debug)]
pub enum ReadError {
#[fail(display = "Read Error: Error while parsing.")]
ParseError,
#[fail(display = "Read Error: No attributes.")]
NoAttr,
#[fail(display = "Read Error: Error with array.")]
ArrayError,
}

Expand Down

0 comments on commit 954e4a7

Please sign in to comment.