1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2024-12-14 11:57:55 +00:00

lib: init toRON

This commit is contained in:
cjshearer 2024-12-02 18:26:32 -05:00
parent 8cf9cb2ee7
commit 3742ae9765
4 changed files with 356 additions and 0 deletions

View file

@ -143,6 +143,102 @@
${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
optionalString;
inherit (lib.strings) floatToString;
inherit (builtins) any hasAttr isAttrs toString typeOf;
tab = " ";
serialize = indentLevel: input:
let
indent = lib.strings.replicate indentLevel tab;
indentNested = indent + tab;
serializeNested = v: serialize (indentLevel + 1) v;
name = input._name or "";
suffix = input._suffix or "";
value = if isAttrs input && any (attr: hasAttr attr input) [
"_name"
"_suffix"
"_type"
"_value"
] then
input._value or null
else
input;
delimiter = {
apostrophe = {
open = "'";
close = "'";
};
brace = {
open = "{";
close = "}";
};
bracket = {
open = "[";
close = "]";
};
none = {
open = "";
close = "";
};
parenthesis = {
open = "(";
close = ")";
};
quote = {
open = ''"'';
close = ''"'';
};
}.${
{
char = "apostrophe";
enum = if isNull value then "none" else "parenthesis";
list = if name == "" then "bracket" else "parenthesis";
map = "brace";
set = if name == "" then "brace" else "parenthesis";
string = "quote";
struct = "parenthesis";
tuple = "parenthesis";
}.${input._type or (typeOf value)} or "none"
};
serializationRules = {
int = toString;
float = floatToString;
path = toString;
bool = boolToString;
string = toString;
null = _: "";
set = set:
lib.pipe set [
(mapAttrsToList
(k: v: "${indentNested}${k}: ${serializeNested v}"))
(concatStringsSep ("," + "\n"))
(v:
optionalString (v != "") "\n" + v
+ optionalString (v != "") ("\n" + indent))
];
list = list:
lib.pipe list [
(map (v: "${indentNested}${serializeNested v}"))
(concatStringsSep ("," + "\n"))
(v:
optionalString (v != "") "\n" + v
+ optionalString (v != "") ("\n" + indent))
];
};
in name + delimiter.open + (serializationRules.${typeOf value} value)
+ delimiter.close + suffix;
in serialize 0;
toSCFG = { }:
let
inherit (lib) concatStringsSep mapAttrsToList any;

View file

@ -1,5 +1,6 @@
{
generators-tokdl = ./tokdl.nix;
generators-toron = ./toron.nix;
generators-toscfg-empty = ./toscfg-empty.nix;
generators-toscfg-example = ./toscfg-example.nix;
}

View file

@ -0,0 +1,88 @@
[
{
byte: b'0',
float_exp: -1.000000e-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: 0xFF,
unsigned_octal: 0o10
},
{
char: 'a'
},
{
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!"
},
{
boolean: true
},
{
enum_nested: Some(Some(10)),
option_none_explicit: None,
option_none_implicit: None,
option_some: Some(10)
},
{
list: [
1,
2,
3
]
},
{
map_explicit: {
a: 1,
b: 2,
c: 3
},
map_implicit: {
a: 1,
b: 2,
c: 3
}
},
{
tuple: (
1,
2,
3
)
},
{
named_struct_explicit: NamedStruct(
a: 1,
b: 2,
c: 3
),
named_struct_implicit: NamedStruct(
a: 1,
b: 2,
c: 3
),
tuple_struct_explicit: TupleStruct(
1,
2,
3
),
tuple_struct_implicit: TupleStruct(
1,
2,
3
),
unit_struct: (),
unit_struct_ident: MyUnitStruct
}
]

View file

@ -0,0 +1,171 @@
{ config, lib, ... }:
{
home.file."toron-result.ron".text = lib.hm.generators.toRON { } [
# numbers
{
byte = {
_type = "char";
_name = "b";
_value = "0";
};
# TODO: nix adds several trailing zeros to floats, is that an issue?
float_exp = {
_value = -1.0;
_suffix = "e-16";
};
# while nix supports fractional notation, nix fmt will complain about .1
# that said, consumers of RON shouldn't need fractional notation
float_frac = { _name = ".1"; };
float_int = 1000;
# TODO: nix adds several trailing zeros to floats, is that an issue?
float_std = 1000.0;
float_suffix = { _name = "-.1f64"; };
integer = 0;
integer_suffix = {
_name = "i";
_value = 8;
};
unsigned_binary = {
_name = "0b";
_value = 10;
};
unsigned_decimal = 10;
unsigned_hexadecimal = {
_type = "hex";
_name = "0x";
_value = "FF";
};
unsigned_octal = {
_name = "0o";
_value = 10;
};
}
# chars
{
char = {
_type = "char";
_value = "a";
};
}
# strings
{
byte_string_raw = {
_name = "br##";
_value = "Hello, World!";
_suffix = "##";
};
byte_string_std = {
_name = "b";
_value = "Hello, World!";
};
string_escape_ascii = "\\'";
string_escape_byte = "\\x0A";
string_escape_unicode = "\\u{0A0A}";
string_raw = {
_name = "r##";
_value = ''
This is a "raw string".
It can contain quotations or backslashes\!'';
_suffix = "##";
};
string_std = "Hello, World!";
}
# boolean
{
boolean = true;
}
# option/enum
{
enum_nested = {
_type = "enum";
_name = "Some";
_value = {
_type = "enum";
_name = "Some";
_value = 10;
};
};
option_none_explicit = {
_type = "enum";
_name = "None";
_value = null;
};
option_none_implicit = {
_name = "None";
};
option_some = {
_type = "enum";
_name = "Some";
_value = 10;
};
}
# list
{
list = [ 1 2 3 ];
}
# map
{
map_explicit = {
_type = "map";
_value = {
a = 1;
b = 2;
c = 3;
};
};
map_implicit = {
a = 1;
b = 2;
c = 3;
};
}
# tuple
{
tuple = {
_type = "tuple";
_value = [ 1 2 3 ];
};
}
# struct
{
named_struct_explicit = {
_name = "NamedStruct";
_type = "struct";
_value = {
a = 1;
b = 2;
c = 3;
};
};
named_struct_implicit = {
_name = "NamedStruct";
_value = {
a = 1;
b = 2;
c = 3;
};
};
tuple_struct_explicit = {
_type = "tuple";
_name = "TupleStruct";
_value = [ 1 2 3 ];
};
tuple_struct_implicit = {
_name = "TupleStruct";
_value = [ 1 2 3 ];
};
unit_struct = {
_type = "struct";
_value = [ ];
};
unit_struct_ident = { _name = "MyUnitStruct"; };
}
];
nmt.script = ''
assertFileContent \
home-files/toron-result.ron \
${./toron-result.ron}
'';
}