Skip to content

Commit 61f43f1

Browse files
authored
Merge pull request #268 from stevefan1999-personal/develop
make encoding optional
2 parents e0f6703 + 4308605 commit 61f43f1

File tree

4 files changed

+85
-50
lines changed

4 files changed

+85
-50
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ members = [ "./", "irc-proto/" ]
2424

2525

2626
[features]
27-
default = ["ctcp", "tls-native", "channel-lists", "toml_config"]
27+
default = ["ctcp", "tls-native", "channel-lists", "toml_config", "encoding"]
2828
ctcp = []
2929
channel-lists = []
3030

@@ -39,11 +39,11 @@ proxy = ["tokio-socks"]
3939

4040
tls-native = ["native-tls", "tokio-native-tls"]
4141
tls-rust = ["tokio-rustls", "webpki-roots", "rustls-pemfile"]
42-
42+
encoding = ["dep:encoding", "irc-proto/encoding"]
4343

4444
[dependencies]
4545
chrono = { version = "0.4.24", default-features = false, features = ["clock", "std"] }
46-
encoding = "0.2.33"
46+
encoding = { version = "0.2.33", optional = true }
4747
futures-util = { version = "0.3.30", default-features = false, features = ["alloc", "sink"] }
4848
irc-proto = { version = "1.0.0", path = "irc-proto" }
4949
log = "0.4.21"

irc-proto/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ travis-ci = { repository = "aatxe/irc" }
1818
default = ["bytes", "tokio", "tokio-util"]
1919

2020
[dependencies]
21-
encoding = "0.2.33"
21+
encoding = { version = "0.2.33", optional = true }
2222
thiserror = "1.0.40"
2323

2424
bytes = { version = "1.4.0", optional = true }

irc-proto/src/line.rs

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,37 @@
33
use std::io;
44

55
use bytes::BytesMut;
6+
#[cfg(feature = "encoding")]
67
use encoding::label::encoding_from_whatwg_label;
8+
#[cfg(feature = "encoding")]
79
use encoding::{DecoderTrap, EncoderTrap, EncodingRef};
810
use tokio_util::codec::{Decoder, Encoder};
911

1012
use crate::error;
1113

1214
/// A line-based codec parameterized by an encoding.
1315
pub struct LineCodec {
16+
#[cfg(feature = "encoding")]
1417
encoding: EncodingRef,
1518
next_index: usize,
1619
}
1720

1821
impl LineCodec {
1922
/// Creates a new instance of LineCodec from the specified encoding.
2023
pub fn new(label: &str) -> error::Result<LineCodec> {
21-
encoding_from_whatwg_label(label)
22-
.map(|enc| LineCodec {
23-
encoding: enc,
24-
next_index: 0,
25-
})
26-
.ok_or_else(|| {
27-
io::Error::new(
28-
io::ErrorKind::InvalidInput,
29-
&format!("Attempted to use unknown codec {}.", label)[..],
30-
)
31-
.into()
32-
})
24+
Ok(LineCodec {
25+
#[cfg(feature = "encoding")]
26+
encoding: match encoding_from_whatwg_label(label) {
27+
Some(x) => x,
28+
None => {
29+
return Err(error::ProtocolError::Io(io::Error::new(
30+
io::ErrorKind::InvalidInput,
31+
&format!("Attempted to use unknown codec {}.", label)[..],
32+
)));
33+
}
34+
},
35+
next_index: 0,
36+
})
3337
}
3438
}
3539

