fix(example/main): Update to use the new result type
This commit is contained in:
parent
a33aa053d2
commit
051e15b0ed
|
@ -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"
|
||||
|
|
|
@ -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);
|
||||
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.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):
|
||||
enum DogType output_dog;
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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<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;
|
||||
}
|
||||
|
|
|
@ -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::<OurResult<TrainedDog_c, TrainingMistake_c>>::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::<OurResult<Dog_c, String>>::into(Err(new_name.into())).into_ptr())
|
||||
.unwrap();
|
||||
}
|
||||
},
|
||||
Commands::outstanding { name } => {
|
||||
println!("(rust): {} is outstanding!", name);
|
||||
|
|
Reference in New Issue