From 61a62ed379f85e87b23bde10430a8ac6f122cffc Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Mon, 20 May 2024 15:21:45 +0200 Subject: [PATCH] fix(example/main): Update to use the new result type --- example/main/Cargo.lock | 8 ++--- example/main/c/main.c | 20 ++++++++--- example/main/c/trinitrix.c | 56 ------------------------------- example/main/src/bin/main/api.tri | 13 ++++--- example/main/src/bin/main/main.rs | 33 +++++++++++++----- 5 files changed, 52 insertions(+), 78 deletions(-) delete mode 100644 example/main/c/trinitrix.c diff --git a/example/main/Cargo.lock b/example/main/Cargo.lock index 5f5e099..0026e92 100644 --- a/example/main/Cargo.lock +++ b/example/main/Cargo.lock @@ -90,9 +90,9 @@ dependencies = [ [[package]] name = "pulldown-cmark" -version = "0.10.3" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76979bea66e7875e7509c4ec5300112b316af87fa7a252ca91c448b32dfe3993" +checksum = "8746739f11d39ce5ad5c2520a9b75285310dbfe78c541ccf832d38615765aec0" dependencies = [ "bitflags", "getopts", @@ -103,9 +103,9 @@ dependencies = [ [[package]] name = "pulldown-cmark-escape" -version = "0.10.1" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd348ff538bc9caeda7ee8cad2d1d48236a1f443c1fa3913c6a02fe0043b1dd3" +checksum = "007d8adb5ddab6f8e3f491ac63566a7d5002cc7ed73901f72057943fa71ae1ae" [[package]] name = "quote" diff --git a/example/main/c/main.c b/example/main/c/main.c index 5de1453..d3a5b37 100644 --- a/example/main/c/main.c +++ b/example/main/c/main.c @@ -88,14 +88,24 @@ plugin_main () // The API also has full support for custom types (the `Dog` and `TrainedDog` // types are defined in the `api.tri` file) struct Dog dog = { .name = "Bruce" }; - struct TrainedDog tDog; + Result (TrainedDog, TrainingMistake) tDog; if (!dogs.train_dog (&tDog, dog)) handle_error (); - println ("Dog %s is now trained with specialization %i", tDog.name, - tDog.training); - // Beware that you need to free them with the appropriate free functions - string_free (tDog.name); + if (tDog.tag == OK) + { + println ("Dog %s is now trained with specialization %i", + tDog.value.ok.name, tDog.value.ok.training); + + // Beware that you need to free them with the appropriate free functions + string_free (tDog.value.ok.name); + } + else + { + eprintln ( + "The dog could not be trained because of a training mistake: '%d'", + tDog.value.err) + } // Trixy also supports functions (i.e. callbacks): enum DogType output_dog; diff --git a/example/main/c/trinitrix.c b/example/main/c/trinitrix.c deleted file mode 100644 index a50866d..0000000 --- a/example/main/c/trinitrix.c +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (C) 2023 - 2024: - * The Trinitrix Project - * SPDX-License-Identifier: LGPL-3.0-or-later - * - * This file is part of the Trixy crate for Trinitrix. - * - * Trixy 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 - -#define println(args...) \ - printf ("\33[32;1m(plugin):\33[0m \33[34;1m"); \ - printf (args); \ - printf ("\n\33[0m"); \ - fflush (stdout); -#define eprintln(args...) \ - printf ("\33[32;1m(plugin):\33[0m\33[31;1m "); \ - printf (args); \ - printf ("\n\33[0m"); \ - fflush (stdout); - -void -handle_error () -{ - int error_length = last_error_length (); - char *error = malloc (error_length); - last_error_message (error, error_length); - eprintln ("Encountered error: %s", error); - free (error); -} - -int -plugin_main () -{ - if (!trinitrix.api.room_message_send ("Hi!")) - handle_error (); - return EXIT_SUCCESS; -} diff --git a/example/main/src/bin/main/api.tri b/example/main/src/bin/main/api.tri index 8b77d96..f5a833e 100644 --- a/example/main/src/bin/main/api.tri +++ b/example/main/src/bin/main/api.tri @@ -29,11 +29,11 @@ enum DogTraining { Blind, } -#[error] +#[derive(Error)] enum TrainingMistake { - #[msg("The dog got bitten")] + #[error = "The dog got bitten"] GotBitten, - #[msg("The trainer died")] + #[error = "The trainer died"] Died, } @@ -57,11 +57,16 @@ mod dogs { name: String, training: DogTraining, } + /// Say hi to a name fn hi(name: String) -> String; /// Train a dog - fn train_dog(dog: Dog) -> TrainedDog; + fn train_dog(dog: Dog) -> Result; + + /// Renames a dog, will return the same name again, if the dog didn't accept the new + /// name. + fn rename_dog(dog: Dog, new_name: String) -> Result; fn guess_my_favourite_dog(callback: fn(name: String, age: u32) -> Dog) -> DogType; } diff --git a/example/main/src/bin/main/main.rs b/example/main/src/bin/main/main.rs index b9e0341..df1be75 100644 --- a/example/main/src/bin/main/main.rs +++ b/example/main/src/bin/main/main.rs @@ -22,15 +22,18 @@ use std::{env, ffi::c_int}; +use dogs_c::TrainedDog_c; use libloading::{Library, Symbol}; -use trixy::types::newtypes; +use trixy::types::{newtypes, traits::convert_trait::Convertible, String}; -use crate::dogs::{DogType, TrainedDog}; +use crate::dogs::DogType; include!(concat!(env!("OUT_DIR"), "/api.rs")); // run `cargo run --bin api > ./src/bin/main/generated_api.rs` to output the generated api -// mod generated_api; + +// pub mod generated_api; +// TODO: Remove this crutch, when trixy accepts a submodule <2024-05-20> // pub use generated_api::*; fn handle_cmd(cmd: Commands) { @@ -47,12 +50,15 @@ fn handle_cmd(cmd: Commands) { } => { trixy_output .send( - TrainedDog { - name: "Willis".into(), - training: DogTraining::Wolf, - } - .try_into() - .unwrap(), + Into::>::into( + Err(TrainingMistake::Died.into()), // ok variant + // Ok(TrainedDog { + // name: "Willis".into(), + // training: DogTraining::Wolf, + // } + // .into_ptr()), + ) + .into_ptr(), ) .unwrap(); } @@ -65,6 +71,15 @@ fn handle_cmd(cmd: Commands) { println!("(rust): They want a dog named: {}", dog_name); trixy_output.send(DogType::Cat.into()).unwrap(); } + dogs::Dogs::rename_dog { + trixy_output, + dog: _, + new_name, + } => { + trixy_output + .send(Into::>::into(Err(new_name.into())).into_ptr()) + .unwrap(); + } }, Commands::outstanding { name } => { println!("(rust): {} is outstanding!", name);