# encoding: UTF-8 # frozen_string_literal: true # Requirements # ======================================================================== # Stdlib # ------------------------------------------------------------------------ # Deps # ------------------------------------------------------------------------ require 'nrser/labs/i8' # Using log system types, which are not loaded along with it to avoid # dependencies. require 'nrser/log/types' # Project / Package # ------------------------------------------------------------------------ # Refinements # ======================================================================= require 'nrser/refinements/types' using NRSER::Types # Namespace # ======================================================================= module Locd class Config # Definitions # ======================================================================= module Types # Mixins # ======================================================================== # Get {NRSER::Types::Factory.def_factory} extend t::Factory # Class Methods # ======================================================================== # A tree representing config key paths to the types they need to be. # # Computed on first call and cached after that as it needs the type # factories below. # # @return [Hamster::Hash] # def self.by_key # Got sick of writing "Hamster::Hash[...]"... "I8" <=> "Immutable" @by_key ||= I8[ 'home' => self.ConfigPath, 'bin' => ( t.When( 'locd' ) | self.ConfigPath ), 'log' => I8[ 'dir' => self.ConfigPath, ], 'tmp' => I8[ 'dir' => self.ConfigPath, ], 'cli' => I8[ 'log' => I8[ 'application' => t.non_empty_str, 'level' => NRSER::Log::Types.level?, 'dest' => ( NRSER::Log::Types.stdio | self.ConfigPath ), ], 'bash_comp' => I8[ 'log' => I8[ 'level' => NRSER::Log::Types.level, 'dest' => self.ConfigPath, ], ] ] ] end # Dig in to {BY_KEY} and see if it has a type for a `key`. # # @param [Array] # # @return [NRSER::Types::Type?] # def self.for_key *key by_key.dig *Locd::Config.key_path_for( *key ) end # @!group Type Factory Class Methods # -------------------------------------------------------------------------- #@!method self.PackagePath **options # @todo Document PackagePath type factory. # # @param [Hash] options # Passed to {Type#initialize}. # # @return [Type] # def_type :PackagePath, from_s: ->( s ) { Locd::ROOT.join( s[2..-1] ) if s.start_with? '//' }, &->( **options ) do t.Intersection \ t.Path, t.Where { |path| begin false == path. to_pn. relative_path_from( Locd::ROOT ). start_with?( '..' + File::SEPARATOR ) rescue StandardError => error false end }, **options end # .PackagePath #@!method self.ConfigPath **options # A path we accept in the config. One of: # # 1. A {.PackagePath} that starts with `//...`, denoting it's relative to # the package root. # # 2. A {NRSER::Types.HomePath} that starts with `~`, meaning it's relative # to a user's home directory. # # 3. An {NRSER::Types.AbsPath} # # @param [Hash] options # Passed to {Type#initialize}. # # @return [Type] # def_type :ConfigPath, &->( **options ) do t.Union \ self.PackagePath, t.HomePath, t.AbsPath, **options end # .ConfigPath # @!endgroup Type Factory Class Methods # ********************************** end # module Types # /Namespace # ======================================================================= end # class Config end # module Locd