Skip to content

Factor out generic utilities for loading recursively nested collections from disk#2688

Open
byorgey wants to merge 8 commits intomainfrom
refactor/byorgey/collection
Open

Factor out generic utilities for loading recursively nested collections from disk#2688
byorgey wants to merge 8 commits intomainfrom
refactor/byorgey/collection

Conversation

@byorgey
Copy link
Member

@byorgey byorgey commented Feb 17, 2026

Towards #2504 . As soon as I started working on #2504 I quickly realized that I wanted to load a whole tree of documentation from disk, which was going to involve a lot of code very similar to the code we already use to load the collection of scenarios. In fact #2504 even mentions possibly reusing the idea of having an 00-ORDER.txt file to specify the ordering of documentation pages within a folder.

So, as a first step, this PR factors out the generic recursively-loading-stuff-from-disk part from the scenario-specific parts. In particular:

  • Move some OrderedMap utilities into their own module
  • Change the ScenarioCollection and ScenarioItem types into generic Collection and CollectionItem types
  • Create loadCollection and loadCollectionItem functions which implement the generic logic (including reading 00-ORDER.txt files), parameterized by a CollectionConfig record which specifies how to load individual items, and how to decide which items to load and which to ignore.
  • Obviously we may decide to extend the CollectionConfig record in the future should we decide that additional configuration options are needed.
  • Collection loading stuff is in Swarm.ResourceLoading.Collection and re-exported from Swarm.ResourceLoading.

@byorgey byorgey requested review from kostmo and xsebek February 17, 2026 20:50
@byorgey
Copy link
Member Author

byorgey commented Mar 6, 2026

@xsebek would you have time to look at this? If not, no worries!

Copy link
Member

@xsebek xsebek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! I think it's actually easier to read once it is more generic. 👍

Comment on lines +162 to +163
-- Load a collection with items in no particular order, and
-- optionally warn that the ORDER file is missing.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this an alphabetical order based on file name?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, in practice it seems to be alphabetical, but really it depends on the order in which files are returned by listDirectory. The documentation for listDirectory does not make any guarantees about the order of the resulting list: https://hackage-content.haskell.org/package/directory-1.3.10.1/docs/System-Directory.html#v:listDirectory I presume it just depends on the particular order of files returned by some OS call.

I suppose we could apply an explicit sort in order to guarantee alphabetical order. @xsebek do you think that would be worthwhile?

m (CollectionItem a)
loadCollectionItem cfg path = do
isDir <- sendIO $ doesDirectoryExist path
let collectionName = into @Text . dropWhile isSpace . takeBaseName $ path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is dropWhile isSpace doing here? I would expect a map trim nonEmptyLines in readOrderFile would better take care of leading and trailing whitespace.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I... am not sure. It would be strange if a folder name started with spaces, wouldn't it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants