Skip to content

Commit c529f43

Browse files
ivilaDemesneGH
authored andcommitted
examples & projects: simplify proto crate
- truncate uuid.txt and remove build.rs - use num_enum for conversion between enum and u32 Signed-off-by: Zehui Chen <[email protected]> Reviewed-by: Yuan Zhuang <[email protected]>
1 parent 6c2fece commit c529f43

File tree

84 files changed

+224
-1153
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+224
-1153
lines changed

examples/acipher-rs/proto/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,4 @@ description = "Data structures and functions shared by host and TA."
2525
edition = "2018"
2626

2727
[dependencies]
28-
29-
[build_dependencies]
30-
uuid = { version = "1.6.1", default-features = false }
28+
num_enum = { version = "0.7.3", default-features = false }

examples/acipher-rs/proto/build.rs

-36
This file was deleted.

examples/acipher-rs/proto/src/lib.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,20 @@
1616
// under the License.
1717

1818
#![no_std]
19+
use num_enum::{FromPrimitive, IntoPrimitive};
1920

21+
#[derive(FromPrimitive, IntoPrimitive)]
22+
#[repr(u32)]
2023
pub enum Command {
2124
GenKey,
2225
GetSize,
2326
Encrypt,
2427
Decrypt,
28+
#[default]
2529
Unknown,
2630
}
2731

28-
impl From<u32> for Command {
29-
#[inline]
30-
fn from(value: u32) -> Command {
31-
match value {
32-
0 => Command::GenKey,
33-
1 => Command::GetSize,
34-
2 => Command::Encrypt,
35-
3 => Command::Decrypt,
36-
_ => Command::Unknown,
37-
}
38-
}
39-
}
40-
41-
pub const UUID: &str = &include_str!(concat!(env!("OUT_DIR"), "/uuid.txt"));
32+
// If Uuid::parse_str() returns an InvalidLength error, there may be an extra
33+
// newline in your uuid.txt file. You can remove it by running
34+
// `truncate -s 36 uuid.txt`.
35+
pub const UUID: &str = &include_str!("../../uuid.txt");

examples/acipher-rs/uuid.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
057f4b66-bdab-11eb-96cf-33d6e41cc849
1+
057f4b66-bdab-11eb-96cf-33d6e41cc849

examples/aes-rs/proto/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,4 @@ description = "Data structures and functions shared by host and TA."
2525
edition = "2018"
2626

2727
[dependencies]
28-
29-
[build_dependencies]
30-
uuid = { version = "1.6.1", default-features = false }
28+
num_enum = { version = "0.7.3", default-features = false }

examples/aes-rs/proto/build.rs

-36
This file was deleted.

examples/aes-rs/proto/src/lib.rs

+17-48
Original file line numberDiff line numberDiff line change
@@ -16,79 +16,48 @@
1616
// under the License.
1717

1818
#![no_std]
19+
use num_enum::{FromPrimitive, IntoPrimitive};
1920

21+
#[derive(FromPrimitive, IntoPrimitive)]
22+
#[repr(u32)]
2023
pub enum Command {
2124
Prepare,
2225
SetKey,
2326
SetIV,
2427
Cipher,
28+
#[default]
2529
Unknown,
2630
}
2731

28-
impl From<u32> for Command {
29-
#[inline]
30-
fn from(value: u32) -> Command {
31-
match value {
32-
0 => Command::Prepare,
33-
1 => Command::SetKey,
34-
2 => Command::SetIV,
35-
3 => Command::Cipher,
36-
_ => Command::Unknown,
37-
}
38-
}
39-
}
40-
32+
#[derive(FromPrimitive, IntoPrimitive)]
33+
#[repr(u32)]
4134
pub enum Algo {
4235
ECB,
4336
CBC,
4437
CTR,
38+
#[default]
4539
Unknown,
4640
}
4741

48-
impl From<u32> for Algo {
49-
#[inline]
50-
fn from(value: u32) -> Algo {
51-
match value {
52-
0 => Algo::ECB,
53-
1 => Algo::CBC,
54-
2 => Algo::CTR,
55-
_ => Algo::Unknown,
56-
}
57-
}
58-
}
59-
42+
#[derive(FromPrimitive, IntoPrimitive)]
43+
#[repr(u32)]
6044
pub enum Mode {
6145
Decode,
6246
Encode,
47+
#[default]
6348
Unknown,
6449
}
6550

