Skip to main content

iec61850_kit/common/
edition.rs

1/// The IEC 61850 edition a server implements.
2///
3/// Edition is a property of a server (it is what its SCL file declares), never of an
4/// association. It drives attribute sets (RCB `ResvTms`/`Owner`), object-reference length
5/// limits, the GOOSE `test`/`simulation` naming and the enumerations that grew between
6/// editions.
7#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
8#[non_exhaustive]
9pub enum Edition {
10    /// IEC 61850 Edition 1 (2003/2004).
11    Ed1,
12    /// IEC 61850 Edition 2 (2009–2011).
13    Ed2,
14    /// IEC 61850 Edition 2.1 (2010/2011 + AMD1:2020) — the default.
15    #[default]
16    Ed2_1,
17}
18
19impl Edition {
20    /// Maximum length of an object reference (`VisString65` in Ed1, `VisString129` from Ed2).
21    pub const fn max_object_reference_len(self) -> usize {
22        match self {
23            Edition::Ed1 => 65,
24            Edition::Ed2 | Edition::Ed2_1 => 129,
25        }
26    }
27
28    /// The SCL schema namespace revision this edition corresponds to.
29    pub const fn scl_version(self) -> &'static str {
30        match self {
31            Edition::Ed1 => "2003",
32            Edition::Ed2 => "2007B",
33            Edition::Ed2_1 => "2007B4",
34        }
35    }
36
37    /// The edition an SCL file declares, from its `version`+`revision`+`release` string.
38    ///
39    /// This is the whole of "edition is a property of the server, and the server's edition is
40    /// what its file says": `2003` is Edition 1, `2007A`/`2007B` through release 3 is
41    /// Edition 2, and `2007B4` (IEC 61850-6:2009+AMD1) or anything later — `2007C`, the
42    /// Ed 2.2 schema — is Edition 2.1. An unrecognised string is read as the newest rather
43    /// than the oldest: a file from a schema this crate has not seen is more likely to be
44    /// ahead of it than behind, and reading a modern file as Edition 1 would silently drop
45    /// `ResvTms` and `Owner` from every report control block.
46    pub fn from_scl_version(version: &str) -> Edition {
47        let v = version.trim();
48        if v.starts_with("2003") {
49            return Edition::Ed1;
50        }
51        if let Some(rest) = v.strip_prefix("2007") {
52            // `2007B4` and up are Ed 2.1; `2007A`, `2007B`, `2007B1`..`2007B3` are Ed 2.
53            let release: u32 = rest.trim_start_matches(char::is_alphabetic).parse().unwrap_or(0);
54            let revision = rest.chars().next().unwrap_or('B');
55            return if revision >= 'C' || (revision == 'B' && release >= 4) { Edition::Ed2_1 } else { Edition::Ed2 };
56        }
57        Edition::Ed2_1
58    }
59
60    /// Whether a report control block has `ResvTms` (buffered) and `Owner`.
61    ///
62    /// Both arrived with Edition 2 (IEC 61850-7-2 §17.2). An Edition 1 server that publishes
63    /// them is claiming a reservation service it does not have, and a client that reads the
64    /// block positionally then reads every field after them at the wrong offset.
65    pub const fn has_rcb_reservation(self) -> bool {
66        !matches!(self, Edition::Ed1)
67    }
68}