encoding/codec/
error.rs

1// This is a part of encoding-next.
2// Copyright (c) 2013-2015, Kang Seonghoon.
3// See README.md and LICENSE.txt for details.
4
5//! A placeholder encoding that returns encoder/decoder error for every case.
6
7use crate::types::*;
8use std::convert::Into;
9
10/// An encoding that returns encoder/decoder error for every case.
11#[derive(Clone, Copy)]
12pub struct ErrorEncoding;
13
14impl Encoding for ErrorEncoding {
15    fn name(&self) -> &'static str {
16        "error"
17    }
18    fn raw_encoder(&self) -> Box<dyn RawEncoder> {
19        ErrorEncoder::new()
20    }
21    fn raw_decoder(&self) -> Box<dyn RawDecoder> {
22        ErrorDecoder::new()
23    }
24}
25
26/// An encoder that always returns error.
27#[derive(Clone, Copy)]
28pub struct ErrorEncoder;
29
30impl ErrorEncoder {
31    #[allow(clippy::new_ret_no_self)]
32    pub fn new() -> Box<dyn RawEncoder> {
33        Box::new(ErrorEncoder)
34    }
35}
36
37impl RawEncoder for ErrorEncoder {
38    fn from_self(&self) -> Box<dyn RawEncoder> {
39        ErrorEncoder::new()
40    }
41
42    fn raw_feed(
43        &mut self,
44        input: &str,
45        _output: &mut dyn ByteWriter,
46    ) -> (usize, Option<CodecError>) {
47        if let Some(ch) = input.chars().next() {
48            (
49                0,
50                Some(CodecError {
51                    upto: ch.len_utf8() as isize,
52                    cause: "unrepresentable character".into(),
53                }),
54            )
55        } else {
56            (0, None)
57        }
58    }
59
60    fn raw_finish(&mut self, _output: &mut dyn ByteWriter) -> Option<CodecError> {
61        None
62    }
63}
64
65/// A decoder that always returns error.
66#[derive(Clone, Copy)]
67pub struct ErrorDecoder;
68
69impl ErrorDecoder {
70    #[allow(clippy::new_ret_no_self)]
71    pub fn new() -> Box<dyn RawDecoder> {
72        Box::new(ErrorDecoder)
73    }
74}
75
76impl RawDecoder for ErrorDecoder {
77    fn from_self(&self) -> Box<dyn RawDecoder> {
78        ErrorDecoder::new()
79    }
80
81    fn raw_feed(
82        &mut self,
83        input: &[u8],
84        _output: &mut dyn StringWriter,
85    ) -> (usize, Option<CodecError>) {
86        if !input.is_empty() {
87            (
88                0,
89                Some(CodecError {
90                    upto: 1,
91                    cause: "invalid sequence".into(),
92                }),
93            )
94        } else {
95            (0, None)
96        }
97    }
98
99    fn raw_finish(&mut self, _output: &mut dyn StringWriter) -> Option<CodecError> {
100        None
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::ErrorEncoding;
107    use crate::types::*;
108
109    #[test]
110    fn test_encoder() {
111        let mut e = ErrorEncoding.raw_encoder();
112        assert_feed_err!(e, "", "A", "", []);
113        assert_feed_err!(e, "", "B", "C", []);
114        assert_feed_ok!(e, "", "", []);
115        assert_feed_err!(e, "", "\u{a0}", "", []);
116        assert_finish_ok!(e, []);
117    }
118
119    #[test]
120    fn test_decoder() {
121        let mut d = ErrorEncoding.raw_decoder();
122        assert_feed_err!(d, [], [0x41], [], "");
123        assert_feed_err!(d, [], [0x42], [0x43], "");
124        assert_feed_ok!(d, [], [], "");
125        assert_feed_err!(d, [], [0xa0], [], "");
126        assert_finish_ok!(d, "");
127    }
128}