66-
impl From<u32> for Mode {
67-
#[inline]
68-
fn from(value: u32) -> Mode {
69-
match value {
70-
0 => Mode::Decode,
71-
1 => Mode::Encode,
72-
_ => Mode::Unknown,
73-
}
74-
}
75-
}
76-
51+
#[derive(FromPrimitive, IntoPrimitive)]
52+
#[repr(u32)]
7753
pub enum KeySize {
7854
Bit128 = 16,
7955
Bit256 = 32,
56+
#[default]
8057
Unknown = 0,
8158
}
8259

83-
impl From<u32> for KeySize {
84-
#[inline]
85-
fn from(value: u32) -> KeySize {
86-
match value {
87-
16 => KeySize::Bit128,
88-
32 => KeySize::Bit256,
89-
_ => KeySize::Unknown,
90-
}
91-
}
92-
}
93-
94-
pub const UUID: &str = &include_str!(concat!(env!("OUT_DIR"), "/uuid.txt"));
60+
// If Uuid::parse_str() returns an InvalidLength error, there may be an extra
61+
// newline in your uuid.txt file. You can remove it by running
62+
// `truncate -s 36 uuid.txt`.
63+
pub const UUID: &str = &include_str!("../../uuid.txt");

examples/aes-rs/uuid.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0864c8ec-bdab-11eb-8926-c7fa47a8c92d
1+
0864c8ec-bdab-11eb-8926-c7fa47a8c92d

examples/authentication-rs/proto/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,4 @@ description = "Data structures and functions shared by host and TA."
2525
edition = "2018"
2626

2727
[dependencies]
28-
29-
[build_dependencies]
30-
uuid = { version = "1.6.1", default-features = false }
28+
num_enum = { version = "0.7.3", default-features = false }

examples/authentication-rs/proto/build.rs

-36
This file was deleted.

examples/authentication-rs/proto/src/lib.rs

+11-25
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,34 @@
1616
// under the License.
1717

1818
#![no_std]
19+
use num_enum::{FromPrimitive, IntoPrimitive};
1920

21+
#[derive(FromPrimitive, IntoPrimitive)]
22+
#[repr(u32)]
2023
pub enum Command {
2124
Prepare,
2225
Update,
2326
EncFinal,
2427
DecFinal,
28+
#[default]
2529
Unknown,
2630
}
2731

28-
impl From<u32> for Command {
29-
#[inline]
30-
fn from(value: u32) -> Command {
31-
match value {
32-
0 => Command::Prepare,
33-
1 => Command::Update,
34-
2 => Command::EncFinal,
35-
3 => Command::DecFinal,
36-
_ => Command::Unknown,
37-
}
38-
}
39-
}
40-
32+
#[derive(FromPrimitive, IntoPrimitive)]
33+
#[repr(u32)]
4134
pub enum Mode {
4235
Encrypt,
4336
Decrypt,
37+
#[default]
4438
Unknown,
4539
}
4640

47-
impl From<u32> for Mode {
48-
#[inline]
49-
fn from(value: u32) -> Mode {
50-
match value {
51-
0 => Mode::Encrypt,
52-
1 => Mode::Decrypt,
53-
_ => Mode::Unknown,
54-
}
55-
}
56-
}
57-
5841
pub const BUFFER_SIZE: usize = 16;
5942
pub const KEY_SIZE: usize = 16;
6043
pub const AAD_LEN: usize = 16;
6144
pub const TAG_LEN: usize = 16;
6245

63-
pub const UUID: &str = &include_str!(concat!(env!("OUT_DIR"), "/uuid.txt"));
46+
// If Uuid::parse_str() returns an InvalidLength error, there may be an extra
47+
// newline in your uuid.txt file. You can remove it by running
48+
// `truncate -s 36 uuid.txt`.
49+
pub const UUID: &str = &include_str!("../../uuid.txt");

examples/authentication-rs/uuid.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0a5a06b2-bdab-11eb-add0-77f29de31296
1+
0a5a06b2-bdab-11eb-add0-77f29de31296

examples/big_int-rs/proto/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,4 @@ description = "Data structures and functions shared by host and TA."
2525
edition = "2018"
2626

2727
[dependencies]
28-
29-
[build_dependencies]
30-
uuid = { version = "1.6.1", default-features = false }
28+
num_enum = { version = "0.7.3", default-features = false }

examples/big_int-rs/proto/build.rs

-36
This file was deleted.

0 commit comments

Comments
 (0)