1 # Wrappers around file system primitives that take a 'resources' object and
  2 # are thus easier to test.
  3 #
  4 # - start-reading - asynchronously open a file, returning a channel source for
  5 #   receiving the results
  6 # - start-writing - asynchronously open a file, returning a channel sink for
  7 #   the data to write
  8 # - slurp - synchronously read from a file
  9 # - dump - synchronously write to a file
 10 
 11 container resources [
 12   lock:bool
 13   data:&:@:resource
 14 ]
 15 
 16 container resource [
 17   name:text
 18   contents:text
 19 ]
 20 
 21 def start-reading resources:&:resources, filename:text -> contents:&:source:char, error?:bool [
 22   local-scope
 23   load-inputs
 24   error? <- copy false
 25   {
 26     break-unless resources
 27     # fake file system
 28     contents, error? <- start-reading-from-fake-resource resources, filename
 29     return
 30   }
 31   # real file system
 32   file:num <- $open-file-for-reading filename
 33   return-unless file, null/no-contents, true/error
 34   contents:&:source:char, sink:&:sink:char <- new-channel 30
 35   start-running receive-from-file file,