Rework Std_utils.Option so it works like the OCaml stdlib module

OCaml 4.08 introduces a stdlib Option module which looks a bit like
ours but has a number of differences.  In particular our functions
Option.may and Option.default have no corresponding functions in
stdlib, although there are close enough equivalents.

This change was automated using this command:

$ perl -pi.bak \
  -e 's/Option.may/Option.iter/g; s/Option.default /Option.value ~default:/g' \
  `git ls-files`

Update common module to include:

  commit cffa077323fafcdfcf78e230c022afa891a6b3ff
  Author: Richard W.M. Jones <rjones@redhat.com>
  Date:   Mon Feb 20 12:11:51 2023 +0000

    mlstdutils: Rework the Option module to be compatible with stdlib

(cherry picked from commit 250ee85839)
This commit is contained in:
Richard W.M. Jones
2023-02-20 12:01:21 +00:00
parent 2cfb5420fe
commit 75c30fe750
4 changed files with 17 additions and 17 deletions

2
common

Submodule common updated: 53713420b6...cffa077323

View File

@@ -21,7 +21,7 @@ open Std_utils
open Utils
let rec vfs_type { Mountable.m_device = device } =
Option.default "" (get_blkid_tag device "TYPE")
Option.value ~default:"" (get_blkid_tag device "TYPE")
and get_blkid_tag device tag =
let r, out, err =

View File

@@ -150,25 +150,25 @@ and string_of_root { root_location; inspection_data } =
and string_of_inspection_data data =
let b = Buffer.create 1024 in
let bpf fs = bprintf b fs in
Option.may (fun v -> bpf " type: %s\n" (string_of_os_type v))
Option.iter (fun v -> bpf " type: %s\n" (string_of_os_type v))
data.os_type;
Option.may (fun v -> bpf " distro: %s\n" (string_of_distro v))
Option.iter (fun v -> bpf " distro: %s\n" (string_of_distro v))
data.distro;
Option.may (fun v -> bpf " package_format: %s\n" (string_of_package_format v))
Option.iter (fun v -> bpf " package_format: %s\n" (string_of_package_format v))
data.package_format;
Option.may (fun v -> bpf " package_management: %s\n" (string_of_package_management v))
Option.iter (fun v -> bpf " package_management: %s\n" (string_of_package_management v))
data.package_management;
Option.may (fun v -> bpf " product_name: %s\n" v)
Option.iter (fun v -> bpf " product_name: %s\n" v)
data.product_name;
Option.may (fun v -> bpf " product_variant: %s\n" v)
Option.iter (fun v -> bpf " product_variant: %s\n" v)
data.product_variant;
Option.may (fun (major, minor) -> bpf " version: %d.%d\n" major minor)
Option.iter (fun (major, minor) -> bpf " version: %d.%d\n" major minor)
data.version;
Option.may (fun v -> bpf " arch: %s\n" v)
Option.iter (fun v -> bpf " arch: %s\n" v)
data.arch;
Option.may (fun v -> bpf " hostname: %s\n" v)
Option.iter (fun v -> bpf " hostname: %s\n" v)
data.hostname;
Option.may (fun v -> bpf " build ID: %s\n" v)
Option.iter (fun v -> bpf " build ID: %s\n" v)
data.build_id;
if data.fstab <> [] then (
let v = List.map (
@@ -176,13 +176,13 @@ and string_of_inspection_data data =
) data.fstab in
bpf " fstab: [%s]\n" (String.concat ", " v)
);
Option.may (fun v -> bpf " windows_systemroot: %s\n" v)
Option.iter (fun v -> bpf " windows_systemroot: %s\n" v)
data.windows_systemroot;
Option.may (fun v -> bpf " windows_software_hive: %s\n" v)
Option.iter (fun v -> bpf " windows_software_hive: %s\n" v)
data.windows_software_hive;
Option.may (fun v -> bpf " windows_system_hive: %s\n" v)
Option.iter (fun v -> bpf " windows_system_hive: %s\n" v)
data.windows_system_hive;
Option.may (fun v -> bpf " windows_current_control_set: %s\n" v)
Option.iter (fun v -> bpf " windows_current_control_set: %s\n" v)
data.windows_current_control_set;
if data.drive_mappings <> [] then (
let v =

View File

@@ -61,7 +61,7 @@ let iso_parse_datetime str =
let old_TZ = try Some (getenv "TZ") with Not_found -> None in
putenv "TZ" "UTC";
let r = Int64.of_float (fst (mktime tm)) in
Option.may (putenv "TZ") old_TZ;
Option.iter (putenv "TZ") old_TZ;
(* The final byte is a time zone offset from GMT.
*