• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zhiburt / expectrl / 4369552515

pending completion
4369552515

Pull #61

github

GitHub
Merge ff4fae653 into 794d67830
Pull Request #61: Windows CI

1471 of 2612 relevant lines covered (56.32%)

3.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

2.75
/src/control_code.rs
1
//! A module which contains [ControlCode] type.
2

3
use std::convert::TryFrom;
4

5
/// ControlCode represents the standard ASCII control codes [wiki]
6
///
7
/// [wiki]: https://en.wikipedia.org/wiki/C0_and_C1_control_codes
8
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9
pub enum ControlCode {
10
    /// Often used as a string terminator, especially in the programming language C.
11
    Null,
12
    /// In message transmission, delimits the start of a message header.
13
    StartOfHeading,
14
    /// First character of message text, and may be used to terminate the message heading.
15
    StartOfText,
16
    /// Often used as a "break" character (Ctrl-C) to interrupt or terminate a program or process.
17
    EndOfText,
18
    /// Often used on Unix to indicate end-of-file on a terminal (Ctrl-D).
19
    EndOfTransmission,
20
    /// Signal intended to trigger a response at the receiving end, to see if it is still present.
21
    Enquiry,
22
    /// Response to an Enquiry, or an indication of successful receipt of a message.
23
    Acknowledge,
24
    /// Used for a beep on systems that didn't have a physical bell.
25
    Bell,
26
    /// Move the cursor one position leftwards.
27
    /// On input, this may delete the character to the left of the cursor.
28
    Backspace,
29
    /// Position to the next character tab stop.
30
    HorizontalTabulation,
31
    /// On Unix, used to mark end-of-line.
32
    /// In DOS, Windows, and various network standards, LF is used following CR as part of the end-of-line mark.
33
    LineFeed,
34
    /// Position the form at the next line tab stop.
35
    VerticalTabulation,
36
    /// It appears in some common plain text files as a page break character.
37
    FormFeed,
38
    /// Originally used to move the cursor to column zero while staying on the same line.
39
    CarriageReturn,
40
    /// Switch to an alternative character set.
41
    ShiftOut,
42
    /// Return to regular character set after ShiftOut.
43
    ShiftIn,
44
    /// May cause a limited number of contiguously following octets to be interpreted in some different way.
45
    DataLinkEscape,
46
    /// A control code which is reserved for device control.
47
    DeviceControl1,
48
    /// A control code which is reserved for device control.
49
    DeviceControl2,
50
    /// A control code which is reserved for device control.
51
    DeviceControl3,
52
    /// A control code which is reserved for device control.
53
    DeviceControl4,
54
    /// In multipoint systems, the NAK is used as the not-ready reply to a poll.
55
    NegativeAcknowledge,
56
    /// Used in synchronous transmission systems to provide a signal from which synchronous correction may be achieved.
57
    SynchronousIdle,
58
    /// Indicates the end of a transmission block of data.
59
    EndOfTransmissionBlock,
60
    /// Indicates that the data preceding it are in error or are to be disregarded.
61
    Cancel,
62
    /// May mark the end of the used portion of the physical medium.
63
    EndOfMedium,
64
    /// Sometimes used to indicate the end of file, both when typing on the terminal and in text files stored on disk.
65
    Substitute,
66
    /// The Esc key on the keyboard will cause this character to be sent on most systems.
67
    /// In systems based on ISO/IEC 2022, even if another set of C0 control codes are used,
68
    /// this octet is required to always represent the escape character.
69
    Escape,
70
    /// Can be used as delimiters to mark fields of data structures.
71
    /// Also it used for hierarchical levels;
72
    /// FS == level 4
73
    FileSeparator,
74
    /// It used for hierarchical levels;
75
    /// GS == level 3
76
    GroupSeparator,
77
    /// It used for hierarchical levels;
78
    /// RS == level 2
79
    RecordSeparator,
80
    /// It used for hierarchical levels;
81
    /// US == level 1
82
    UnitSeparator,
83
    /// Space is a graphic character. It causes the active position to be advanced by one character position.
84
    Space,
85
    /// Usually called backspace on modern machines, and does not correspond to the PC delete key.
86
    Delete,
87
}
88

