module Map = MoreLabels.Map.Make (String) type event = Icalendar.event list (* We use a list of events here because there can be multiple events with the same UID, and we want to preserve all of them. This is important for handling cases where there are multiple events with the same UID but different properties (e.g., due to updates or recurring events or cancellations). *) let ical2rem ical_file = let ic = open_in ical_file in let n = in_channel_length ic in let s = Bytes.create n in really_input ic s 0 n; close_in ic; let cal_or_error = Icalendar.parse (Bytes.unsafe_to_string s) in match cal_or_error with | Error e -> prerr_endline ("Error parsing iCalendar file: " ^ e) | Ok (_, components) -> begin let events_map : event Map.t = ListLabels.fold_left ~init:Map.empty components ~f:(fun acc comp -> match comp with | `Event ev -> let uid = Utils.get_uid ev in let event_list = Map.find_opt uid acc |> Option.value ~default:[] in Map.add ~key:uid ~data:(ev :: event_list) acc | _ -> acc (* Ignore non-event components *)) in (* Now revert all the lists *) let events_map = Map.map ~f:List.rev events_map in (* let () = *) (* Map.iter *) (* ~f:(fun ~key ~data -> *) (* let uid = key in *) (* let evs = data in *) (* Printf.printf "󰧓 ⇒ UID: %s\n" uid; *) (* List.iter (fun ev -> Printf.printf "%s\n" (Icalendar.show_component (`Event ev))) evs; *) (* Printf.printf "\n\n") *) (* events_map *) (* in *) Printf.printf "Events: %d\n\n" (Map.cardinal events_map); let events = List.filter_map (function | `Event ev -> Some ev | _ -> None) components in let _reminders = List.map EventTransformer.remind_of_event events in () end let () = if !Sys.interactive then () else exit (CommandLine.main ical2rem)