None working attempt at using cxx.

This commit is contained in:
jimmy committed 2026-07-19 20:24:22 -04:00
1 parent 4f509b4ad6
commit 93a9f1baba
5 files changed
+227 -148

No files matched your search

+140 -16
View File
@@ -1,35 +1,159 @@
#![allow(non_snake_case)]
use anyhow::anyhow;
use std::collections::BTreeMap;
#[cxx::bridge]
mod ffi {
// Shared structs whose fields can be seen in both C++ and Rust.
#[derive(Clone, PartialOrd, PartialEq, Default)]
pub enum DiagnosticLevel {
#[default]
UNSET,
OK,
WARN,
ERROR,
STALE,
}
#[derive(Clone)]
pub struct DiagnosticStatus {
level: DiagnosticLevel,
name: String,
message: String,
hardware_id: String,
values: Box<StringMap>
}
extern "Rust" {
fn add(a: &f64, b: &f64) -> f64;
// 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++.
type OptionalString;
#[Self = "OptionalString"]
fn some(val: String) -> Box<OptionalString>;
#[Self = "OptionalString"]
fn none() -> Box<OptionalString>;
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);
type DiagnosticTree;
#[Self = "DiagnosticTree"]
fn build() -> Box<DiagnosticTree>;
fn add_statuses(self: &mut DiagnosticTree, statuses: &Vec<DiagnosticStatus>) -> Result<()> ;
fn reconcile(self: &mut DiagnosticTree) -> Result<()>;
fn aggregate(self: &DiagnosticTree) -> Result<Vec<DiagnosticStatus>>;
type MyStruct;
fn MyStruct_build(a: f64, b: f64) -> Box<MyStruct>;
fn sum(self: &MyStruct) -> f64;
}
}
pub struct MyStruct {
a: f64,
b: f64
fn diagnostic_status_to_rust(cpp: &crate::ffi::DiagnosticStatus) -> j7s_diagnostics::DiagnosticStatus {
todo!()
}
impl MyStruct {
pub fn new(a: f64, b: f64) -> Self {
MyStruct {a: a, b: b}
fn diagnostic_status_from_rust(rust: &j7s_diagnostics::DiagnosticStatus) -> crate::ffi::DiagnosticStatus {
todo!()
}
#[derive(Clone)]
pub struct DiagnosticTree {
graph: j7s_diagnostics::DiagnosticGraph,
}
impl DiagnosticTree {
pub fn build() -> Box<DiagnosticTree> {
let graph = j7s_diagnostics::DiagnosticGraph::new();
let tree = DiagnosticTree {
graph: graph
};
Box::new(tree)
}
pub fn sum(&self) -> f64 {
self.a + self.b
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));
}
Ok(self.graph.add_status_vec(&converted_statuses)?)
}
pub fn reconcile(self: &mut DiagnosticTree) -> anyhow::Result<()> {
Ok(self.graph.reconcile_levels()?)
}
fn aggregate(self: &DiagnosticTree) -> anyhow::Result<Vec<crate::ffi::DiagnosticStatus>> {
let statuses = self.graph.export_graph()?;
let mut converted_statuses = Vec::new();
for status in statuses.iter() {
converted_statuses.push(diagnostic_status_from_rust(&status));
}
Ok(converted_statuses)
}
}
fn MyStruct_build(a: f64, b: f64) -> Box<MyStruct> {
Box::new(MyStruct::new(a, b))
#[derive(Clone)]
pub struct OptionalString {
val: Option<String>
}
pub fn add(a: &f64, b: &f64) -> f64 {
a + b
impl OptionalString {
pub fn some(val: String) -> Box<Self> {
let raw = Self {
val: Some(val)
};
Box::new(raw)
}
pub fn none() -> Box<Self> {
let raw = Self {
val: None
};
Box::new(raw)
}
pub fn is_some(&self) -> bool {
self.val.is_some()
}
pub fn value(&self) -> anyhow::Result<String> {
match &self.val {
None => Err(anyhow!("Option not valid.")),
Some(val) => Ok(val.clone()),
}
}
}
#[derive(Clone)]
pub struct StringMap {
map: BTreeMap<String, String>
}
impl StringMap {
pub fn build() -> Box<Self> {
let raw = Self {
map: BTreeMap::<String, String>::new()
};
Box::new(raw)
}
pub fn get(&self, key: &String) -> Box<OptionalString> {
match self.map.get(key) {
None => OptionalString::none(),
Some(val) => OptionalString::some(val.clone()),
}
}
pub fn insert(&mut self, key: String, value: String) {
let _ = self.map.insert(key, value);
}
pub fn to_box(self) -> Box<StringMap> {
Box::new(self)
}
}