Skip to content

Commit 25e400a

Browse files
Initial no_std support
Since `core::error` isn't in Rust until 1.81, we can't use it and so we only derive `Error` for the error types when building with the `std` feature. Also, `webdriver` implies `std` as well (for now?).
1 parent 3719e6a commit 25e400a

File tree

5 files changed

+27
-12
lines changed

5 files changed

+27
-12
lines changed

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ edition = "2021"
1111
rust-version = "1.61"
1212

1313
[features]
14-
default = ["serde", "webdriver"]
14+
default = ["serde", "std", "webdriver"]
1515
serde = ["dep:serde", "bitflags/serde"]
16-
webdriver = ["unicode-segmentation"]
16+
std = ["serde?/std"]
17+
webdriver = ["std", "unicode-segmentation"]
1718

1819
[dependencies]
1920
bitflags = "2"
20-
serde = { version = "1.0.0", optional = true, features = ["derive"] }
21+
serde = { version = "1.0.0", optional = true, default-features = false, features = ["derive"] }
2122
unicode-segmentation = { version = "1.2.0", optional = true }

convert.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ def convert_key(text, file):
9292
#![cfg_attr(rustfmt, rustfmt_skip)]
9393
#![allow(clippy::doc_markdown)]
9494
95-
use std::fmt::{self, Display};
96-
use std::str::FromStr;
95+
use alloc::string::{String, ToString};
96+
use core::fmt::{self, Display};
97+
use core::str::FromStr;
98+
#[cfg(feature = "std")]
9799
use std::error::Error;
98100
99101
/// Key represents the meaning of a keypress.
@@ -159,6 +161,7 @@ def convert_key(text, file):
159161
}
160162
}
161163
164+
#[cfg(feature = "std")]
162165
impl Error for UnrecognizedKeyError {}
163166
164167
/// Check if string can be used as a `Key::Character` _keystring_.
@@ -189,8 +192,9 @@ def convert_code(text, file):
189192
#![cfg_attr(rustfmt, rustfmt_skip)]
190193
#![allow(clippy::doc_markdown)]
191194
192-
use std::fmt::{self, Display};
193-
use std::str::FromStr;
195+
use core::fmt::{self, Display};
196+
use core::str::FromStr;
197+
#[cfg(feature = "std")]
194198
use std::error::Error;
195199
196200
/// Code is the physical position of a key.
@@ -294,6 +298,7 @@ def convert_code(text, file):
294298
}
295299
}
296300
301+
#[cfg(feature = "std")]
297302
impl Error for UnrecognizedCodeError {}
298303
""", file=file)
299304

src/code.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
#![cfg_attr(rustfmt, rustfmt_skip)]
44
#![allow(clippy::doc_markdown)]
55

6-
use std::fmt::{self, Display};
7-
use std::str::FromStr;
6+
use core::fmt::{self, Display};
7+
use core::str::FromStr;
8+
#[cfg(feature = "std")]
89
use std::error::Error;
910

1011
/// Code is the physical position of a key.
@@ -931,5 +932,6 @@ impl fmt::Display for UnrecognizedCodeError {
931932
}
932933
}
933934

935+
#[cfg(feature = "std")]
934936
impl Error for UnrecognizedCodeError {}
935937

src/key.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
#![cfg_attr(rustfmt, rustfmt_skip)]
44
#![allow(clippy::doc_markdown)]
55

6-
use std::fmt::{self, Display};
7-
use std::str::FromStr;
6+
use alloc::string::{String, ToString};
7+
use core::fmt::{self, Display};
8+
use core::str::FromStr;
9+
#[cfg(feature = "std")]
810
use std::error::Error;
911

1012
/// Key represents the meaning of a keypress.
@@ -1316,6 +1318,7 @@ impl fmt::Display for UnrecognizedKeyError {
13161318
}
13171319
}
13181320

1321+
#[cfg(feature = "std")]
13191322
impl Error for UnrecognizedKeyError {}
13201323

13211324
/// Check if string can be used as a `Key::Character` _keystring_.

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
//! input in a cross-platform way.
66
77
#![warn(clippy::doc_markdown)]
8+
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
89

9-
use std::fmt;
10+
extern crate alloc;
11+
12+
use alloc::string::String;
13+
use core::fmt;
1014

1115
pub use crate::code::{Code, UnrecognizedCodeError};
1216
pub use crate::key::{Key, UnrecognizedKeyError};

0 commit comments

Comments
 (0)