refactor: modules/data/feeds: fewer file-level inherits

This commit is contained in:
2024-09-28 09:55:12 +00:00
parent 09cd3ec2a5
commit aa7e1dfd33

View File

@@ -1,18 +1,13 @@
{ lib, sane-lib, ... }: { lib, sane-lib, ... }:
let let
inherit (builtins) concatLists concatStringsSep foldl' fromJSON map readDir readFile;
inherit (lib) hasSuffix listToAttrs mapAttrsToList removeSuffix splitString;
inherit (sane-lib) enumerateFilePaths;
# given a path to a .json file relative to sources, construct the best feed object we can. # given a path to a .json file relative to sources, construct the best feed object we can.
# the .json file could be empty, in which case we make assumptions about the feed based # the .json file could be empty, in which case we make assumptions about the feed based
# on its fs path. # on its fs path.
# Type: feedFromSourcePath :: String -> { name = String; value = feed; } # Type: feedFromSourcePath :: String -> { name = String; value = feed; }
feedFromSourcePath = json-path: feedFromSourcePath = json-path:
assert hasSuffix "/default.json" json-path; assert lib.hasSuffix "/default.json" json-path;
let let
canonical-name = removeSuffix "/default.json" json-path; canonical-name = lib.removeSuffix "/default.json" json-path;
default-url = "https://${canonical-name}"; default-url = "https://${canonical-name}";
feed-details = { url = default-url; } // (tryImportJson (./sources/${json-path})); feed-details = { url = default-url; } // (tryImportJson (./sources/${json-path}));
in { name = canonical-name; value = mkFeed feed-details; }; in { name = canonical-name; value = mkFeed feed-details; };
@@ -25,13 +20,13 @@ let
# or {} if the path is empty. # or {} if the path is empty.
tryImportJson = path: tryImportJson = path:
let let
as-str = readFile path; as-str = builtins.readFile path;
in in
if as-str == "" then if as-str == "" then
{} {}
else else
fromJSON as-str; builtins.fromJSON as-str;
sources = enumerateFilePaths ./sources; sources = sane-lib.enumerateFilePaths ./sources;
in in
listToAttrs (map feedFromSourcePath sources) lib.listToAttrs (builtins.map feedFromSourcePath sources)