core/lib/gvariant.nix

323 lines
7.5 KiB
Nix
Raw Normal View History

2024-05-01 22:14:04 +00:00
/*
A partial and basic implementation of GVariant formatted strings.
See [GVariant Format Strings](https://docs.gtk.org/glib/gvariant-format-strings.html) for details.
:::{.warning}
This API is not considered fully stable and it might therefore
change in backwards incompatible ways without prior notice.
:::
*/
# This file is based on https://github.com/nix-community/home-manager
# Copyright (c) 2017-2022 Home Manager contributors
{ lib }:
let
inherit (lib)
2024-06-30 08:16:52 +00:00
concatMapStringsSep
concatStrings
escape
head
replaceStrings
;
2024-05-01 22:14:04 +00:00
mkPrimitive = t: v: {
_type = "gvariant";
type = t;
value = v;
__toString = self: "@${self.type} ${toString self.value}"; # https://docs.gtk.org/glib/gvariant-text.html
};
type = {
arrayOf = t: "a${t}";
maybeOf = t: "m${t}";
tupleOf = ts: "(${concatStrings ts})";
dictionaryEntryOf = nameType: valueType: "{${nameType}${valueType}}";
string = "s";
boolean = "b";
uchar = "y";
int16 = "n";
uint16 = "q";
int32 = "i";
uint32 = "u";
int64 = "x";
uint64 = "t";
double = "d";
variant = "v";
};
2024-06-30 08:16:52 +00:00
/*
Check if a value is a GVariant value
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
isGVariant :: Any -> Bool
2024-05-01 22:14:04 +00:00
*/
isGVariant = v: v._type or "" == "gvariant";
in
rec {
inherit type isGVariant;
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant value that most closely matches the given Nix value.
If no GVariant value can be found unambiguously then error is thrown.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkValue :: Any -> gvariant
2024-05-01 22:14:04 +00:00
*/
2024-06-30 08:16:52 +00:00
mkValue =
v:
2024-05-01 22:14:04 +00:00
if builtins.isBool v then
mkBoolean v
else if builtins.isFloat v then
mkDouble v
else if builtins.isString v then
mkString v
else if builtins.isList v then
mkArray v
else if isGVariant v then
v
else
throw "The GVariant type of ${v} can't be inferred.";
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant array from the given type of the elements and a Nix list.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkArray :: [Any] -> gvariant
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Example:
# Creating a string array
lib.gvariant.mkArray [ "a" "b" "c" ]
2024-05-01 22:14:04 +00:00
*/
2024-06-30 08:16:52 +00:00
mkArray =
elems:
2024-05-01 22:14:04 +00:00
let
vs = map mkValue (lib.throwIf (elems == [ ]) "Please create empty array with mkEmptyArray." elems);
2024-06-30 08:16:52 +00:00
elemType = lib.throwIfNot (lib.all (t: (head vs).type == t) (
map (v: v.type) vs
)) "Elements in a list should have same type." (head vs).type;
2024-05-01 22:14:04 +00:00
in
2024-06-30 08:16:52 +00:00
mkPrimitive (type.arrayOf elemType) vs
// {
__toString = self: "@${self.type} [${concatMapStringsSep "," toString self.value}]";
2024-05-01 22:14:04 +00:00
};
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant array from the given empty Nix list.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkEmptyArray :: gvariant.type -> gvariant
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Example:
# Creating an empty string array
lib.gvariant.mkEmptyArray (lib.gvariant.type.string)
2024-05-01 22:14:04 +00:00
*/
2024-06-30 08:16:52 +00:00
mkEmptyArray =
elemType: mkPrimitive (type.arrayOf elemType) [ ] // { __toString = self: "@${self.type} []"; };
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant variant from the given Nix value. Variants are containers
of different GVariant type.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkVariant :: Any -> gvariant
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Example:
lib.gvariant.mkArray [
(lib.gvariant.mkVariant "a string")
(lib.gvariant.mkVariant (lib.gvariant.mkInt32 1))
]
2024-05-01 22:14:04 +00:00
*/
2024-06-30 08:16:52 +00:00
mkVariant =
elem:
let
gvarElem = mkValue elem;
in
mkPrimitive type.variant gvarElem // { __toString = self: "<${toString self.value}>"; };
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant dictionary entry from the given key and value.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkDictionaryEntry :: String -> Any -> gvariant
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Example:
# A dictionary describing an Epiphanys search provider
[
(lib.gvariant.mkDictionaryEntry "url" (lib.gvariant.mkVariant "https://duckduckgo.com/?q=%s&t=epiphany"))
(lib.gvariant.mkDictionaryEntry "bang" (lib.gvariant.mkVariant "!d"))
(lib.gvariant.mkDictionaryEntry "name" (lib.gvariant.mkVariant "DuckDuckGo"))
]
2024-05-01 22:14:04 +00:00
*/
mkDictionaryEntry =
# The key of the entry
name:
# The value of the entry
value:
let
name' = mkValue name;
value' = mkValue value;
dictionaryType = type.dictionaryEntryOf name'.type value'.type;
in
2024-06-30 08:16:52 +00:00
mkPrimitive dictionaryType { inherit name value; }
// {
2024-05-01 22:14:04 +00:00
__toString = self: "@${self.type} {${name'},${value'}}";
};
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant maybe from the given element type.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkMaybe :: gvariant.type -> Any -> gvariant
2024-05-01 22:14:04 +00:00
*/
2024-06-30 08:16:52 +00:00
mkMaybe =
elemType: elem:
mkPrimitive (type.maybeOf elemType) elem
// {
__toString =
self: if self.value == null then "@${self.type} nothing" else "just ${toString self.value}";
2024-05-01 22:14:04 +00:00
};
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant nothing from the given element type.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkNothing :: gvariant.type -> gvariant
2024-05-01 22:14:04 +00:00
*/
mkNothing = elemType: mkMaybe elemType null;
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant just from the given Nix value.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkJust :: Any -> gvariant
2024-05-01 22:14:04 +00:00
*/
2024-06-30 08:16:52 +00:00
mkJust =
elem:
let
gvarElem = mkValue elem;
in
mkMaybe gvarElem.type gvarElem;
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant tuple from the given Nix list.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkTuple :: [Any] -> gvariant
2024-05-01 22:14:04 +00:00
*/
2024-06-30 08:16:52 +00:00
mkTuple =
elems:
2024-05-01 22:14:04 +00:00
let
gvarElems = map mkValue elems;
tupleType = type.tupleOf (map (e: e.type) gvarElems);
in
2024-06-30 08:16:52 +00:00
mkPrimitive tupleType gvarElems
// {
__toString = self: "@${self.type} (${concatMapStringsSep "," toString self.value})";
2024-05-01 22:14:04 +00:00
};
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant boolean from the given Nix bool value.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkBoolean :: Bool -> gvariant
2024-05-01 22:14:04 +00:00
*/
2024-06-30 08:16:52 +00:00
mkBoolean =
v: mkPrimitive type.boolean v // { __toString = self: if self.value then "true" else "false"; };
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant string from the given Nix string value.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkString :: String -> gvariant
2024-05-01 22:14:04 +00:00
*/
2024-06-30 08:16:52 +00:00
mkString =
v:
let
sanitize =
s:
replaceStrings [ "\n" ] [ "\\n" ] (
escape [
"'"
"\\"
] s
);
in
mkPrimitive type.string v // { __toString = self: "'${sanitize self.value}'"; };
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant object path from the given Nix string value.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkObjectpath :: String -> gvariant
2024-05-01 22:14:04 +00:00
*/
2024-06-30 08:16:52 +00:00
mkObjectpath =
v: mkPrimitive type.string v // { __toString = self: "objectpath '${escape [ "'" ] self.value}'"; };
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant uchar from the given Nix int value.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkUchar :: Int -> gvariant
2024-05-01 22:14:04 +00:00
*/
mkUchar = mkPrimitive type.uchar;
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant int16 from the given Nix int value.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkInt16 :: Int -> gvariant
2024-05-01 22:14:04 +00:00
*/
mkInt16 = mkPrimitive type.int16;
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant uint16 from the given Nix int value.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkUint16 :: Int -> gvariant
2024-05-01 22:14:04 +00:00
*/
mkUint16 = mkPrimitive type.uint16;
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant int32 from the given Nix int value.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkInt32 :: Int -> gvariant
2024-05-01 22:14:04 +00:00
*/
2024-06-30 08:16:52 +00:00
mkInt32 = v: mkPrimitive type.int32 v // { __toString = self: toString self.value; };
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant uint32 from the given Nix int value.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkUint32 :: Int -> gvariant
2024-05-01 22:14:04 +00:00
*/
mkUint32 = mkPrimitive type.uint32;
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant int64 from the given Nix int value.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkInt64 :: Int -> gvariant
2024-05-01 22:14:04 +00:00
*/
mkInt64 = mkPrimitive type.int64;
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant uint64 from the given Nix int value.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkUint64 :: Int -> gvariant
2024-05-01 22:14:04 +00:00
*/
mkUint64 = mkPrimitive type.uint64;
2024-06-30 08:16:52 +00:00
/*
Returns the GVariant double from the given Nix float value.
2024-05-01 22:14:04 +00:00
2024-06-30 08:16:52 +00:00
Type:
mkDouble :: Float -> gvariant
2024-05-01 22:14:04 +00:00
*/
2024-06-30 08:16:52 +00:00
mkDouble = v: mkPrimitive type.double v // { __toString = self: toString self.value; };
2024-05-01 22:14:04 +00:00
}