1use core::{
2 fmt::{Debug, Display},
3 str::FromStr,
4};
5
6use const_panic::concat_panic;
7
8#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
9pub enum Abi {
11 Rust,
13 C,
15 System,
17 Win64,
19 Sysv64,
21 Aapcs,
23 Cdecl,
25 Stdcall,
27 Fastcall,
29 Vectorcall,
31}
32
33impl Abi {
34 #[must_use]
36 pub const fn to_str(&self) -> &'static str {
37 match self {
38 Abi::Rust => "Rust",
39 Abi::C => "C",
40 Abi::System => "System",
41 Abi::Win64 => "Win64",
42 Abi::Sysv64 => "Sysv64",
43 Abi::Aapcs => "Aapcs",
44 Abi::Cdecl => "Cdecl",
45 Abi::Stdcall => "Stdcall",
46 Abi::Fastcall => "Fastcall",
47 Abi::Vectorcall => "Vectorcall",
48 }
49 }
50}
51
52impl FromStr for Abi {
53 type Err = ();
54
55 fn from_str(s: &str) -> Result<Self, Self::Err> {
56 match s {
57 "" | "Rust" => Ok(Abi::C),
58 "C" => Ok(Abi::C),
59 "system" => Ok(Abi::System),
60 "win64" => Ok(Abi::Win64),
61 "sysv64" => Ok(Abi::Sysv64),
62 "aapcs" => Ok(Abi::Aapcs),
63 "cdecl" => Ok(Abi::Cdecl),
64 "stdcall" => Ok(Abi::Stdcall),
65 "fastcall" => Ok(Abi::Fastcall),
66 "vectorcall" => Ok(Abi::Vectorcall),
67 _ => Err(()),
68 }
69 }
70}
71
72impl Display for Abi {
73 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74 write!(f, "{}", self.to_str())
75 }
76}
77
78#[must_use]
79pub const fn parse(conv: &'static str) -> Option<Abi> {
80 if konst::eq_str(conv, "") || konst::eq_str(conv, "Rust") {
81 Some(Abi::Rust)
82 } else if konst::eq_str(conv, "C") {
83 Some(Abi::C)
84 } else if konst::eq_str(conv, "system") {
85 Some(Abi::System)
86 } else if konst::eq_str(conv, "win64") {
87 Some(Abi::Win64)
88 } else if konst::eq_str(conv, "sysv64") {
89 Some(Abi::Sysv64)
90 } else if konst::eq_str(conv, "aapcs") {
91 Some(Abi::Aapcs)
92 } else if konst::eq_str(conv, "cdecl") {
93 Some(Abi::Cdecl)
94 } else if konst::eq_str(conv, "stdcall") {
95 Some(Abi::Stdcall)
96 } else if konst::eq_str(conv, "fastcall") {
97 Some(Abi::Fastcall)
98 } else if konst::eq_str(conv, "vectorcall") {
99 Some(Abi::Vectorcall)
100 } else {
101 None
102 }
103}
104
105#[must_use]
106pub const fn parse_or_fail(conv: &'static str) -> Abi {
107 match parse(conv) {
108 Some(c) => c,
109 None => concat_panic!("invalid or unknown abi", conv),
110 }
111}