feat(treewide): Commit to provide an example of the trixy crate
This commit is contained in:
parent
db4243a918
commit
6d5f6c9d0f
|
@ -1,10 +1,13 @@
|
||||||
# build
|
# build
|
||||||
/target
|
/target
|
||||||
/result
|
/result
|
||||||
|
/dist
|
||||||
|
|
||||||
# dev env
|
# dev env
|
||||||
.direnv
|
.direnv
|
||||||
.ccls-cache
|
.ccls-cache
|
||||||
|
*.d
|
||||||
|
*.o
|
||||||
|
|
||||||
# rpc is a library
|
# rpc is a library
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
|
|
@ -27,3 +27,8 @@ edition = "2021"
|
||||||
crate-type = ["cdylib"]
|
crate-type = ["cdylib"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
rand = "0.8.5"
|
||||||
|
trixy = { path = "../trixy" }
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
trixy = { path = "../trixy" }
|
||||||
|
|
6
Makefile
6
Makefile
|
@ -20,7 +20,7 @@ PREFIX := /usr/local
|
||||||
BINPREFIX := $(DESTDIR)$(PREFIX)/bin
|
BINPREFIX := $(DESTDIR)$(PREFIX)/bin
|
||||||
MANPREFIX := $(DESTDIR)$(PREFIX)/share/man/man1
|
MANPREFIX := $(DESTDIR)$(PREFIX)/share/man/man1
|
||||||
|
|
||||||
BIN_NAME := main
|
BIN_NAME := ./target/main
|
||||||
|
|
||||||
SRC := $(wildcard c_src/*.c)
|
SRC := $(wildcard c_src/*.c)
|
||||||
OBJ := $(SRC:.c=.o)
|
OBJ := $(SRC:.c=.o)
|
||||||
|
@ -28,8 +28,8 @@ DEP := $(OBJ:.o=.d)
|
||||||
|
|
||||||
LIBS := rpc
|
LIBS := rpc
|
||||||
|
|
||||||
ALL_CFLAGS := -fPIC -O3 -MMD -Wall -Wextra -Wno-unused-parameter $(CFLAGS) $(CPPFLAGS)
|
ALL_CFLAGS := -O3 -MMD -Wall -Wextra -Wno-unused-parameter $(CFLAGS) $(CPPFLAGS)
|
||||||
ALL_LDFLAGS := -shared $(addprefix -l,$(LIBS)) -L $(LD_LIBRARY_PATH) $(CFLAGS) $(LDFLAGS)
|
ALL_LDFLAGS := $(addprefix -l,$(LIBS)) -L $(LD_LIBRARY_PATH) $(CFLAGS) $(LDFLAGS)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
5
build.rs
5
build.rs
|
@ -18,11 +18,12 @@
|
||||||
* If not, see <https://www.gnu.org/licenses/>.
|
* If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use trixy_macros::config::TrixyConfig;
|
use trixy::macros::config::TrixyConfig;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
println!("cargo:rerun-if-changed=./dist/*");
|
||||||
println!("cargo:rerun-if-changed=./src/api.tri");
|
println!("cargo:rerun-if-changed=./src/api.tri");
|
||||||
TrixyConfig::new("callback")
|
TrixyConfig::new("handle_cmd")
|
||||||
.trixy_path("./src/api.tri")
|
.trixy_path("./src/api.tri")
|
||||||
.dist_dir_path("./dist")
|
.dist_dir_path("./dist")
|
||||||
.generate_debug(true)
|
.generate_debug(true)
|
||||||
|
|
64
c_src/main.c
64
c_src/main.c
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2023 The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
|
* Copyright (C) 2023 The Trinitrix Project <soispha@vhack.eu,
|
||||||
|
* antifallobst@systemausfall.org>
|
||||||
*
|
*
|
||||||
* This file is part of the RPC crate for Trinitrix.
|
* This file is part of the RPC crate for Trinitrix.
|
||||||
*
|
*
|
||||||
|
@ -18,3 +19,64 @@
|
||||||
* If not, see <https://www.gnu.org/licenses/>.
|
* If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "../dist/interface.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
printf("Checking result\n");
|
||||||
|
result_t result;
|
||||||
|
if (!initialize_socket(&result, "hi")) {
|
||||||
|
int error_length = last_error_length();
|
||||||
|
char *buffer = malloc(error_length);
|
||||||
|
last_error_message(buffer, error_length);
|
||||||
|
printf("error: %s\n", buffer);
|
||||||
|
}
|
||||||
|
if (result.tag == ok) {
|
||||||
|
printf("Hurray! Value is: %p\n", result.value);
|
||||||
|
}
|
||||||
|
printf("Type is: %d\n", result.type_id);
|
||||||
|
|
||||||
|
printf("\n\nChecking enum\n");
|
||||||
|
enum_a_t enum_a;
|
||||||
|
if (fn_alone(&enum_a, "hi")) {
|
||||||
|
printf("Call succeded, it returned: %d\n", enum_a);
|
||||||
|
assert(enum_a == Val1);
|
||||||
|
} else {
|
||||||
|
int error_length = last_error_length();
|
||||||
|
char *buffer = malloc(error_length);
|
||||||
|
last_error_message(buffer, error_length);
|
||||||
|
printf("error: %s\n", buffer);
|
||||||
|
};
|
||||||
|
|
||||||
|
printf("\n\nChecking str\n");
|
||||||
|
str_t str;
|
||||||
|
if (one.fn_one(&str)) {
|
||||||
|
printf("Call succeded, it returned: %s\n", str);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n\nChecking string\n");
|
||||||
|
string_t string;
|
||||||
|
if (one.two.fn_two(&string)) {
|
||||||
|
printf("Call succeded, it returned: %s\n", string);
|
||||||
|
}
|
||||||
|
string = strcpy(string, "some other text");
|
||||||
|
printf("String is after the strcpy: %s\n", string);
|
||||||
|
free(string);
|
||||||
|
|
||||||
|
printf("\n\nChecking result with string\n");
|
||||||
|
result_t str_result;
|
||||||
|
if (one.two.three.fn_three(&str_result)) {
|
||||||
|
if (str_result.tag == err) {
|
||||||
|
printf("Call failed, it returned: %s\n", (str_t)str_result.value);
|
||||||
|
} else {
|
||||||
|
printf("Call succeded, it returned: %s\n", (string_t)str_result.value);
|
||||||
|
}
|
||||||
|
printf("Type is: %d\n", str_result.type_id);
|
||||||
|
assert(str_result.type_id == type_string_t);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
/// Initialize the underlying rpc socket.
|
||||||
|
/// This function is not actually part of Trinitrix's API, meaning that only this rpc
|
||||||
|
/// library will understand it.
|
||||||
|
///
|
||||||
|
/// It **must** be called before any other functions, as rpc otherwise won't be able to
|
||||||
|
/// talk to Trinitrix
|
||||||
|
fn initialize_socket(path: String) -> Result<void, SocketInitError>;
|
||||||
|
|
||||||
|
enum SocketInitError {
|
||||||
|
Err1,
|
||||||
|
Err2
|
||||||
|
};
|
||||||
|
|
||||||
|
enum EnumA {
|
||||||
|
Val1,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn string_to_enum(input: String) -> EnumA;
|
||||||
|
|
||||||
|
nasp one {
|
||||||
|
enum Error {
|
||||||
|
Val1,
|
||||||
|
};
|
||||||
|
fn str_to_result(input: str) -> Result<str, Error>;
|
||||||
|
fn vec_to_string(input: Vec<String>) -> String;
|
||||||
|
fn option_to_result(input: Option<String>) -> Result<Option<String>, Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// That's a flat out lie, but it results in a rather nice syntax highlight compared to nothing:
|
||||||
|
// vim: syntax=rust
|
|
@ -0,0 +1 @@
|
||||||
|
/home/soispha/repos/rust/trinitrix/trixy/trixy-parser/example/full.tri
|
65
src/lib.rs
65
src/lib.rs
|
@ -18,17 +18,62 @@
|
||||||
* If not, see <https://www.gnu.org/licenses/>.
|
* If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
pub fn add(left: usize, right: usize) -> usize {
|
use std::ffi::{CStr, CString};
|
||||||
left + right
|
|
||||||
|
use crate::one::Error;
|
||||||
|
|
||||||
|
include!(concat!(env!("OUT_DIR"), "/api.rs"));
|
||||||
|
|
||||||
|
macro_rules! cstr {
|
||||||
|
($str:expr) => {
|
||||||
|
std::ffi::CStr::from_bytes_with_nul(concat!($str, '\0').as_bytes())
|
||||||
|
.expect("Should always work")
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
fn handle_cmd(cmd: Commands) {
|
||||||
mod tests {
|
match cmd {
|
||||||
use super::*;
|
Commands::initialize_socket { path, trixy_output } => {
|
||||||
|
println!("(rust): {}", path);
|
||||||
#[test]
|
trixy_output
|
||||||
fn it_works() {
|
.send(Ok::<(), SocketInitError>(()).into())
|
||||||
let result = add(2, 2);
|
.expect("This should work");
|
||||||
assert_eq!(result, 4);
|
}
|
||||||
|
Commands::string_to_enum {
|
||||||
|
trixy_output,
|
||||||
|
input,
|
||||||
|
} => {
|
||||||
|
println!("(rust): {}", input);
|
||||||
|
trixy_output.send(EnumA::Val1).expect("Will work");
|
||||||
|
}
|
||||||
|
Commands::One(one) => match one {
|
||||||
|
one::One::str_to_result {
|
||||||
|
trixy_output,
|
||||||
|
input,
|
||||||
|
} => {
|
||||||
|
println!("(rust): {}", input);
|
||||||
|
trixy_output
|
||||||
|
.send(Ok::<(), Error>(()).into())
|
||||||
|
.expect("Will work");
|
||||||
|
}
|
||||||
|
one::One::vec_to_string {
|
||||||
|
trixy_output,
|
||||||
|
input,
|
||||||
|
} => {
|
||||||
|
println!("(rust): {:#?}", input);
|
||||||
|
trixy_output
|
||||||
|
.send(CString::new("Hi").expect("Is valid").into())
|
||||||
|
.expect("Will work");
|
||||||
|
}
|
||||||
|
one::One::option_to_result {
|
||||||
|
trixy_output,
|
||||||
|
input,
|
||||||
|
} => {
|
||||||
|
println!("(rust): {:#?}", input);
|
||||||
|
trixy_output
|
||||||
|
.send(Err::<Error, CString>(CString::new("Hi").expect("Is valid")).into())
|
||||||
|
.expect("Will work");
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
fn main() {
|
||||||
|
let input = include_str!(concat!(env!("OUT_DIR"), "/api.rs"));
|
||||||
|
println!("{}", input);
|
||||||
|
}
|
Loading…
Reference in New Issue