use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::{Diagnostic, PluginId, Range, TextEdit};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "codegen", derive(schemars::JsonSchema, ts_rs::TS))]
#[serde(
tag = "type",
rename_all = "camelCase",
rename_all_fields = "camelCase"
)]
#[cfg_attr(feature = "codegen", schemars(title = "Modelith host message"))]
pub enum HostMessage {
Activate {
api_version: String,
plugin_id: PluginId,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional, type = "unknown"))]
settings: Option<Value>,
},
DidChange {
version: u32,
text: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional))]
model: Option<ModelSnapshot>,
},
RunCommand { request_id: u32, command: String },
Import {
request_id: u32,
importer: String,
file_name: String,
base64: String,
},
Export {
request_id: u32,
exporter: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional))]
view: Option<String>,
},
StorageResult {
request_id: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional, type = "unknown"))]
value: Option<Value>,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "codegen", derive(schemars::JsonSchema, ts_rs::TS))]
#[serde(
tag = "type",
rename_all = "camelCase",
rename_all_fields = "camelCase"
)]
#[cfg_attr(feature = "codegen", schemars(title = "Modelith plugin message"))]
pub enum PluginMessage {
Ready,
Diagnostics {
version: u32,
items: Vec<Diagnostic>,
},
ProposeEdits {
version: u32,
edits: Vec<TextEdit>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional))]
label: Option<String>,
},
CommandDone { request_id: u32 },
Imported {
request_id: u32,
file_name: String,
text: String,
},
Exported {
request_id: u32,
file_name: String,
media_type: String,
base64: String,
},
Failed { request_id: u32, message: String },
StorageGet { request_id: u32, key: String },
StorageSet {
request_id: u32,
key: String,
#[cfg_attr(feature = "codegen", ts(type = "unknown"))]
value: Value,
},
Notify { level: NotifyLevel, message: String },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "codegen", derive(schemars::JsonSchema, ts_rs::TS))]
#[serde(rename_all = "lowercase")]
pub enum NotifyLevel {
Info,
Warning,
Error,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "codegen", derive(schemars::JsonSchema, ts_rs::TS))]
#[serde(rename_all = "camelCase")]
pub struct ModelSnapshot {
pub elements: Vec<ModelElement>,
pub relations: Vec<ModelRelation>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "codegen", derive(schemars::JsonSchema, ts_rs::TS))]
#[serde(rename_all = "camelCase")]
pub struct ModelElement {
pub key: String,
pub name: String,
pub kind: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional))]
pub phase: Option<String>,
#[serde(default)]
pub ports: Vec<ModelPort>,
pub range: Range,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "codegen", derive(schemars::JsonSchema, ts_rs::TS))]
#[serde(rename_all = "camelCase")]
pub struct ModelPort {
pub name: String,
#[serde(rename = "type", default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional))]
pub port_type: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional))]
pub direction: Option<PortDirection>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "codegen", derive(schemars::JsonSchema, ts_rs::TS))]
#[serde(rename_all = "lowercase")]
pub enum PortDirection {
In,
Out,
Inout,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "codegen", derive(schemars::JsonSchema, ts_rs::TS))]
#[serde(rename_all = "camelCase")]
pub struct ModelRelation {
pub id: String,
pub kind: RelationKind,
pub from: String,
pub to: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "codegen", derive(schemars::JsonSchema, ts_rs::TS))]
#[serde(rename_all = "lowercase")]
pub enum RelationKind {
Satisfy,
Verify,
Frame,
Derivation,
Flow,
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::*;
#[test]
fn host_message_uses_type_tag_and_camel_case_fields() {
let message = HostMessage::RunCommand {
request_id: 7,
command: "fmea.open".to_owned(),
};
let value = serde_json::to_value(&message).unwrap();
assert_eq!(
value,
json!({ "type": "runCommand", "requestId": 7, "command": "fmea.open" })
);
}
#[test]
fn unit_variant_is_just_the_tag() {
let message = PluginMessage::Ready;
let value = serde_json::to_value(&message).unwrap();
assert_eq!(value, json!({ "type": "ready" }));
}
#[test]
fn omitted_optional_fields_are_none() {
let json = r#"{"type":"didChange","version":0,"text":""}"#;
let message: HostMessage = serde_json::from_str(json).unwrap();
assert_eq!(
message,
HostMessage::DidChange {
version: 0,
text: String::new(),
model: None
}
);
}
#[test]
fn activate_without_settings() {
let json = r#"{"type":"activate","apiVersion":"0","pluginId":"a.b"}"#;
let message: HostMessage = serde_json::from_str(json).unwrap();
assert!(matches!(
message,
HostMessage::Activate { settings: None, .. }
));
}
#[test]
fn rejects_invalid_plugin_id_in_activate() {
let json = r#"{"type":"activate","apiVersion":"0","pluginId":"Bad"}"#;
let result = serde_json::from_str::<HostMessage>(json);
assert!(result.is_err());
}
#[test]
fn ignores_unknown_fields() {
let json = r#"{"type":"commandDone","requestId":1,"durationMs":3}"#;
let message: PluginMessage = serde_json::from_str(json).unwrap();
assert_eq!(message, PluginMessage::CommandDone { request_id: 1 });
}
#[test]
fn port_type_is_named_type_in_json() {
let port = ModelPort {
name: "p".to_owned(),
port_type: Some("Power".to_owned()),
direction: Some(PortDirection::Inout),
};
let value = serde_json::to_value(&port).unwrap();
assert_eq!(
value,
json!({ "name": "p", "type": "Power", "direction": "inout" })
);
}
#[test]
fn rejects_unknown_type() {
let json = r#"{"type":"shutdown"}"#;
let result = serde_json::from_str::<HostMessage>(json);
assert!(result.is_err());
}
#[test]
fn rejects_missing_type() {
let json = r#"{"requestId":1}"#;
let result = serde_json::from_str::<PluginMessage>(json);
assert!(result.is_err());
}
#[test]
fn rejects_missing_required_field() {
let json = r#"{"type":"failed","requestId":1}"#;
let result = serde_json::from_str::<PluginMessage>(json);
assert!(result.is_err());
}
#[test]
fn rejects_negative_request_id() {
let json = r#"{"type":"commandDone","requestId":-1}"#;
let result = serde_json::from_str::<PluginMessage>(json);
assert!(result.is_err());
}
#[test]
fn rejects_unknown_relation_kind() {
let json = r#"{"id":"r1","kind":"refinement","from":"a","to":"b"}"#;
let result = serde_json::from_str::<ModelRelation>(json);
assert!(result.is_err());
}
#[test]
fn rejects_unknown_notify_level() {
let json = r#"{"type":"notify","level":"debug","message":"m"}"#;
let result = serde_json::from_str::<PluginMessage>(json);
assert!(result.is_err());
}
}