# 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' => config_path, 'bin' => ( t.when( 'locd' ) | config_path ), 'cli' => I8[ 'log' => I8[ 'application' => t.non_empty_str, 'level' => NRSER::Log::Types.level?, 'dest' => ( NRSER::Log::Types.stdio | config_path ), ], 'bash_comp' => I8[ 'log' => I8[ 'level' => NRSER::Log::Types.level, 'dest' => config_path, ], ] ] ] 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 # -------------------------------------------------------------------------- def_factory :package_path do | name: 'PackagePath', from_s: ->( string ) { if string.start_with? '//' Locd::ROOT.join string[2..-1] end }, **options | t.path & t.where( name: name, from_s: from_s, **options) { |path| begin false == path. to_pn. relative_path_from( Locd::ROOT ). start_with?( '..' + File::SEPARATOR ) rescue StandardError => error false end } end # def_factory :package_path def_factory :config_path do |name: 'ConfigPath', **options| t.or \ package_path, t.abs_path, name: name, **options end # def_factory :config_path # @!endgroup Type Factory Class Methods # ********************************** end # module Types # /Namespace # ======================================================================= end # class Config end # module Locd