89
impl ControlCode {
90
    /// See [ControlCode::Null]
91
    pub const NUL: ControlCode = ControlCode::Null;
92
    /// See [ControlCode::StartOfHeading]
93
    pub const SOH: ControlCode = ControlCode::StartOfHeading;
94
    /// See [ControlCode::StartOfText]
95
    pub const STX: ControlCode = ControlCode::StartOfText;
96
    /// See [ControlCode::EndOfText]
97
    pub const ETX: ControlCode = ControlCode::EndOfText;
98
    /// See [ControlCode::EndOfTransmission]
99
    pub const EOT: ControlCode = ControlCode::EndOfTransmission;
100
    /// See [ControlCode::Enquiry]
101
    pub const ENQ: ControlCode = ControlCode::Enquiry;
102
    /// See [ControlCode::Acknowledge]
103
    pub const ACK: ControlCode = ControlCode::Acknowledge;
104
    /// See [ControlCode::Bell]
105
    pub const BEL: ControlCode = ControlCode::Bell;
106
    /// See [ControlCode::Backspace]
107
    pub const BS: ControlCode = ControlCode::Backspace;
108
    /// See [ControlCode::HorizontalTabulation]
109
    pub const HT: ControlCode = ControlCode::HorizontalTabulation;
110
    /// See [ControlCode::LineFeed]
111
    pub const LF: ControlCode = ControlCode::LineFeed;
112
    /// See [ControlCode::VerticalTabulation]
113
    pub const VT: ControlCode = ControlCode::VerticalTabulation;
114
    /// See [ControlCode::FormFeed]
115
    pub const FF: ControlCode = ControlCode::FormFeed;
116
    /// See [ControlCode::CarriageReturn]
117
    pub const CR: ControlCode = ControlCode::CarriageReturn;
118
    /// See [ControlCode::ShiftOut]
119
    pub const SO: ControlCode = ControlCode::ShiftOut;
120
    /// See [ControlCode::ShiftIn]
121
    pub const SI: ControlCode = ControlCode::ShiftIn;
122
    /// See [ControlCode::DataLinkEscape]
123
    pub const DLE: ControlCode = ControlCode::DataLinkEscape;
124
    /// See [ControlCode::DeviceControl1]
125
    pub const DC1: ControlCode = ControlCode::DeviceControl1;
126
    /// See [ControlCode::DeviceControl2]
127
    pub const DC2: ControlCode = ControlCode::DeviceControl2;
128
    /// See [ControlCode::DeviceControl3]
129
    pub const DC3: ControlCode = ControlCode::DeviceControl3;
130
    /// See [ControlCode::DeviceControl4]
131
    pub const DC4: ControlCode = ControlCode::DeviceControl4;
132
    /// See [ControlCode::NegativeAcknowledge]
133
    pub const NAK: ControlCode = ControlCode::NegativeAcknowledge;
134
    /// See [ControlCode::SynchronousIdle]
135
    pub const SYN: ControlCode = ControlCode::SynchronousIdle;
136
    /// See [ControlCode::EndOfTransmissionBlock]
137
    pub const ETB: ControlCode = ControlCode::EndOfTransmissionBlock;
138
    /// See [ControlCode::Cancel]
139
    pub const CAN: ControlCode = ControlCode::Cancel;
140
    /// See [ControlCode::EndOfMedium]
141
    pub const EM: ControlCode = ControlCode::EndOfMedium;
142
    /// See [ControlCode::Substitute]
143
    pub const SUB: ControlCode = ControlCode::Substitute;
144
    /// See [ControlCode::Escape]
145
    pub const ESC: ControlCode = ControlCode::Escape;
146
    /// See [ControlCode::FileSeparator]
147
    pub const FS: ControlCode = ControlCode::FileSeparator;
148
    /// See [ControlCode::GroupSeparator]
149
    pub const GS: ControlCode = ControlCode::GroupSeparator;
150
    /// See [ControlCode::RecordSeparator]
151
    pub const RS: ControlCode = ControlCode::RecordSeparator;
152
    /// See [ControlCode::UnitSeparator]
153
    pub const US: ControlCode = ControlCode::UnitSeparator;
154
    /// See [ControlCode::Space]
155
    pub const SP: ControlCode = ControlCode::Space;
156
    /// See [ControlCode::Delete]
157
    pub const DEL: ControlCode = ControlCode::Delete;
158
}
159

