diff options
author | Alban Gruin | 2020-09-11 23:39:47 +0200 |
---|---|---|
committer | Alban Gruin | 2020-09-11 23:39:47 +0200 |
commit | 3dc6cc287227bdfc218e79ea4753b601689eff2a (patch) | |
tree | a46ed8f09d8c4145cf327099d5325bc56d174b51 /src | |
parent | dd909afda419f39f16120fbfe0e60d847f6f3e2d (diff) |
ics: clean Event module, conversion to string
Signed-off-by: Alban Gruin <alban at pa1ch dot fr>
Diffstat (limited to 'src')
-rw-r--r-- | src/course.ml | 4 | ||||
-rw-r--r-- | src/course.mli | 2 | ||||
-rw-r--r-- | src/ics.ml | 49 | ||||
-rw-r--r-- | src/ics.mli | 19 | ||||
-rw-r--r-- | src/ucs.ml | 8 |
5 files changed, 61 insertions, 21 deletions
diff --git a/src/course.ml b/src/course.ml index 3a0d1fc..fdabb50 100644 --- a/src/course.ml +++ b/src/course.ml @@ -68,7 +68,7 @@ let encoding = (fun ((_id, start, stop, _allDay, description, (), (), (), (), category), (_sites, _modules, (), (), (), (), ())) -> let location, summary = location_and_summary description category in - Ics.{start; stop; summary; category; location}) + Ics.Event.make start stop summary location) (merge_objs (obj10 (req "id" string) @@ -95,4 +95,4 @@ let decode s = match s with | "" -> `O [] | s -> Ezjsonm.from_string s in - J.destruct (J.list encoding) toks + Ics.make @@ J.destruct (J.list encoding) toks diff --git a/src/course.mli b/src/course.mli index a91954d..054444e 100644 --- a/src/course.mli +++ b/src/course.mli @@ -15,4 +15,4 @@ * along with ucs. If not, see <http://www.gnu.org/licenses/>. *) -val decode : string -> Ics.event list +val decode : string -> Ics.t @@ -17,10 +17,45 @@ open CalendarLib -type event = { - start: Calendar.t; - stop: Calendar.t; - summary: string; - category: string; - location: string - } +let to_date = Printer.Calendar.sprint "%Y%m%dT%H%M%SZ" +let current_date () = to_date @@ Calendar.now () + +module Event = struct + type t = { + start: Calendar.t; + stop: Calendar.t; + summary: string; + location: string; + } + + let make start stop summary location = + {start; stop; summary; location} + + let to_string date event id = + Printf.sprintf "BEGIN:VEVENT\n\ + UID:%s.%d@ucs.pa1ch.fr\n\ + DTSTART:%s\n\ + DTEND:%s\n\ + DTSTAMP:%s\n\ + SUMMARY:%s\n\ + LOCATION:%s\n\ + END:VEVENT\n" + date id (to_date event.start) (to_date event.stop) + date event.summary event.location +end + +type t = Event.t list + +let make (events: t) = events + +let to_string events = + let date = current_date () in + let rec gen_events id str = function + | [] -> str + | event :: l -> + gen_events (id + 1) (str ^ Event.to_string date event id) l in + "BEGIN:VCALENDAR\n\ + VERSION:2.0\n\ + PRODID:-//ucs//\n" + ^ gen_events 0 "" events + ^ "END:VCALENDAR\n" diff --git a/src/ics.mli b/src/ics.mli index 352ce68..219f851 100644 --- a/src/ics.mli +++ b/src/ics.mli @@ -15,10 +15,15 @@ * along with ucs. If not, see <http://www.gnu.org/licenses/>. *) -type event = { - start: CalendarLib.Calendar.t; - stop: CalendarLib.Calendar.t; - summary: string; - category: string; - location: string - } +module Event : sig + type t + + val make : CalendarLib.Calendar.t -> CalendarLib.Calendar.t -> + string -> string -> t + val to_string : string -> t -> int -> string +end + +type t + +val make : Event.t list -> t +val to_string : t -> string @@ -33,7 +33,7 @@ let body = let dump_date = CalendarLib.Printer.Calendar.to_string let () = - let body = Lwt_main.run body in - List.iter (fun Ics.{start; stop; summary; category; location} -> - Printf.printf "%s\n%s\n%s\n%s\n%s\n\n" (dump_date start) (dump_date stop) summary category location) - @@ Course.decode body + Lwt_main.run body + |> Course.decode + |> Ics.to_string + |> print_endline |