fix(example/main): Update to use the new result type

This commit is contained in:
Benedikt Peetz 2024-05-20 15:21:45 +02:00
parent a33aa053d2
commit 051e15b0ed
Signed by: bpeetz
GPG Key ID: B6139BCB07CE946D
5 changed files with 52 additions and 78 deletions

View File

@ -90,9 +90,9 @@ dependencies = [
[[package]] [[package]]
name = "pulldown-cmark" name = "pulldown-cmark"
version = "0.10.3" version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76979bea66e7875e7509c4ec5300112b316af87fa7a252ca91c448b32dfe3993" checksum = "8746739f11d39ce5ad5c2520a9b75285310dbfe78c541ccf832d38615765aec0"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"getopts", "getopts",
@ -103,9 +103,9 @@ dependencies = [
[[package]] [[package]]
name = "pulldown-cmark-escape" name = "pulldown-cmark-escape"
version = "0.10.1" version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd348ff538bc9caeda7ee8cad2d1d48236a1f443c1fa3913c6a02fe0043b1dd3" checksum = "007d8adb5ddab6f8e3f491ac63566a7d5002cc7ed73901f72057943fa71ae1ae"
[[package]] [[package]]
name = "quote" name = "quote"

View File

@ -88,14 +88,24 @@ plugin_main ()
// The API also has full support for custom types (the `Dog` and `TrainedDog` // The API also has full support for custom types (the `Dog` and `TrainedDog`
// types are defined in the `api.tri` file) // types are defined in the `api.tri` file)
struct Dog dog = { .name = "Bruce" }; struct Dog dog = { .name = "Bruce" };
struct TrainedDog tDog; Result (TrainedDog, TrainingMistake) tDog;
if (!dogs.train_dog (&tDog, dog)) if (!dogs.train_dog (&tDog, dog))
handle_error (); handle_error ();
println ("Dog %s is now trained with specialization %i", tDog.name, if (tDog.tag == OK)
tDog.training); {
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 // Beware that you need to free them with the appropriate free functions
string_free (tDog.name); 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): // Trixy also supports functions (i.e. callbacks):
enum DogType output_dog; enum DogType output_dog;

View File

@ -1,56 +0,0 @@
/*
* Copyright (C) 2023 - 2024:
* The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
* 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 <https://www.gnu.org/licenses/>.
*/
#include "../dist/interface.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}

View File

@ -29,11 +29,11 @@ enum DogTraining {
Blind, Blind,
} }
#[error] #[derive(Error)]
enum TrainingMistake { enum TrainingMistake {
#[msg("The dog got bitten")] #[error = "The dog got bitten"]
GotBitten, GotBitten,
#[msg("The trainer died")] #[error = "The trainer died"]
Died, Died,
} }
@ -57,11 +57,16 @@ mod dogs {
name: String, name: String,
training: DogTraining, training: DogTraining,
} }
/// Say hi to a name /// Say hi to a name
fn hi(name: String) -> String; fn hi(name: String) -> String;
/// Train a dog /// Train a dog
fn train_dog(dog: Dog) -> TrainedDog; fn train_dog(dog: Dog) -> Result<TrainedDog, TrainingMistake>;
/// 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<Dog, String>;
fn guess_my_favourite_dog(callback: fn(name: String, age: u32) -> Dog) -> DogType; fn guess_my_favourite_dog(callback: fn(name: String, age: u32) -> Dog) -> DogType;
} }

View File

@ -22,15 +22,18 @@
use std::{env, ffi::c_int}; use std::{env, ffi::c_int};
use dogs_c::TrainedDog_c;
use libloading::{Library, Symbol}; 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")); include!(concat!(env!("OUT_DIR"), "/api.rs"));
// run `cargo run --bin api > ./src/bin/main/generated_api.rs` to output the generated api // 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::*; // pub use generated_api::*;
fn handle_cmd(cmd: Commands) { fn handle_cmd(cmd: Commands) {
@ -47,12 +50,15 @@ fn handle_cmd(cmd: Commands) {
} => { } => {
trixy_output trixy_output
.send( .send(
TrainedDog { Into::<OurResult<TrainedDog_c, TrainingMistake_c>>::into(
name: "Willis".into(), Err(TrainingMistake::Died.into()), // ok variant
training: DogTraining::Wolf, // Ok(TrainedDog {
} // name: "Willis".into(),
.try_into() // training: DogTraining::Wolf,
.unwrap(), // }
// .into_ptr()),
)
.into_ptr(),
) )
.unwrap(); .unwrap();
} }
@ -65,6 +71,15 @@ fn handle_cmd(cmd: Commands) {
println!("(rust): They want a dog named: {}", dog_name); println!("(rust): They want a dog named: {}", dog_name);
trixy_output.send(DogType::Cat.into()).unwrap(); trixy_output.send(DogType::Cat.into()).unwrap();
} }
dogs::Dogs::rename_dog {
trixy_output,
dog: _,
new_name,
} => {
trixy_output
.send(Into::<OurResult<Dog_c, String>>::into(Err(new_name.into())).into_ptr())
.unwrap();
}
}, },
Commands::outstanding { name } => { Commands::outstanding { name } => {
println!("(rust): {} is outstanding!", name); println!("(rust): {} is outstanding!", name);