about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJan Tojnar <jtojnar@gmail.com>2024-05-01 00:48:27 +0200
committerJan Tojnar <jtojnar@gmail.com>2024-05-02 23:19:08 +0200
commit29521b42cf644cd9713476c703507afa8d7ca187 (patch)
tree06a1df4c50af6ee3347d8599f406df43d3a7261d
parent96e6f1a418702870bfeb3e80ce62b9158fce0084 (diff)
lib/gvariant: Use more specific instructions for int and attrset
-rw-r--r--lib/gvariant.nix59
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/gvariant.nix b/lib/gvariant.nix
index 222267f60d8e..54aa4ea80571 100644
--- a/lib/gvariant.nix
+++ b/lib/gvariant.nix
@@ -53,6 +53,53 @@ rec {
 
   inherit type isGVariant;
 
+  intConstructors = [
+    {
+      name = "mkInt32";
+      type = type.int32;
+      min = -2147483648;
+      max = 2147483647;
+    }
+    {
+      name = "mkUint32";
+      type = type.uint32;
+      min = 0;
+      max = 4294967295;
+    }
+    {
+      name = "mkInt64";
+      type = type.int64;
+      # Nix does not support such large numbers.
+      min = null;
+      max = null;
+    }
+    {
+      name = "mkUint64";
+      type = type.uint64;
+      min = 0;
+      # Nix does not support such large numbers.
+      max = null;
+    }
+    {
+      name = "mkInt16";
+      type = type.int16;
+      min = -32768;
+      max = 32767;
+    }
+    {
+      name = "mkUint16";
+      type = type.uint16;
+      min = 0;
+      max = 65535;
+    }
+    {
+      name = "mkUchar";
+      type = type.uchar;
+      min = 0;
+      max = 255;
+    }
+  ];
+
   /* Returns the GVariant value that most closely matches the given Nix value.
      If no GVariant value can be found unambiguously then error is thrown.
 
@@ -70,6 +117,18 @@ rec {
       mkArray v
     else if isGVariant v then
       v
+    else if builtins.isInt v then
+      let
+        validConstructors = builtins.filter ({ min, max, ... }: (min == null || min <= v) && (max == null || v <= max)) intConstructors;
+      in
+      throw ''
+        The GVariant type for number “${builtins.toString v}” is unclear.
+        Please wrap the value with one of the following, depending on the value type in GSettings schema:
+
+        ${lib.concatMapStringsSep "\n" ({ name, type, ...}: "- `lib.gvariant.${name}` for `${type}`") validConstructors}
+      ''
+    else if builtins.isAttrs v then
+      throw "Cannot construct GVariant value from an attribute set. If you want to construct a dictionary, you will need to create an array containing items constructed with `lib.gvariant.mkDictionaryEntry`."
     else
       throw "The GVariant type of “${builtins.typeOf v}” can't be inferred.";