Generate srcset images for a variety of resolutions from OCaml
at main 72 lines 2.3 kB view raw
1(** Image entry management for responsive image generation. 2 3 This module provides types and functions for managing image entries 4 that contain metadata about responsive images including their dimensions 5 and size variants. *) 6 7(** {1 Types} *) 8 9(** String map for storing image variants keyed by filename. *) 10module MS : Map.S with type key = string 11 12(** An image entry representing a source image and its generated variants. 13 14 Each entry tracks: 15 - The output filename ([name]) 16 - A URL-safe identifier ([slug]) 17 - The original source path ([origin]) 18 - Image dimensions as [(width, height)] 19 - A map of variant filenames to their dimensions *) 20type t = { 21 name : string; 22 slug : string; 23 origin : string; 24 dims : int * int; 25 variants : (int * int) MS.t; 26} 27 28(** {1 Constructors} *) 29 30val v : string -> string -> string -> (int * int) MS.t -> int * int -> t 31(** [v name slug origin variants dims] creates a new image entry. 32 33 @param name The output filename (e.g., ["photo.webp"]) 34 @param slug A URL-safe identifier derived from the filename 35 @param origin The original source file path 36 @param variants Map of variant filenames to their [(width, height)] dimensions 37 @param dims The base image dimensions as [(width, height)] *) 38 39(** {1 Accessors} *) 40 41val name : t -> string 42(** [name entry] returns the output filename. *) 43 44val slug : t -> string 45(** [slug entry] returns the URL-safe identifier. *) 46 47val origin : t -> string 48(** [origin entry] returns the original source file path. *) 49 50val dims : t -> int * int 51(** [dims entry] returns the base image dimensions as [(width, height)]. *) 52 53val variants : t -> (int * int) MS.t 54(** [variants entry] returns the map of variant filenames to dimensions. *) 55 56(** {1 JSON Serialization} *) 57 58val json_t : t Jsont.t 59(** JSON codec for a single image entry. *) 60 61val list : t list Jsont.t 62(** JSON codec for a list of image entries. *) 63 64val list_to_json : t list -> (string, string) result 65(** [list_to_json entries] serializes a list of entries to a JSON string. 66 67 Returns [Ok json_string] on success, or [Error message] if encoding fails. *) 68 69val list_of_json : string -> (t list, string) result 70(** [list_of_json json_string] parses a JSON string into a list of entries. 71 72 Returns [Ok entries] on success, or [Error message] if parsing fails. *)