@@ -45,14 +49,29 @@ impl Decoder for LineCodec {
4549
// Set the search start index back to 0 since we found a newline.
4650
self.next_index = 0;
4751

48-
// Decode the line using the codec's encoding.
49-
match self.encoding.decode(line.as_ref(), DecoderTrap::Replace) {
50-
Ok(data) => Ok(Some(data)),
51-
Err(data) => Err(io::Error::new(
52-
io::ErrorKind::InvalidInput,
53-
&format!("Failed to decode {} as {}.", data, self.encoding.name())[..],
54-
)
55-
.into()),
52+
#[cfg(feature = "encoding")]
53+
{
54+
// Decode the line using the codec's encoding.
55+
match self.encoding.decode(line.as_ref(), DecoderTrap::Replace) {
56+
Ok(data) => Ok(Some(data)),
57+
Err(data) => Err(io::Error::new(
58+
io::ErrorKind::InvalidInput,
59+
&format!("Failed to decode {} as {}.", data, self.encoding.name())[..],
60+
)
61+
.into()),
62+
}
63+
}
64+
65+
#[cfg(not(feature = "encoding"))]
66+
{
67+
match String::from_utf8(line.to_vec()) {
68+
Ok(data) => Ok(Some(data)),
69+
Err(data) => Err(io::Error::new(
70+
io::ErrorKind::InvalidInput,
71+
&format!("Failed to decode {} as UTF-8.", data)[..],
72+
)
73+
.into()),
74+
}
5675
}
5776
} else {
5877
// Set the search start index to the current length since we know that none of the
@@ -67,20 +86,27 @@ impl Encoder<String> for LineCodec {
6786
type Error = error::ProtocolError;
6887

6988
fn encode(&mut self, msg: String, dst: &mut BytesMut) -> error::Result<()> {
70-
// Encode the message using the codec's encoding.
71-
let data: error::Result<Vec<u8>> = self
72-
.encoding
73-
.encode(&msg, EncoderTrap::Replace)
74-
.map_err(|data| {
75-
io::Error::new(
76-
io::ErrorKind::InvalidInput,
77-
&format!("Failed to encode {} as {}.", data, self.encoding.name())[..],
78-
)
79-
.into()
80-
});
89+
#[cfg(feature = "encoding")]
90+
{
91+
// Encode the message using the codec's encoding.
92+
let data: error::Result<Vec<u8>> = self
93+
.encoding
94+
.encode(&msg, EncoderTrap::Replace)
95+
.map_err(|data| {
96+
io::Error::new(
97+
io::ErrorKind::InvalidInput,
98+
&format!("Failed to encode {} as {}.", data, self.encoding.name())[..],
99+
)
100+
.into()
101+
});
102+
// Write the encoded message to the output buffer.
103+
dst.extend(&data?);
104+
}
81105

82-
// Write the encoded message to the output buffer.
83-
dst.extend(&data?);
106+
#[cfg(not(feature = "encoding"))]
107+
{
108+
dst.extend(msg.into_bytes());
109+
}
84110

85111
Ok(())
86112
}

src/client/conn.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -386,22 +386,31 @@ impl Connection {
386386
config: &Config,
387387
tx: UnboundedSender<Message>,
388388
) -> error::Result<Transport<MockStream>> {
389-
use encoding::{label::encoding_from_whatwg_label, EncoderTrap};
389+
let init_str = config.mock_initial_value();
390390

391-
let encoding = encoding_from_whatwg_label(config.encoding()).ok_or_else(|| {
392-
error::Error::UnknownCodec {
393-
codec: config.encoding().to_owned(),
394-
}
395-
})?;
391+
let initial = {
392+
#[cfg(feature = "encoding")]
393+
{
394+
use encoding::{label::encoding_from_whatwg_label, EncoderTrap};
396395

397-
let init_str = config.mock_initial_value();
398-
let initial = encoding
399-
.encode(init_str, EncoderTrap::Replace)
400-
.map_err(|data| error::Error::CodecFailed {
401-
codec: encoding.name(),
402-
data: data.into_owned(),
403-
})?;
396+
let encoding = encoding_from_whatwg_label(config.encoding()).ok_or_else(|| {
397+
error::Error::UnknownCodec {
398+
codec: config.encoding().to_owned(),
399+
}
400+
})?;
401+
encoding
402+
.encode(init_str, EncoderTrap::Replace)
403+
.map_err(|data| error::Error::CodecFailed {
404+
codec: encoding.name(),
405+
data: data.into_owned(),
406+
})?
407+
}
404408

409+
#[cfg(not(feature = "encoding"))]
410+
{
411+
init_str.as_bytes()
412+
}
413+
};
405414
let stream = MockStream::new(&initial);
406415
let framed = Framed::new(stream, IrcCodec::new(config.encoding())?);
407416

0 commit comments

Comments
 (0)