Skip to content

Commit

Permalink
test: Update no_std test
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Sep 12, 2024
1 parent fe9814e commit 04ccafa
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
1 change: 1 addition & 0 deletions test_no_std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
error_set = { path = "../" }
heapless = "0.8"

[[bin]]
name = "no_std"
Expand Down
57 changes: 52 additions & 5 deletions test_no_std/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down Expand Up @@ -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(_))));
}
}

// //************************************************************************//

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");
}

0 comments on commit 04ccafa

Please sign in to comment.