From 04ccafa029396a8e68a73e6c52dbec8967cfe246 Mon Sep 17 00:00:00 2001 From: mcmah309 Date: Thu, 12 Sep 2024 03:40:06 +0000 Subject: [PATCH] test: Update no_std test --- Cargo.toml | 4 +-- test_no_std/Cargo.toml | 1 + test_no_std/main.rs | 57 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d24b8c2..f39801f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,8 +22,8 @@ tracing-test = { version = "0.2", features = ["no-env-filter"] } lazy_static = "1" [workspace] -members = ["impl"] -exclude = ["test_no_std"] +members = ["impl", "test_no_std"] + [features] default = [] diff --git a/test_no_std/Cargo.toml b/test_no_std/Cargo.toml index 3a4f779..4632b8c 100644 --- a/test_no_std/Cargo.toml +++ b/test_no_std/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] error_set = { path = "../" } +heapless = "0.8" [[bin]] name = "no_std" diff --git a/test_no_std/main.rs b/test_no_std/main.rs index 2bdc529..35b0e54 100644 --- a/test_no_std/main.rs +++ b/test_no_std/main.rs @@ -2,19 +2,23 @@ #![no_main] use error_set::{error_set, CoerceResult}; +use core::fmt::Write; + #[no_mangle] pub extern "C" fn _start() -> ! { - readme_example(); + // readme_example(); + display(); exit(0); } + fn exit(code: i32) -> ! { unsafe { core::arch::asm!( - "mov rax, 60", // Syscall number for exit - "mov rdi, {0}", // Exit code - "syscall", // Trigger the syscall + "mov rax, 60", + "mov rdi, {0}", + "syscall", in(reg) code, options(noreturn) ); @@ -94,4 +98,47 @@ fn readme_example() { let result_download_error: Result<(), DownloadError> = Err(io_error).coerce(); let result_media_error: Result<(), MediaError> = result_download_error.map_err(Into::into); assert!(matches!(result_media_error, Err(MediaError::IoError(_)))); -} \ No newline at end of file +} + +// //************************************************************************// + +error_set! { + AuthError = { + A, + // #[display("User `{}` with role `{}` does not exist", name, role)] // cannot use format + UserDoesNotExist { + name: u32, + role: u32, + }, + #[display("The provided credentials are invalid")] + InvalidCredentials + }; + AuthError2 = { + #[display("User does not exist")] + UserDoesNotExist { + name: u32, + role: u32, + } + }; +} + + +fn display() { + // Seems to always seg fault for some reason + + // let x: AuthError2 = AuthError2::UserDoesNotExist { + // name: 1, + // role: 30, + // }; + // let mut buf: heapless::String<300> = heapless::String::new(); + // write!(buf, "{}", x).unwrap(); + // assert_eq!(buf.as_str(), "User does not exist"); + // let x: AuthError = x.into(); + // let mut buf: heapless::String<300> = heapless::String::new(); + // write!(buf, "{}", x).unwrap(); + // assert_eq!(buf.as_str(), "AuthError::UserDoesNotExist"); + // let x: AuthError = AuthError::InvalidCredentials; + // let mut buf: heapless::String<300> = heapless::String::new(); + // write!(buf, "{}", x).unwrap(); + // assert_eq!(buf.as_str(), "The provided credentials are invalid"); +}