Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,106 changes: 1,950 additions & 156 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ serde_json = "1.0.114"
[workspace]
members = [
"sunspec-gen",
"sunspec-cli",
"examples/readme",
"examples/model103",
"examples/model712",
Expand Down
43 changes: 43 additions & 0 deletions src/group.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
/// Static metadata for a field inside a SunSpec group.
#[derive(Clone, Copy, Debug)]
pub struct FieldInfo {
/// Stable serialized field name.
pub name: &'static str,
/// Human-readable label.
pub label: &'static str,
/// Human-readable description.
pub description: &'static str,
/// Whether this field is a point or a nested group.
pub kind: FieldKind,
}

/// Static metadata for a SunSpec group.
#[derive(Clone, Copy, Debug)]
pub struct GroupInfo {
/// Stable group name from the source JSON.
pub name: &'static str,
/// Human-readable group label.
pub label: &'static str,
/// Human-readable group description.
pub description: &'static str,
/// Group fields in source order.
pub fields: &'static [FieldInfo],
}

/// The kind of a generated group field.
#[derive(Clone, Copy, Debug)]
pub enum FieldKind {
/// A regular point field.
Point,
/// A nested single group.
Group(fn() -> &'static GroupInfo),
/// A repeating group.
RepeatingGroup(fn() -> &'static GroupInfo),
}

/// Every group and model implements this trait.
pub trait Group: Sized {
/// Group length (without nested and repeating groups)
const LEN: u16;
}

/// Runtime type information for generated groups and models.
pub trait GroupMeta: Group {
/// Static metadata for this group.
fn group_info() -> &'static GroupInfo;
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
)]

pub use constants::{DEFAULT_DISCOVERY_ADDRESSES, SUNS_IDENTIFIER};
pub use group::Group;
pub use model::{InvalidPointData, Model, ModelAddr, ParseError};
pub use group::{FieldInfo, FieldKind, Group, GroupInfo, GroupMeta};
pub use model::{DiscoveredModel, InvalidPointData, Model, ModelAddr, ModelInfo, ParseError};
pub use models::Models;
pub use point::Point;
pub use value::{DecodeError, EnumValue, FixedSize, Value};
Expand Down
39 changes: 39 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@ use thiserror::Error;

use crate::{DecodeError, Group, Models};

/// Static metadata for a generated SunSpec model type.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ModelInfo {
/// SunSpec model ID.
pub id: u16,
/// Stable model name from the source JSON.
pub name: &'static str,
/// Human-readable model label.
pub label: &'static str,
/// Human-readable model description.
pub description: &'static str,
}

/// A discovered model together with its static metadata.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DiscoveredModel {
/// Static metadata for the discovered model.
pub info: ModelInfo,
/// The discovered address of this model.
pub addr: u16,
/// The discovered length of this model.
pub len: u16,
}

/// Model data that decoded successfully but failed semantic validation.
#[derive(Debug, Error)]
#[error("Invalid point data")]
Expand All @@ -28,10 +52,25 @@ pub enum ParseError<T: Debug> {
pub trait Model: Sized + Group + Debug {
/// Model ID
const ID: u16;
/// Stable model name from the source JSON.
const NAME: &'static str;
/// Human-readable model label.
const LABEL: &'static str;
/// Human-readable model description.
const DESCRIPTION: &'static str;
/// Get model address from discovered models struct
fn addr(models: &Models) -> ModelAddr<Self>;
/// Parse model data from a given u16 slice
fn parse(data: &[u16]) -> Result<Self, ParseError<Self>>;
/// Static metadata for this model type.
fn info() -> ModelInfo {
ModelInfo {
id: Self::ID,
name: Self::NAME,
label: Self::LABEL,
description: Self::DESCRIPTION,
}
}
}

/// This structure is used to store the address of
Expand Down
Loading
Loading