Skip to content

Commit 09b2f53

Browse files
committed
feat: #![no_std]
1 parent 3b5195f commit 09b2f53

File tree

18 files changed

+93
-48
lines changed

18 files changed

+93
-48
lines changed

.github/workflows/ci.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,19 @@ jobs:
3030
steps:
3131
- uses: actions/checkout@v3
3232
- name: Install stable Rust toolchain
33-
uses: actions-rs/toolchain@v1
34-
with:
35-
profile: minimal
36-
toolchain: stable
33+
uses: dtolnay/rust-toolchain@stable
3734
- uses: Swatinem/rust-cache@v1
3835
- name: Run tests
3936
run: |
4037
cargo test --verbose
38+
check-no-std:
39+
name: Check no-std
40+
runs-on: ubuntu-latest
41+
steps:
42+
- uses: actions/checkout@v3
43+
- name: Install stable Rust toolchain
44+
uses: dtolnay/rust-toolchain@stable
45+
- uses: Swatinem/rust-cache@v1
46+
- name: Run checks
47+
run: |
48+
cargo check --no-default-features

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ repository = "https://github.com/pykeio/ssml"
99

1010
[dependencies]
1111
dyn-clone = "1.0"
12-
serde = { version = "1.0", optional = true, features = [ "derive" ] }
13-
erased-serde = { version = "0.4", optional = true }
12+
serde = { version = "1.0", optional = true, default-features = false, features = [ "alloc", "derive" ] }
13+
erased-serde = { version = "0.4", optional = true, default-features = false, features = [ "alloc" ] }
1414

1515
[features]
16-
default = []
16+
default = [ "std" ]
17+
std = []
1718
serde = [ "dep:serde", "dep:erased-serde" ]

src/audio.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use std::fmt::{Display, Write};
1+
use alloc::{
2+
string::{String, ToString},
3+
vec::Vec
4+
};
5+
use core::fmt::{self, Display, Write};
26

37
use crate::{
48
Element, Flavor, Serialize, SerializeOptions, XmlWriter,
@@ -208,7 +212,7 @@ pub fn audio(src: impl ToString) -> Audio {
208212

209213
struct SpeedFormatter(f32);
210214
impl Display for SpeedFormatter {
211-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
215+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212216
f.write_fmt(format_args!("{}%", self.0 * 100.))
213217
}
214218
}

src/break.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fmt::Write;
1+
use core::fmt::Write;
22

33
use crate::{Serialize, SerializeOptions, TimeDesignation, XmlWriter};
44

src/element.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::fmt::{Debug, Write};
1+
use alloc::{boxed::Box, string::ToString, vec::Vec};
2+
use core::fmt::{Debug, Write};
23

34
use dyn_clone::DynClone;
45

@@ -29,7 +30,7 @@ macro_rules! el {
2930
})*
3031

