From 6d5f6c9d0f5e2fdb8bb0bb3c192f12480ca9c739 Mon Sep 17 00:00:00 2001 From: Soispha Date: Thu, 28 Dec 2023 10:24:25 +0100 Subject: [PATCH] feat(treewide): Commit to provide an example of the trixy crate --- .gitignore | 3 ++ Cargo.toml | 5 +++ Makefile | 6 ++-- build.rs | 5 +-- c_src/main.c | 98 ++++++++++++++++++++++++++++++++++++++++++---------- src/api.tri | 32 +++++++++++++++++ src/full.tri | 1 + src/lib.rs | 65 ++++++++++++++++++++++++++++------ src/main.rs | 4 +++ 9 files changed, 186 insertions(+), 33 deletions(-) create mode 100644 src/api.tri create mode 120000 src/full.tri create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore index 8738970..2bb710e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,13 @@ # build /target /result +/dist # dev env .direnv .ccls-cache +*.d +*.o # rpc is a library Cargo.lock diff --git a/Cargo.toml b/Cargo.toml index 94f3356..9991ec3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,3 +27,8 @@ edition = "2021" crate-type = ["cdylib"] [dependencies] +rand = "0.8.5" +trixy = { path = "../trixy" } + +[build-dependencies] +trixy = { path = "../trixy" } diff --git a/Makefile b/Makefile index 8ceba28..c63c85d 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/build.rs b/build.rs index 81808dc..20bfea4 100644 --- a/build.rs +++ b/build.rs @@ -18,11 +18,12 @@ * If not, see . */ -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) diff --git a/c_src/main.c b/c_src/main.c index 09e0c44..0804e0f 100644 --- a/c_src/main.c +++ b/c_src/main.c @@ -1,20 +1,82 @@ /* -* Copyright (C) 2023 The Trinitrix Project -* -* 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 . -*/ + * Copyright (C) 2023 The Trinitrix Project + * + * 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 . + */ +#include "../dist/interface.h" +#include +#include +#include +#include + +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; +} diff --git a/src/api.tri b/src/api.tri new file mode 100644 index 0000000..d68e1fd --- /dev/null +++ b/src/api.tri @@ -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; + +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; + fn vec_to_string(input: Vec) -> String; + fn option_to_result(input: Option) -> Result, Error>; +} + + + +// That's a flat out lie, but it results in a rather nice syntax highlight compared to nothing: +// vim: syntax=rust diff --git a/src/full.tri b/src/full.tri new file mode 120000 index 0000000..3220231 --- /dev/null +++ b/src/full.tri @@ -0,0 +1 @@ +/home/soispha/repos/rust/trinitrix/trixy/trixy-parser/example/full.tri \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index c355d64..b10972e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,17 +18,62 @@ * If not, see . */ -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::(CString::new("Hi").expect("Is valid")).into()) + .expect("Will work"); + } + }, } } diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..0e3a1ff --- /dev/null +++ b/src/main.rs @@ -0,0 +1,4 @@ +fn main() { + let input = include_str!(concat!(env!("OUT_DIR"), "/api.rs")); + println!("{}", input); +}