MQTT 3.1 and 5 in OCaml using Eio
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

sync

+93 -9
+9 -7
lib/cmd/mqtte_cmd.ml
··· 308 308 309 309 type parsed = { mqtt : t; xdg : Xdge.t; xdg_config : Xdge.Cmd.t } 310 310 311 - let term ~app_name ~fs () = 312 - (* Create XDG context to read config file defaults at term construction time. 313 - The xdg_term from Xdge.Cmd will provide runtime XDG paths with CLI override. *) 314 - let xdg = Xdge.create fs app_name in 315 - let file_config = 316 - match Config_file.load xdg with Some c -> c | None -> Config_file.empty 317 - in 311 + let term_with_config ~app_name ~fs ~config:file_config () = 318 312 (* Xdge.Cmd.term provides XDG paths with command-line override support *) 319 313 let xdg_term = Xdge.Cmd.term app_name fs ~dirs:[ `Config ] () in 320 314 let make (xdg, xdg_config) connection config pool_config = ··· 325 319 $ connection_term ~file_config 326 320 $ config_term ~file_config 327 321 $ pool_config_term ~file_config) 322 + 323 + let term ~app_name ~fs () = 324 + (* Create XDG context to read config file defaults at term construction time. *) 325 + let xdg = Xdge.create fs app_name in 326 + let file_config = 327 + match Config_file.load xdg with Some c -> c | None -> Config_file.empty 328 + in 329 + term_with_config ~app_name ~fs ~config:file_config () 328 330 329 331 (** {1 Connection Pool Helpers} *) 330 332
+84 -2
lib/cmd/mqtte_cmd.mli
··· 129 129 (** {1 Configuration File} *) 130 130 131 131 module Config_file : sig 132 - type t 133 - (** Parsed TOML configuration. *) 132 + (** {2 MQTT Configuration} *) 133 + 134 + type mqtt_config = { 135 + host : string option; 136 + port : int option; 137 + tls : bool option; 138 + insecure : bool option; 139 + client_id : string option; 140 + clean_session : bool option; 141 + keep_alive : int option; 142 + username : string option; 143 + password : string option; 144 + protocol_version : string option; 145 + } 146 + (** MQTT connection settings parsed from config file. All fields optional. *) 147 + 148 + val empty_mqtt_config : mqtt_config 149 + (** Empty MQTT config with all fields set to [None]. *) 150 + 151 + val mqtt_codec : mqtt_config Tomlt.t 152 + (** Tomlt codec for the [[mqtt]] section. Use this to compose with your 153 + application's config codec. *) 154 + 155 + (** {2 Pool Configuration} *) 156 + 157 + type pool_config = { 158 + min_connections : int option; 159 + max_connections : int option; 160 + idle_timeout : float option; 161 + } 162 + (** Connection pool settings parsed from config file. *) 163 + 164 + val empty_pool_config : pool_config 165 + (** Empty pool config with all fields set to [None]. *) 166 + 167 + val pool_codec : pool_config Tomlt.t 168 + (** Tomlt codec for the [[pool]] section. *) 169 + 170 + (** {2 Combined Configuration} *) 171 + 172 + type t = { mqtt : mqtt_config; pool : pool_config } 173 + (** Combined configuration from both sections. *) 174 + 175 + val empty : t 176 + (** Empty configuration. *) 177 + 178 + val codec : t Tomlt.t 179 + (** Tomlt codec for the full config file (both [[mqtt]] and [[pool]] sections). 180 + Use this when loading a standalone config.toml, or use {!mqtt_codec} and 181 + {!pool_codec} separately to compose with application-specific sections. *) 134 182 135 183 val load : Xdge.t -> t option 136 184 (** [load xdg] attempts to load [config.toml] from the XDG config directory. ··· 140 188 (** [load_from_path path] loads configuration from a specific file path. 141 189 Returns [None] if the file doesn't exist or cannot be parsed. *) 142 190 end 191 + 192 + val term_with_config : 193 + app_name:string -> 194 + fs:Eio.Fs.dir_ty Eio.Path.t -> 195 + config:Config_file.t -> 196 + unit -> 197 + parsed Cmdliner.Term.t 198 + (** [term_with_config ~app_name ~fs ~config ()] creates a Cmdliner term using 199 + the provided pre-loaded configuration. Use this when you have a custom 200 + config file format that includes the [[mqtt]] and [[pool]] sections. 201 + 202 + Example with composed config: 203 + {[ 204 + (* In your app's config module *) 205 + type t = { 206 + app_setting : string; 207 + mqtt : Mqtte_cmd.Config_file.mqtt_config; 208 + pool : Mqtte_cmd.Config_file.pool_config; 209 + } 210 + 211 + let codec = Tomlt.Table.( 212 + obj (fun app_setting mqtt pool -> { app_setting; mqtt; pool }) 213 + |> mem "app_setting" Tomlt.string ~enc:(fun c -> c.app_setting) 214 + |> opt_mem "mqtt" Mqtte_cmd.Config_file.mqtt_codec 215 + ~enc:(fun c -> Some c.mqtt) 216 + |> opt_mem "pool" Mqtte_cmd.Config_file.pool_codec 217 + ~enc:(fun c -> Some c.pool) 218 + |> finish 219 + ) 220 + 221 + (* When creating the term *) 222 + let mqtt_config = Mqtte_cmd.Config_file.{ mqtt = config.mqtt; pool = config.pool } in 223 + Mqtte_cmd.term_with_config ~app_name ~fs ~config:mqtt_config () 224 + ]} *) 143 225 144 226 (** {1 Man Page Documentation} 145 227