160
impl From<ControlCode> for u8 {
161
    fn from(val: ControlCode) -> Self {
×
162
        use ControlCode::*;
163
        match val {
×
164
            Null => 0,
×
165
            StartOfHeading => 1,
×
166
            StartOfText => 2,
×
167
            EndOfText => 3,
×
168
            EndOfTransmission => 4,
×
169
            Enquiry => 5,
×
170
            Acknowledge => 6,
×
171
            Bell => 7,
×
172
            Backspace => 8,
×
173
            HorizontalTabulation => 9,
×
174
            LineFeed => 10,
×
175
            VerticalTabulation => 11,
×
176
            FormFeed => 12,
×
177
            CarriageReturn => 13,
×
178
            ShiftOut => 14,
×
179
            ShiftIn => 15,
×
180
            DataLinkEscape => 16,
×
181
            DeviceControl1 => 17,
×
182
            DeviceControl2 => 18,
×
183
            DeviceControl3 => 19,
×
184
            DeviceControl4 => 20,
×
185
            NegativeAcknowledge => 21,
×
186
            SynchronousIdle => 22,
×
187
            EndOfTransmissionBlock => 23,
×
188
            Cancel => 24,
×
189
            EndOfMedium => 25,
×
190
            Substitute => 26,
×
191
            Escape => 27,
×
192
            FileSeparator => 28,
×
193
            GroupSeparator => 29,
×
194
            RecordSeparator => 30,
×
195
            UnitSeparator => 31,
×
196
            Space => 32,
×
197
            Delete => 127,
×
198
        }
199
    }
200
}
201

202
impl TryFrom<char> for ControlCode {
203
    type Error = ();
204

205
    fn try_from(c: char) -> Result<ControlCode, ()> {
×
206
        use ControlCode::*;
207
        match c {
×
208
            '@' => Ok(Null),
×
209
            'A' | 'a' => Ok(StartOfHeading),
×
210
            'B' | 'b' => Ok(StartOfText),
×
211
            'C' | 'c' => Ok(EndOfText),
×
212
            'D' | 'd' => Ok(EndOfTransmission),
×
213
            'E' | 'e' => Ok(Enquiry),
×
214
            'F' | 'f' => Ok(Acknowledge),
×
215
            'G' | 'g' => Ok(Bell),
×
216
            'H' | 'h' => Ok(Backspace),
×
217
            'I' | 'i' => Ok(HorizontalTabulation),
×
218
            'J' | 'j' => Ok(LineFeed),
×
219
            'K' | 'k' => Ok(VerticalTabulation),
×
220
            'L' | 'l' => Ok(FormFeed),
×
221
            'M' | 'm' => Ok(CarriageReturn),
×
222
            'N' | 'n' => Ok(ShiftOut),
×
223
            'O' | 'o' => Ok(ShiftIn),
×
224
            'P' | 'p' => Ok(DataLinkEscape),
×
225
            'Q' | 'q' => Ok(DeviceControl1),
×
226
            'R' | 'r' => Ok(DeviceControl2),
×
227
            'S' | 's' => Ok(DeviceControl3),
×
228
            'T' | 't' => Ok(DeviceControl4),
×
229
            'U' | 'u' => Ok(NegativeAcknowledge),
×
230
            'V' | 'v' => Ok(SynchronousIdle),
×
231
            'W' | 'w' => Ok(EndOfTransmissionBlock),
×
232
            'X' | 'x' => Ok(Cancel),
×
233
            'Y' | 'y' => Ok(EndOfMedium),
×
234
            'Z' | 'z' => Ok(Substitute),
×
235
            '[' => Ok(Escape),
×
236
            '\\' => Ok(FileSeparator),
×
237
            ']' => Ok(GroupSeparator),
×
238
            '^' => Ok(RecordSeparator),
×
239
            '_' => Ok(UnitSeparator),
×
240
            ' ' => Ok(Space),
×
241
            '?' => Ok(Delete),
×
242
            _ => Err(()),
×
243
        }
244
    }
245
}
246

