Skip to content

Commit

Permalink
lib: init toRON
Browse files Browse the repository at this point in the history
  • Loading branch information
cjshearer committed Nov 25, 2024
1 parent 8cf9cb2 commit 43d0794
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 0 deletions.
50 changes: 50 additions & 0 deletions modules/lib/generators.nix
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,56 @@
${concatStringsSep "\n" (mapAttrsToList convertAttributeToKDL attrs)}
'';

# https://github.com/ron-rs/ron/blob/master/docs/grammar.md
toRON = { }:
let
inherit (lib)
filterAttrs concatStrings concatStringsSep mapAttrsToList boolToString;
inherit (builtins) typeOf toString;

serialize = indentLevel: value:
let
indent = lib.strings.replicate indentLevel " ";
indentNested = indent + " ";
serializeNested = v: serialize (indentLevel + 1) v;
serializationRules = {
int = toString;
float = toString;
bool = boolToString;
string = s: ''"${toString s}"'';
path = toString;
null = toString;
set = hashMap:
lib.pipe hashMap [
(filterAttrs (k: v: v != null))
(mapAttrsToList
(k: v: "${indentNested}${k}: ${serializeNested v}"))
(concatStringsSep ("," + "\n"))
(hashMap: "{" + "\n" + hashMap + "\n" + indent + "}")
];
list = list:
lib.pipe list [
(map (v: "${indentNested}${serializeNested v}"))
(concatStringsSep ("," + "\n"))
(list: "[" + "\n" + list + "\n" + indent + "]")
];
lambda = lambda:
let
ident = ({ident = "";} // (builtins.functionArgs lambda)).ident;
lambdaResult = lambda { };
in (serializationRules // {
string = toString;
list = tuple:
lib.pipe tuple [
(map (v: "${indentNested}${serializeNested v}"))
(concatStringsSep ("," + "\n"))
(tuple: ident + "(" + "\n" + tuple + "\n" + indent + ")")
];
}).${typeOf lambdaResult} lambdaResult;
};
in serializationRules.${typeOf value} value;
in serialize 0;

toSCFG = { }:
let
inherit (lib) concatStringsSep mapAttrsToList any;
Expand Down
1 change: 1 addition & 0 deletions tests/lib/generators/default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
generators-tokdl = ./tokdl.nix;
generators-toron = ./toron.nix;
generators-toscfg-empty = ./toscfg-empty.nix;
generators-toscfg-example = ./toscfg-example.nix;
}
71 changes: 71 additions & 0 deletions tests/lib/generators/toron-result.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
[
{
byte: b'0',
float_exp: -1.0e-16,
float_frac: .1,
float_int: 1000,
float_std: 1000.000000,
float_suffix: -.1f64,
integer: 0,
integer_suffix: i8,
unsigned_binary: 0b10,
unsigned_decimal: 10,
unsigned_hexadecimal: 0x10,
unsigned_octal: 0o10
},
{
byte_string_raw: br##"Hello, World!"##,
byte_string_std: b"Hello, World!",
string_escape_ascii: "\'",
string_escape_byte: "\x0A",
string_escape_unicode: "\u{0A0A}",
string_raw: r##"This is a "raw string".
It can contain quotations or backslashes (\)!"##,
string_std: "Hello, World!"
},
{
char: 'a'
},
{
boolean: true
},
{
option_none: None,
option_some: Some(10)
},
{
list: [
1,
2,
3
]
},
{
map: {
a: 1,
b: 2,
c: 3
}
},
{
tuple: (
1,
2,
3
)
},
{
tuple_struct: (
1,
2,
3
),
tuple_struct_ident: MyTupleStruct(
1,
2,
3
),
unit_struct: (),
unit_struct_ident: MyUnitStruct
}
]
77 changes: 77 additions & 0 deletions tests/lib/generators/toron.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{ config, lib, ... }:

{
home.file."toron-result.txt".text = lib.hm.generators.toRON { } [
# numbers
{
byte = f: "b'0'";
# while nix supports scientific notation, it will convert -1.0e-16 to -0.000000
float_exp = f: "-1.0e-16";
# while nix supports fractional notation, nix fmt will complain about .1
float_frac = f: ".1";
float_int = 1000;
float_std = 1000.0;
float_suffix = f: "-.1f64";
integer = 0;
integer_suffix = f: "i8";
unsigned_binary = f: "0b10";
unsigned_decimal = 10;
unsigned_hexadecimal = f: "0x10";
unsigned_octal = f: "0o10";
}
# strings
{
byte_string_raw = f: ''br##"Hello, World!"##'';
byte_string_std = f: ''b"Hello, World!"'';
string_escape_ascii = "\\'";
string_escape_byte = "\\x0A";
string_escape_unicode = "\\u{0A0A}";
string_raw = f: ''
r##"This is a "raw string".
It can contain quotations or backslashes (\)!"##'';
string_std = "Hello, World!";
}
# char
{
char = f: "'a'";
}
# boolean
{
boolean = true;
}
# option
{
option_none = f: "None";
option_some = f: "Some(10)";
}
# list
{
list = [ 1 2 3 ];
}
# map
{
map = {
a = 1;
b = 2;
c = 3;
};
}
# tuple
{
tuple = f: [ 1 2 3 ];
}
# struct
{
tuple_struct = f: [ 1 2 3 ];
tuple_struct_ident = f: [ 1 2 3 ];
unit_struct = f: "()";
unit_struct_ident = f: "MyUnitStruct";
}
];

nmt.script = ''
assertFileContent \
home-files/toron-result.txt \
${./toron-result.txt}
'';
}

0 comments on commit 43d0794

Please sign in to comment.