3132
impl $crate::Serialize for $name {
32-
fn serialize_xml<W: ::std::fmt::Write>(&self, writer: &mut $crate::XmlWriter<W>, options: &$crate::SerializeOptions) -> crate::Result<()> {
33+
fn serialize_xml<W: ::core::fmt::Write>(&self, writer: &mut $crate::XmlWriter<W>, options: &$crate::SerializeOptions) -> $crate::Result<()> {
3334
match self {
3435
$($name::$variant(inner) => inner.serialize_xml(writer, options),)*
3536
}
@@ -73,6 +74,9 @@ impl<'a> serde::Deserialize<'a> for Element {
7374
where
7475
D: serde::Deserializer<'a>
7576
{
77+
use alloc::string::String;
78+
use core::{fmt, marker::PhantomData};
79+
7680
#[allow(non_camel_case_types)]
7781
enum ElementField {
7882
Text,
@@ -89,7 +93,7 @@ impl<'a> serde::Deserialize<'a> for Element {
8993
impl<'de> serde::de::Visitor<'de> for ElementFieldVisitor {
9094
type Value = ElementField;
9195

92-
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
96+
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
9397
f.write_str("variant identifier")
9498
}
9599

@@ -160,13 +164,13 @@ impl<'a> serde::Deserialize<'a> for Element {
160164

161165
#[doc(hidden)]
162166
struct Visitor<'de> {
163-
marker: std::marker::PhantomData<Element>,
164-
lifetime: std::marker::PhantomData<&'de ()>
167+
marker: PhantomData<Element>,
168+
lifetime: PhantomData<&'de ()>
165169
}
166170
impl<'de> serde::de::Visitor<'de> for Visitor<'de> {
167171
type Value = Element;
168172

169-
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
173+
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
170174
f.write_str("enum Element")
171175
}
172176

src/emphasis.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::fmt::Write;
1+
use alloc::vec::Vec;
2+
use core::fmt::Write;
23

34
use crate::{Element, Serialize, SerializeOptions, XmlWriter};
45

src/error.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::{
2-
error::Error as StdError,
1+
use alloc::string::String;
2+
use core::{
33
fmt::{self, Display},
44
str::Utf8Error
55
};
@@ -46,16 +46,17 @@ impl Display for Error {
4646
}
4747
}
4848

49-
impl StdError for Error {}
49+
#[cfg(feature = "std")]
50+
impl std::error::Error for Error {}
5051

51-
pub type Result<T, E = Error> = std::result::Result<T, E>;
52+
pub type Result<T, E = Error> = core::result::Result<T, E>;
5253

5354
macro_rules! error {
5455
($m:literal) => {
55-
$crate::Error::Generic(format!($m))
56+
$crate::Error::Generic(::alloc::format!($m))
5657
};
5758
($fmt:expr, $($arg:tt)*) => {
58-
$crate::Error::Generic(format!($fmt, $($arg)*))
59+
$crate::Error::Generic(::alloc::format!($fmt, $($arg)*))
5960
};
6061
}
6162
pub(crate) use error;

src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@
2424
//! # }
2525
//! ```
2626
27+
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
2728
#![allow(clippy::tabs_in_doc_comments)]
2829

29-
use std::fmt::{Debug, Write};
30+
extern crate alloc;
31+
extern crate core;
32+
33+
use alloc::{
34+
string::{String, ToString},
35+
vec::Vec
36+
};
37+
use core::fmt::{Debug, Write};
3038

3139
mod audio;
3240
mod r#break;

src/mark.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::fmt::Write;
1+
use alloc::string::{String, ToString};
2+
use core::fmt::Write;
23

34
use crate::{Serialize, SerializeOptions, XmlWriter};
45

src/mstts/express.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::fmt::Write;
1+
use alloc::{string::String, vec::Vec};
2+
use core::fmt::Write;
23

34
use crate::{Element, Flavor, Serialize, SerializeOptions, XmlWriter, util};
45

src/mstts/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Elements exclusive to [`Flavor::MicrosoftAzureCognitiveSpeechServices`] (ACSS/MSTTS).
22
3-
use std::fmt::Display;
3+
use alloc::{format, string::ToString};
4+
use core::fmt::{self, Display};
45

56
use crate::{Flavor, Meta, voice::Voice};
67

@@ -27,7 +28,7 @@ pub enum MicrosoftViseme {
2728
}
2829

2930
impl Display for MicrosoftViseme {
30-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3132
f.write_str(match self {
3233
MicrosoftViseme::ById => "redlips_front",
3334
MicrosoftViseme::FacialExpression => "FacialExpression"
@@ -56,7 +57,7 @@ pub enum MicrosoftVoiceEffect {
5657
}
5758

5859
impl Display for MicrosoftVoiceEffect {
59-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6061
f.write_str(match self {
6162
MicrosoftVoiceEffect::Automobile => "eq_car",
6263
MicrosoftVoiceEffect::Telecom => "eq_telecomhp8k"

src/say_as.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use alloc::string::String;
2+
13
pub(crate) trait SpeechFormat {
24
fn into_format(self) -> (String, Option<String>, Option<String>);
35
}

src/speak.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use std::fmt::{Debug, Write};
1+
use alloc::{
2+
string::{String, ToString},
3+
vec::Vec
4+
};
5+
use core::fmt::{Debug, Write};
26

37
use crate::{Element, Flavor, Serialize, SerializeOptions, XmlWriter, util};
48

@@ -23,7 +27,7 @@ impl Speak {
2327
pub fn new<S: Into<Element>, I: IntoIterator<Item = S>>(lang: Option<&str>, elements: I) -> Self {
2428
Self {
2529
children: elements.into_iter().map(|f| f.into()).collect(),
26-
lang: lang.map(|f| f.to_owned()),
30+
lang: lang.map(|f| f.to_string()),
2731
..Speak::default()
2832
}
2933
}
@@ -142,7 +146,7 @@ impl Serialize for Speak {
142146
pub fn speak<S: Into<Element>, I: IntoIterator<Item = S>>(lang: Option<&str>, elements: I) -> Speak {
143147
Speak {
144148
children: elements.into_iter().map(|f| f.into()).collect(),
145-
lang: lang.map(|f| f.to_owned()),
149+
lang: lang.map(|f| f.to_string()),
146150
..Speak::default()
147151
}
148152
}

src/text.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::fmt::Write;
1+
use alloc::string::{String, ToString};
2+
use core::fmt::Write;
23

34
use crate::{Serialize, SerializeOptions, XmlWriter};
45

src/unit.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::{
2-
error::Error,
3-
fmt::{Debug, Display},
1+
use core::{
2+
fmt::{self, Debug, Display},
43
num::ParseFloatError,
54
str::FromStr
65
};
@@ -16,7 +15,7 @@ pub enum TimeDesignationError {
1615
}
1716

1817
impl Display for TimeDesignationError {
19-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019
match self {
2120
TimeDesignationError::BadUnit => f.write_str("time designation has invalid unit (allowed are ms, s)"),
2221
TimeDesignationError::BadLength => f.write_str("string is too short to be a valid time designation"),
@@ -26,7 +25,8 @@ impl Display for TimeDesignationError {
2625
}
2726
}
2827

29-
impl Error for TimeDesignationError {}
28+
#[cfg(feature = "std")]
29+
impl std::error::Error for TimeDesignationError {}
3030

3131
/// A time designation is a representation of a non-negative offset of time.
3232
///
@@ -97,14 +97,14 @@ impl From<&str> for TimeDesignation {
9797
}
9898

9999
impl Display for TimeDesignation {
100-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101101
f.write_fmt(format_args!("{:+}ms", self.as_millis()))
102102
}
103103
}
104104
impl TrustedNoEscape for TimeDesignation {}
105105

106106
impl Debug for TimeDesignation {
107-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
107+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108108
Display::fmt(self, f)
109109
}
110110
}
@@ -117,7 +117,7 @@ pub enum DecibelsError {
117117
}
118118

119119
impl Display for DecibelsError {
120-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
120+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121121
match self {
122122
DecibelsError::BadUnit => f.write_str("decibels has invalid unit (allowed are dB)"),
123123
DecibelsError::BadLength => f.write_str("string is too short to be a valid decibel value"),
@@ -126,7 +126,8 @@ impl Display for DecibelsError {
126126
}
127127
}
128128

129-
impl Error for DecibelsError {}
129+
#[cfg(feature = "std")]
130+
impl std::error::Error for DecibelsError {}
130131

131132
/// A string representation of a signed amplitude offset in decibels (`dB`).
132133
///
@@ -179,14 +180,14 @@ impl From<&str> for Decibels {
179180
}
180181

181182
impl Display for Decibels {
182-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
183+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183184
f.write_fmt(format_args!("{:+}dB", self.0))
184185
}
185186
}
186187
impl TrustedNoEscape for Decibels {}
187188

188189
impl Debug for Decibels {
189-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
190+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
190191
Display::fmt(self, f)
191192
}
192193
}

src/util.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Utilities for serializing XML.
22
3-
use std::fmt::{self, Write};
3+
use alloc::string::String;
4+
use core::fmt::{self, Write};
45

56
use crate::{Element, Serialize, SerializeOptions, XmlWriter};
67

src/voice.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use std::fmt::{Display, Write};
1+
use alloc::{
2+
string::{String, ToString},
3+
vec,
4+
vec::Vec
5+
};
6+
use core::fmt::{self, Display, Write};
27

38
use crate::{Element, Serialize, SerializeOptions, XmlWriter, util, xml::TrustedNoEscape};
49

@@ -13,7 +18,7 @@ pub enum VoiceGender {
1318
}
1419

1520
impl Display for VoiceGender {
16-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1722
f.write_str(match self {
1823
VoiceGender::Unspecified => "",
1924
VoiceGender::Neutral => "neutral",

src/xml/writer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::fmt::{self, Display, Write};
1+
use alloc::string::{String, ToString};
2+
use core::fmt::{self, Display, Write};
23

34
use crate::util;
45

0 commit comments

Comments
 (0)