|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// DesignFormat.res - JSON schema for stapeln designs |
| 3 | + |
| 4 | +open Model |
| 5 | + |
| 6 | +// Design file format version |
| 7 | +let currentVersion = "1.0" |
| 8 | + |
| 9 | +type designMetadata = { |
| 10 | + version: string, |
| 11 | + created: string, |
| 12 | + author: string, |
| 13 | + description: string, |
| 14 | +} |
| 15 | + |
| 16 | +type designFile = { |
| 17 | + metadata: designMetadata, |
| 18 | + canvas: model, |
| 19 | +} |
| 20 | + |
| 21 | +// Serialize component to JSON |
| 22 | +let componentToJson = (comp: component): Js.Json.t => { |
| 23 | + Js.Dict.fromArray([ |
| 24 | + ("id", Js.Json.string(comp.id)), |
| 25 | + ("type", Js.Json.string(componentTypeToString(comp.componentType))), |
| 26 | + ("position", Js.Dict.fromArray([ |
| 27 | + ("x", Js.Json.number(comp.position.x)), |
| 28 | + ("y", Js.Json.number(comp.position.y)), |
| 29 | + ])->Js.Json.object_), |
| 30 | + ("config", comp.config->Js.Json.object_), |
| 31 | + ])->Js.Json.object_ |
| 32 | +} |
| 33 | + |
| 34 | +// Deserialize component from JSON |
| 35 | +let componentFromJson = (json: Js.Json.t): option<component> => { |
| 36 | + open Js.Json |
| 37 | + switch classify(json) { |
| 38 | + | JSONObject(obj) => { |
| 39 | + let id = Js.Dict.get(obj, "id")->Belt.Option.flatMap(decodeString) |
| 40 | + let typeStr = Js.Dict.get(obj, "type")->Belt.Option.flatMap(decodeString) |
| 41 | + let position = Js.Dict.get(obj, "position")->Belt.Option.flatMap(posJson => { |
| 42 | + switch classify(posJson) { |
| 43 | + | JSONObject(posObj) => { |
| 44 | + let x = Js.Dict.get(posObj, "x")->Belt.Option.flatMap(decodeNumber) |
| 45 | + let y = Js.Dict.get(posObj, "y")->Belt.Option.flatMap(decodeNumber) |
| 46 | + switch (x, y) { |
| 47 | + | (Some(x), Some(y)) => Some({x: x, y: y}) |
| 48 | + | _ => None |
| 49 | + } |
| 50 | + } |
| 51 | + | _ => None |
| 52 | + } |
| 53 | + }) |
| 54 | + let config = Js.Dict.get(obj, "config")->Belt.Option.flatMap(cfg => { |
| 55 | + switch classify(cfg) { |
| 56 | + | JSONObject(dict) => Some(dict) |
| 57 | + | _ => None |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + // Convert type string to componentType |
| 62 | + let componentType = switch typeStr { |
| 63 | + | Some("Cerro Torre") => Some(CerroTorre) |
| 64 | + | Some("Lago Grey") => Some(LagoGrey) |
| 65 | + | Some("Svalinn") => Some(Svalinn) |
| 66 | + | Some("selur") => Some(Selur) |
| 67 | + | Some("Vörðr") => Some(Vordr) |
| 68 | + | Some("Podman") => Some(Podman) |
| 69 | + | Some("Docker") => Some(Docker) |
| 70 | + | Some("nerdctl") => Some(Nerdctl) |
| 71 | + | Some("Volume") => Some(Volume) |
| 72 | + | Some("Network") => Some(Network) |
| 73 | + | _ => None |
| 74 | + } |
| 75 | + |
| 76 | + switch (id, componentType, position, config) { |
| 77 | + | (Some(id), Some(ct), Some(pos), Some(cfg)) => |
| 78 | + Some({id: id, componentType: ct, position: pos, config: cfg}) |
| 79 | + | _ => None |
| 80 | + } |
| 81 | + } |
| 82 | + | _ => None |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// Serialize connection to JSON |
| 87 | +let connectionToJson = (conn: connection): Js.Json.t => { |
| 88 | + Js.Dict.fromArray([ |
| 89 | + ("id", Js.Json.string(conn.id)), |
| 90 | + ("from", Js.Json.string(conn.from)), |
| 91 | + ("to", Js.Json.string(conn.to)), |
| 92 | + ])->Js.Json.object_ |
| 93 | +} |
| 94 | + |
| 95 | +// Deserialize connection from JSON |
| 96 | +let connectionFromJson = (json: Js.Json.t): option<connection> => { |
| 97 | + open Js.Json |
| 98 | + switch classify(json) { |
| 99 | + | JSONObject(obj) => { |
| 100 | + let id = Js.Dict.get(obj, "id")->Belt.Option.flatMap(decodeString) |
| 101 | + let from = Js.Dict.get(obj, "from")->Belt.Option.flatMap(decodeString) |
| 102 | + let to = Js.Dict.get(obj, "to")->Belt.Option.flatMap(decodeString) |
| 103 | + |
| 104 | + switch (id, from, to) { |
| 105 | + | (Some(id), Some(from), Some(to)) => Some({id: id, from: from, to: to}) |
| 106 | + | _ => None |
| 107 | + } |
| 108 | + } |
| 109 | + | _ => None |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// Serialize model to JSON |
| 114 | +let modelToJson = (model: model): Js.Json.t => { |
| 115 | + Js.Dict.fromArray([ |
| 116 | + ("components", model.components->Array.map(componentToJson)->Js.Json.array), |
| 117 | + ("connections", model.connections->Array.map(connectionToJson)->Js.Json.array), |
| 118 | + ])->Js.Json.object_ |
| 119 | +} |
| 120 | + |
| 121 | +// Deserialize model from JSON |
| 122 | +let modelFromJson = (json: Js.Json.t): option<model> => { |
| 123 | + open Js.Json |
| 124 | + switch classify(json) { |
| 125 | + | JSONObject(obj) => { |
| 126 | + let components = Js.Dict.get(obj, "components")->Belt.Option.flatMap(arr => { |
| 127 | + switch classify(arr) { |
| 128 | + | JSONArray(items) => { |
| 129 | + let parsed = items->Array.map(componentFromJson)->Array.keepMap(x => x) |
| 130 | + Some(parsed) |
| 131 | + } |
| 132 | + | _ => None |
| 133 | + } |
| 134 | + }) |
| 135 | + |
| 136 | + let connections = Js.Dict.get(obj, "connections")->Belt.Option.flatMap(arr => { |
| 137 | + switch classify(arr) { |
| 138 | + | JSONArray(items) => { |
| 139 | + let parsed = items->Array.map(connectionFromJson)->Array.keepMap(x => x) |
| 140 | + Some(parsed) |
| 141 | + } |
| 142 | + | _ => None |
| 143 | + } |
| 144 | + }) |
| 145 | + |
| 146 | + switch (components, connections) { |
| 147 | + | (Some(comps), Some(conns)) => |
| 148 | + Some({ |
| 149 | + ...initialModel, |
| 150 | + components: comps, |
| 151 | + connections: conns, |
| 152 | + }) |
| 153 | + | _ => None |
| 154 | + } |
| 155 | + } |
| 156 | + | _ => None |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +// Serialize full design to JSON string |
| 161 | +let serializeDesign = (model: model, metadata: designMetadata): string => { |
| 162 | + let design = Js.Dict.fromArray([ |
| 163 | + ("version", Js.Json.string(metadata.version)), |
| 164 | + ("metadata", Js.Dict.fromArray([ |
| 165 | + ("created", Js.Json.string(metadata.created)), |
| 166 | + ("author", Js.Json.string(metadata.author)), |
| 167 | + ("description", Js.Json.string(metadata.description)), |
| 168 | + ])->Js.Json.object_), |
| 169 | + ("canvas", modelToJson(model)), |
| 170 | + ]) |
| 171 | + |
| 172 | + Js.Json.stringify(design->Js.Json.object_) |
| 173 | +} |
| 174 | + |
| 175 | +// Deserialize design from JSON string |
| 176 | +let deserializeDesign = (jsonStr: string): Result.t<(designMetadata, model), string> => { |
| 177 | + try { |
| 178 | + let json = Js.Json.parseExn(jsonStr) |
| 179 | + open Js.Json |
| 180 | + |
| 181 | + switch classify(json) { |
| 182 | + | JSONObject(obj) => { |
| 183 | + let version = Js.Dict.get(obj, "version")->Belt.Option.flatMap(decodeString) |
| 184 | + |
| 185 | + let metadata = Js.Dict.get(obj, "metadata")->Belt.Option.flatMap(metaJson => { |
| 186 | + switch classify(metaJson) { |
| 187 | + | JSONObject(metaObj) => { |
| 188 | + let created = Js.Dict.get(metaObj, "created")->Belt.Option.flatMap(decodeString) |
| 189 | + let author = Js.Dict.get(metaObj, "author")->Belt.Option.flatMap(decodeString) |
| 190 | + let description = Js.Dict.get(metaObj, "description")->Belt.Option.flatMap(decodeString) |
| 191 | + |
| 192 | + switch (created, author, description) { |
| 193 | + | (Some(created), Some(author), Some(description)) => |
| 194 | + Some({version: version->Belt.Option.getWithDefault("1.0"), created, author, description}) |
| 195 | + | _ => None |
| 196 | + } |
| 197 | + } |
| 198 | + | _ => None |
| 199 | + } |
| 200 | + }) |
| 201 | + |
| 202 | + let canvas = Js.Dict.get(obj, "canvas")->Belt.Option.flatMap(modelFromJson) |
| 203 | + |
| 204 | + switch (metadata, canvas) { |
| 205 | + | (Some(meta), Some(model)) => Ok((meta, model)) |
| 206 | + | _ => Error("Invalid design file structure") |
| 207 | + } |
| 208 | + } |
| 209 | + | _ => Error("Design file must be a JSON object") |
| 210 | + } |
| 211 | + } catch { |
| 212 | + | Js.Exn.Error(e) => |
| 213 | + Error("JSON parse error: " ++ Js.Exn.message(e)->Belt.Option.getWithDefault("Unknown error")) |
| 214 | + } |
| 215 | +} |
0 commit comments