1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//! A Rust implementation of the IEC Common Information Model for power systems.
//!
//! `cim-kit` reads, navigates, validates and writes CIM grid models in both profile sets
//! European transmission system operators use: **CGMES 3.0**
//! (IEC TS 61970-600-1/-2:2021) and **CGMES 2.4.15**.
//!
//! Each vintage is a module behind a feature of the same name — [`cgmes3`] (default) and
//! [`cgmes2`] — holding its schema tables, typed views and named constants. Identifiers
//! are per-vintage and cannot be mixed by accident.
//!
//! # What this crate models
//!
//! A CGMES grid model is not one file. It is a *set* of profile files — Equipment (EQ),
//! Steady State Hypothesis (SSH), Topology (TP), State Variables (SV) and others — that
//! describe the **same objects** from different angles. A `SynchronousMachine` gets its
//! ratings from EQ and its dispatch from SSH, under one identifier.
//!
//! [`Dataset`] is built around that fact: loading several files merges each object's
//! attributes rather than producing duplicates, and writing filters attributes back down
//! to a single profile. Which file contributed a value is remembered, so a model set
//! written back out is the file set it came from.
//!
//! Merging is order-independent for everything except a genuine contradiction — two files
//! giving one single-valued attribute two different values. Resolving that means
//! discarding one of them, so it is recorded rather than swallowed:
//! [`Dataset::merge_conflicts`], reported by [`validate()`] as `CIM0018`.
//!
//! # Getting started
//!
//! [`Dataset::open`] is the shortest path in: it reads the vintage out of the document's own
//! namespace declarations, so a directory, a zip archive or a single file loads without the
//! caller knowing whether it is CGMES 3.0 or 2.4.15. Name the schema explicitly — as below —
//! when a program only ever handles one vintage.
//!
// An example has to name a vintage, and a vintage is a feature — so it is written only
// into a build that has the one it names. See `Dataset::view`.
//!
//! # Design
//!
//! Objects are stored **sparsely** — only attributes actually present are kept — because a
//! `Terminal` has around thirty possible attributes and a Steady State Hypothesis file sets
//! exactly one. Typed access comes from generated zero-cost [views](view::TypedView) rather
//! than a struct per class, which keeps profile merging natural and compile times low.
//!
//! Everything model-specific is generated from the official RDFS vocabularies; everything
//! else is written against the [`schema`] interface, so a new CIM vintage is a regeneration
//! rather than a rewrite. Three facts about CGMES shape the I/O layer — attributes are
//! declared far more widely than they are used, one file serves several profiles at once,
//! and the form of a document is a property of the class within the profile. The
//! [guide](https://hupe1980.github.io/cim-kit/docs/concepts/) explains each.
//!
//! # Changing a model, not resending it
//!
//! IEC 61970-552 defines `dm:DifferenceModel` as the incremental form of exchange: the
//! statements to retract, then the statements to assert. [`diff`] computes one from a
//! before and an after — [`Dataset::difference_to`] — and [`Dataset::apply_difference`]
//! applies it. Applying a computed difference to its base reproduces the target exactly;
//! that is a test against the published conformity models, not a claim.
//!
//! # Leaving for the wider RDF world
//!
//! CIM/XML looks like RDF/XML and is not, and it carries **no datatype information at
//! all** — every value is element text, so `1` could be an integer, a float or a string.
//! That is why a CGMES file cannot simply be handed to a triple store or to the SHACL
//! shapes ENTSO-E publishes beside the RDFS.
//!
//! [`rdf`] closes that: it writes the model as ordinary N-Triples or Turtle with
//! `urn:uuid:` subjects and every literal typed from the profile. The result loads in any
//! RDF toolchain and validates against ENTSO-E's shapes as it stands.
//!
//! # Conformance
//!
//! * Serialization follows IEC 61970-552: `rdf:ID`/`rdf:about` identity, `md:FullModel`
//! headers, `rdf:parseType="Resource"` compounds, `dm:` difference models. Both reading
//! and writing are covered, and a model set re-exports as the files it came from.
//! * Every document written is well-formed XML, checked over the whole published corpus
//! with namespace resolution, duplicate-attribute detection and XML 1.0's character
//! range — a tolerant reader accepting its own output proves nothing about anyone else's
//! parser. Two constraints of the output syntax that the object model cannot express are
//! enforced at the writer and reported by [`validate()`]: a value must hold only
//! characters XML can represent (`CIM0019`), and an identifier must have an `rdf:ID` or
//! `rdf:about` form (`CIM0020`). See [`xml`].
//! * [`validate()`] performs the structural checks the RDFS vocabulary justifies:
//! cardinality, value datatypes, fixed values, reference targets, profile membership,
//! identifier form. The datatype check is not redundant with parsing — CIM/XML exchanges
//! no type information at all, so a value's type is only decidable against the profile.
//! Semantic rules ENTSO-E publishes as SHACL shapes are out of scope: export with
//! [`rdf`] and run them with a SHACL engine.
//! * Reading is tolerant by default ([`ReadOptions::lenient`]), because published models
//! carry vendor extensions and identifier deviations. Nothing is silently dropped: every
//! deviation becomes a [`Diagnostic`] with a stable rule code (`CIM0001`–`CIM0022`) and,
//! from parsing, a byte offset that [`line_and_column`] turns into a line. The report
//! grows with how broken the input is, so [`ReadOptions::max_diagnostics`] bounds it and
//! the reader says when it stopped.
//! * Identifiers are compared by the UUID they denote, not by how a producer spelled it.
//! Published CGMES 2.4.15 files write `rdf:ID="_1fa19c281c8f4e1eaad9e1cab70f923e"`, and
//! references as absolute IRIs; read as opaque text, such an object loses its
//! `urn:uuid:` name in RDF and splits in two against a file that spells the same UUID
//! conventionally. The spelling is remembered so re-export reproduces the document; the
//! identity does not depend on it. See [`MridForm`].
//! * Numbers are compared by value and written as they arrived. Published models write
//! `2.62637E-05` and `250.000000`, and re-rendering an `f64` returns neither; [`Real`]
//! carries the spelling beside the number, so a re-export is the file that was read and
//! a difference never reports a reformatting as a change.
//!
//! # A command line
//!
//! The `cli` feature builds `cim`, a tool over this same API: `cim info`, `validate`,
//! `export`, `rdf`, `diff`, `apply` and `schema` over a model set given as files, archives
//! or directories. `cargo install cim-kit --features cli`.
//!
//! # Further reading
//!
//! This page is orientation and the rest of these docs are the reference. The **guide** —
//! CGMES background, one page per task, and the full rule catalogue — is at
//! <https://hupe1980.github.io/cim-kit/>.
/// The README's examples are compiled as doctests so the front page cannot drift from
/// the API.
///
/// Gated on `cgmes3` for the same reason the site pages are: the examples name a vintage,
/// because an example that named none would not be an example of anything. Without the
/// gate `cargo test --no-default-features --features cgmes2` failed on the front page
/// rather than on the code — a failure CI never saw, since the feature matrix runs
/// `clippy --all-targets`, which does not compile doctests.
;
/// The documentation site's examples, compiled for the same reason.
///
/// A guide whose code does not build is worse than no guide, and a guide is exactly the
/// thing nobody re-runs. `site/` is repository machinery rather than published crate
/// content, so this is gated on `doctest`: `cfg` stripping happens before macro expansion,
/// which means an ordinary build — including the one `cargo package` runs to verify the
/// tarball — never looks for the files.