247
impl TryFrom<&str> for ControlCode {
248
    type Error = ();
249

250
    fn try_from(c: &str) -> Result<ControlCode, ()> {
×
251
        use ControlCode::*;
252
        match c {
×
253
            "^@" => Ok(Null),
×
254
            "^A" => Ok(StartOfHeading),
×
255
            "^B" => Ok(StartOfText),
×
256
            "^C" => Ok(EndOfText),
×
257
            "^D" => Ok(EndOfTransmission),
×
258
            "^E" => Ok(Enquiry),
×
259
            "^F" => Ok(Acknowledge),
×
260
            "^G" => Ok(Bell),
×
261
            "^H" => Ok(Backspace),
×
262
            "^I" => Ok(HorizontalTabulation),
×
263
            "^J" => Ok(LineFeed),
×
264
            "^K" => Ok(VerticalTabulation),
×
265
            "^L" => Ok(FormFeed),
×
266
            "^M" => Ok(CarriageReturn),
×
267
            "^N" => Ok(ShiftOut),
×
268
            "^O" => Ok(ShiftIn),
×
269
            "^P" => Ok(DataLinkEscape),
×
270
            "^Q" => Ok(DeviceControl1),
×
271
            "^R" => Ok(DeviceControl2),
×
272
            "^S" => Ok(DeviceControl3),
×
273
            "^T" => Ok(DeviceControl4),
×
274
            "^U" => Ok(NegativeAcknowledge),
×
275
            "^V" => Ok(SynchronousIdle),
×
276
            "^W" => Ok(EndOfTransmissionBlock),
×
277
            "^X" => Ok(Cancel),
×
278
            "^Y" => Ok(EndOfMedium),
×
279
            "^Z" => Ok(Substitute),
×
280
            "^[" => Ok(Escape),
×
281
            "^\\" => Ok(FileSeparator),
×
282
            "^]" => Ok(GroupSeparator),
×
283
            "^^" => Ok(RecordSeparator),
×
284
            "^_" => Ok(UnitSeparator),
×
285
            "^ " => Ok(Space),
×
286
            "^?" => Ok(Delete),
×
287
            _ => Err(()),
×
288
        }
289
    }
290
}
291

292
impl AsRef<str> for ControlCode {
293
    fn as_ref(&self) -> &str {
×
294
        use ControlCode::*;
295
        match self {
×
296
            Null => "^@",
×
297
            StartOfHeading => "^A",
×
298
            StartOfText => "^B",
×
299
            EndOfText => "^C",
×
300
            EndOfTransmission => "^D",
×
301
            Enquiry => "^E",
×
302
            Acknowledge => "^F",
×
303
            Bell => "^G",
×
304
            Backspace => "^H",
×
305
            HorizontalTabulation => "^I",
×
306
            LineFeed => "^J",
×
307
            VerticalTabulation => "^K",
×
308
            FormFeed => "^L",
×
309
            CarriageReturn => "^M",
×
310
            ShiftOut => "^N",
×
311
            ShiftIn => "^O",
×
312
            DataLinkEscape => "^P",
×
313
            DeviceControl1 => "^Q",
×
314
            DeviceControl2 => "^R",
×
315
            DeviceControl3 => "^S",
×
316
            DeviceControl4 => "^T",
×
317
            NegativeAcknowledge => "^U",
×
318
            SynchronousIdle => "^V",
×
319
            EndOfTransmissionBlock => "^W",
×
320
            Cancel => "^X",
×
321
            EndOfMedium => "^Y",
×
322
            Substitute => "^Z",
×
323
            Escape => "^[",
×
324
            FileSeparator => "^\\",
×
325
            GroupSeparator => "^]",
×
326
            RecordSeparator => "^^",
×
327
            UnitSeparator => "^_",
×
328
            Space => " ",
×
329
            Delete => "^?",
×
330
        }
331
    }
332
}
333

334
impl AsRef<[u8]> for ControlCode {
335
    fn as_ref(&self) -> &[u8] {
2✔
336
        use ControlCode::*;
337
        match self {
2✔
338
            Null => &[0],
×
339
            StartOfHeading => &[1],
×
340
            StartOfText => &[2],
×
341
            EndOfText => &[3],
1✔
342
            EndOfTransmission => &[4],
2✔
343
            Enquiry => &[5],
×
344
            Acknowledge => &[6],
×
345
            Bell => &[7],
×
346
            Backspace => &[8],
×
347
            HorizontalTabulation => &[9],
×
348
            LineFeed => &[10],
×
349
            VerticalTabulation => &[11],
×
350
            FormFeed => &[12],
×
351
            CarriageReturn => &[13],
×
352
            ShiftOut => &[14],
×
353
            ShiftIn => &[15],
×
354
            DataLinkEscape => &[16],
×
355
            DeviceControl1 => &[17],
×
356
            DeviceControl2 => &[18],
×
357
            DeviceControl3 => &[19],
×
358
            DeviceControl4 => &[20],
×
359
            NegativeAcknowledge => &[21],
×
360
            SynchronousIdle => &[22],
×
361
            EndOfTransmissionBlock => &[23],
×
362
            Cancel => &[24],
×
363
            EndOfMedium => &[25],
×
364
            Substitute => &[26],
1✔
365
            Escape => &[27],
×
366
            FileSeparator => &[28],
×
367
            GroupSeparator => &[29],
×
368
            RecordSeparator => &[30],
×
369
            UnitSeparator => &[31],
×
370
            Space => &[32],
×
371
            Delete => &[127],
×
372
        }
373
    }
374
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc