feat(treewide): Commit to provide an example of the trixy crate

This commit is contained in:
Benedikt Peetz 2023-12-28 10:24:25 +01:00
parent db4243a918
commit 6d5f6c9d0f
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
9 changed files with 186 additions and 33 deletions

3
.gitignore vendored
View File

@ -1,10 +1,13 @@
# build
/target
/result
/dist
# dev env
.direnv
.ccls-cache
*.d
*.o
# rpc is a library
Cargo.lock

View File

@ -27,3 +27,8 @@ edition = "2021"
crate-type = ["cdylib"]
[dependencies]
rand = "0.8.5"
trixy = { path = "../trixy" }
[build-dependencies]
trixy = { path = "../trixy" }

View File

@ -20,7 +20,7 @@ PREFIX := /usr/local
BINPREFIX := $(DESTDIR)$(PREFIX)/bin
MANPREFIX := $(DESTDIR)$(PREFIX)/share/man/man1
BIN_NAME := main
BIN_NAME := ./target/main
SRC := $(wildcard c_src/*.c)
OBJ := $(SRC:.c=.o)
@ -28,8 +28,8 @@ DEP := $(OBJ:.o=.d)
LIBS := rpc
ALL_CFLAGS := -fPIC -O3 -MMD -Wall -Wextra -Wno-unused-parameter $(CFLAGS) $(CPPFLAGS)
ALL_LDFLAGS := -shared $(addprefix -l,$(LIBS)) -L $(LD_LIBRARY_PATH) $(CFLAGS) $(LDFLAGS)
ALL_CFLAGS := -O3 -MMD -Wall -Wextra -Wno-unused-parameter $(CFLAGS) $(CPPFLAGS)
ALL_LDFLAGS := $(addprefix -l,$(LIBS)) -L $(LD_LIBRARY_PATH) $(CFLAGS) $(LDFLAGS)

View File

@ -18,11 +18,12 @@
* If not, see <https://www.gnu.org/licenses/>.
*/
use trixy_macros::config::TrixyConfig;
use trixy::macros::config::TrixyConfig;
fn main() {
println!("cargo:rerun-if-changed=./dist/*");
println!("cargo:rerun-if-changed=./src/api.tri");
TrixyConfig::new("callback")
TrixyConfig::new("handle_cmd")
.trixy_path("./src/api.tri")
.dist_dir_path("./dist")
.generate_debug(true)

View File

@ -1,20 +1,82 @@
/*
* Copyright (C) 2023 The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
*
* This file is part of the RPC crate for Trinitrix.
*
* RPC is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* and the Lesser GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/
* Copyright (C) 2023 The Trinitrix Project <soispha@vhack.eu,
* antifallobst@systemausfall.org>
*
* This file is part of the RPC crate for Trinitrix.
*
* RPC is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* and the Lesser GNU General Public License along with this program.
* 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;
}

32
src/api.tri Normal file
View File

@ -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

1
src/full.tri Symbolic link
View File

@ -0,0 +1 @@
/home/soispha/repos/rust/trinitrix/trixy/trixy-parser/example/full.tri

View File

@ -18,17 +18,62 @@
* If not, see <https://www.gnu.org/licenses/>.
*/
pub fn add(left: usize, right: usize) -> usize {
left + right
use std::ffi::{CStr, CString};
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)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
fn handle_cmd(cmd: Commands) {
match cmd {
Commands::initialize_socket { path, trixy_output } => {
println!("(rust): {}", path);
trixy_output
.send(Ok::<(), SocketInitError>(()).into())
.expect("This should work");
}
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");
}
},
}
}

4
src/main.rs Normal file
View File

@ -0,0 +1,4 @@
fn main() {
let input = include_str!(concat!(env!("OUT_DIR"), "/api.rs"));
println!("{}", input);
}