Conversion functions.
This commit is contained in:
1 parent
93a9f1baba
commit
d1314797a5
7 files changed
+243
-39
No files matched your search
Generated
+5
-5
@@ -26,9 +26,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.6.2"
|
||||
version = "4.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd059f9da4f5c36b3787f65d38ccaab1cc315f07b01f89abc8359ee6a8205011"
|
||||
checksum = "d91e0c145792ef73a6ad36d27c75ac09f1832222a3c209689d90f534685ee5b7"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
]
|
||||
@@ -160,7 +160,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "j7s_diagnostics"
|
||||
version = "0.1.0"
|
||||
source = "git+https://git.jpace121.net/public/j7s_diagnostics.git?branch=main#f40b64151807cbca03a021e66cb3bf3558e4619d"
|
||||
source = "git+https://git.jpace121.net/public/j7s_diagnostics.git?branch=main#5f5e9c3b8aa82fd6d14f7a7931604a225fd6b551"
|
||||
dependencies = [
|
||||
"limbo_graph",
|
||||
"thiserror",
|
||||
@@ -258,9 +258,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "3.0.1"
|
||||
version = "3.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5edbec4ed188954a10c12c038215f8ce7606b2d5c973cd8dc43e8795065c5f2f"
|
||||
checksum = "53e9bae58849f64dfa4f5d5ae372c8341f7305f82a3868709269343628b659a3"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
use anyhow::anyhow;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[cxx::bridge]
|
||||
#[cxx::bridge(namespace = "j7s_diagnostics_cxx")]
|
||||
mod ffi {
|
||||
// Shared structs whose fields can be seen in both C++ and Rust.
|
||||
#[derive(Clone, PartialOrd, PartialEq, Default)]
|
||||
@@ -21,13 +21,16 @@ mod ffi {
|
||||
name: String,
|
||||
message: String,
|
||||
hardware_id: String,
|
||||
values: Box<StringMap>
|
||||
values: Box<StringMap>,
|
||||
}
|
||||
|
||||
extern "Rust" {
|
||||
// Functions in here can be seen on both sides, but are
|
||||
// defined in rust.
|
||||
// types can be seen in both languages, but only opaquely in C++.
|
||||
#[Self = "DiagnosticStatus"]
|
||||
fn build() -> DiagnosticStatus;
|
||||
|
||||
type OptionalString;
|
||||
#[Self = "OptionalString"]
|
||||
fn some(val: String) -> Box<OptionalString>;
|
||||
@@ -36,29 +39,80 @@ mod ffi {
|
||||
fn is_some(self: &OptionalString) -> bool;
|
||||
fn value(self: &OptionalString) -> Result<String>;
|
||||
|
||||
|
||||
type StringMap;
|
||||
#[Self = "StringMap"]
|
||||
fn build() -> Box<StringMap>;
|
||||
fn get(self: &StringMap, key: &String) -> Box<OptionalString>;
|
||||
fn insert(self: &mut StringMap, key: String, value: String);
|
||||
fn keys(self: &StringMap) -> Vec<String>;
|
||||
|
||||
type DiagnosticTree;
|
||||
#[Self = "DiagnosticTree"]
|
||||
fn build() -> Box<DiagnosticTree>;
|
||||
fn add_statuses(self: &mut DiagnosticTree, statuses: &Vec<DiagnosticStatus>) -> Result<()> ;
|
||||
fn add_statuses(self: &mut DiagnosticTree, statuses: &Vec<DiagnosticStatus>) -> Result<()>;
|
||||
fn reconcile(self: &mut DiagnosticTree) -> Result<()>;
|
||||
fn aggregate(self: &DiagnosticTree) -> Result<Vec<DiagnosticStatus>>;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fn diagnostic_status_to_rust(cpp: &crate::ffi::DiagnosticStatus) -> j7s_diagnostics::DiagnosticStatus {
|
||||
todo!()
|
||||
fn diagnostic_level_to_rust(cpp: &crate::ffi::DiagnosticLevel) -> j7s_diagnostics::DiagnosticLevel {
|
||||
match cpp {
|
||||
&crate::ffi::DiagnosticLevel::UNSET => j7s_diagnostics::DiagnosticLevel::UNSET,
|
||||
&crate::ffi::DiagnosticLevel::OK => j7s_diagnostics::DiagnosticLevel::OK,
|
||||
&crate::ffi::DiagnosticLevel::WARN => j7s_diagnostics::DiagnosticLevel::WARN,
|
||||
&crate::ffi::DiagnosticLevel::ERROR => j7s_diagnostics::DiagnosticLevel::ERROR,
|
||||
&crate::ffi::DiagnosticLevel::STALE => j7s_diagnostics::DiagnosticLevel::STALE,
|
||||
_ => j7s_diagnostics::DiagnosticLevel::UNSET,
|
||||
}
|
||||
}
|
||||
|
||||
fn diagnostic_status_from_rust(rust: &j7s_diagnostics::DiagnosticStatus) -> crate::ffi::DiagnosticStatus {
|
||||
todo!()
|
||||
fn diagnostic_level_from_rust(
|
||||
rust: &j7s_diagnostics::DiagnosticLevel,
|
||||
) -> crate::ffi::DiagnosticLevel {
|
||||
match rust {
|
||||
&j7s_diagnostics::DiagnosticLevel::UNSET => crate::ffi::DiagnosticLevel::UNSET,
|
||||
&j7s_diagnostics::DiagnosticLevel::OK => crate::ffi::DiagnosticLevel::OK,
|
||||
&j7s_diagnostics::DiagnosticLevel::WARN => crate::ffi::DiagnosticLevel::WARN,
|
||||
&j7s_diagnostics::DiagnosticLevel::ERROR => crate::ffi::DiagnosticLevel::ERROR,
|
||||
&j7s_diagnostics::DiagnosticLevel::STALE => crate::ffi::DiagnosticLevel::STALE,
|
||||
}
|
||||
}
|
||||
|
||||
fn diagnostic_status_to_rust(
|
||||
cpp: &crate::ffi::DiagnosticStatus,
|
||||
) -> j7s_diagnostics::DiagnosticStatus {
|
||||
j7s_diagnostics::DiagnosticStatus::new(
|
||||
diagnostic_level_to_rust(&cpp.level),
|
||||
cpp.name.clone(),
|
||||
cpp.message.clone(),
|
||||
cpp.hardware_id.clone(),
|
||||
cpp.values.to_btree(),
|
||||
)
|
||||
}
|
||||
|
||||
fn diagnostic_status_from_rust(
|
||||
rust: &j7s_diagnostics::DiagnosticStatus,
|
||||
) -> crate::ffi::DiagnosticStatus {
|
||||
crate::ffi::DiagnosticStatus {
|
||||
level: diagnostic_level_from_rust(&rust.level()),
|
||||
name: rust.name(),
|
||||
message: rust.message(),
|
||||
hardware_id: rust.hardware_id(),
|
||||
values: Box::new(StringMap::from_btree(rust.values())),
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::ffi::DiagnosticStatus {
|
||||
fn build() -> Self {
|
||||
Self {
|
||||
level: crate::ffi::DiagnosticLevel::default(),
|
||||
name: String::default(),
|
||||
message: String::default(),
|
||||
hardware_id: String::default(),
|
||||
values: StringMap::build(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -69,13 +123,14 @@ pub struct DiagnosticTree {
|
||||
impl DiagnosticTree {
|
||||
pub fn build() -> Box<DiagnosticTree> {
|
||||
let graph = j7s_diagnostics::DiagnosticGraph::new();
|
||||
let tree = DiagnosticTree {
|
||||
graph: graph
|
||||
};
|
||||
let tree = DiagnosticTree { graph: graph };
|
||||
Box::new(tree)
|
||||
}
|
||||
|
||||
pub fn add_statuses(self: &mut DiagnosticTree, statuses: &Vec<crate::ffi::DiagnosticStatus>) -> anyhow::Result<()> {
|
||||
pub fn add_statuses(
|
||||
self: &mut DiagnosticTree,
|
||||
statuses: &Vec<crate::ffi::DiagnosticStatus>,
|
||||
) -> anyhow::Result<()> {
|
||||
let mut converted_statuses = Vec::new();
|
||||
for status in statuses.iter() {
|
||||
converted_statuses.push(diagnostic_status_to_rust(&status));
|
||||
@@ -99,21 +154,17 @@ impl DiagnosticTree {
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct OptionalString {
|
||||
val: Option<String>
|
||||
val: Option<String>,
|
||||
}
|
||||
|
||||
impl OptionalString {
|
||||
pub fn some(val: String) -> Box<Self> {
|
||||
let raw = Self {
|
||||
val: Some(val)
|
||||
};
|
||||
let raw = Self { val: Some(val) };
|
||||
Box::new(raw)
|
||||
}
|
||||
|
||||
pub fn none() -> Box<Self> {
|
||||
let raw = Self {
|
||||
val: None
|
||||
};
|
||||
let raw = Self { val: None };
|
||||
Box::new(raw)
|
||||
}
|
||||
|
||||
@@ -131,22 +182,34 @@ impl OptionalString {
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct StringMap {
|
||||
map: BTreeMap<String, String>
|
||||
map: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
impl StringMap {
|
||||
pub fn build() -> Box<Self> {
|
||||
let raw = Self {
|
||||
map: BTreeMap::<String, String>::new()
|
||||
map: BTreeMap::<String, String>::new(),
|
||||
};
|
||||
Box::new(raw)
|
||||
}
|
||||
|
||||
pub fn from_btree(map: BTreeMap<String, String>) -> Self {
|
||||
Self { map: map }
|
||||
}
|
||||
|
||||
fn keys(&self) -> Vec<String> {
|
||||
self.map.keys().map(|s| s.clone()).collect()
|
||||
}
|
||||
|
||||
pub fn to_btree(&self) -> BTreeMap<String, String> {
|
||||
self.map.clone()
|
||||
}
|
||||
|
||||
pub fn get(&self, key: &String) -> Box<OptionalString> {
|
||||
match self.map.get(key) {
|
||||
None => OptionalString::none(),
|
||||
Some(val) => OptionalString::some(val.clone()),
|
||||
}
|
||||
match self.map.get(key) {
|
||||
None => OptionalString::none(),
|
||||
Some(val) => OptionalString::some(val.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, key: String, value: String) {
|
||||
|
||||
Reference in new issue
Block a user