File_system/Fundamentals/
Flags.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
use std::fmt::Debug;

use super::Permission_type;

/// The mode of a file.
///
/// The mode is stored in a 8-bit integer, with the following layout:
///
/// | Read | Write |
/// |------|-------|
/// | 0    | 1     |
///
/// # Example
///
/// ```rust
/// use File_system::Mode_type;
///
/// let Mode = Mode_type::New(true, false);
///
/// assert_eq!(Mode.Get_read(), true);
/// assert_eq!(Mode.Get_write(), false);
///
/// let Mode = Mode_type::New(false, true);
///
/// assert_eq!(Mode.Get_read(), false);
/// assert_eq!(Mode.Get_write(), true);
///
/// let Mode = Mode_type::New(true, true);
///
/// assert_eq!(Mode.Get_read(), true);
/// assert_eq!(Mode.Get_write(), true);
/// ```
#[derive(PartialEq, Eq, Clone, Copy)]
#[repr(transparent)]
pub struct Mode_type(u8);

impl Mode_type {
    pub const Read_bit: u8 = 1 << 0;
    pub const Write_bit: u8 = 1 << 1;

    pub const Size: u8 = 2;

    pub const Read_only: Self = Self::New(true, false);
    pub const Write_only: Self = Self::New(false, true);
    pub const Read_write: Self = Self::New(true, true);

    pub const fn New(Read: bool, Write: bool) -> Self {
        Self(0).Set_read(Read).Set_write(Write)
    }

    pub const fn Set_bit(mut self, Mask: u8, Value: bool) -> Self {
        if Value {
            self.0 |= Mask;
        } else {
            self.0 &= !Mask;
        }
        self
    }

    pub const fn Set_read(self, Value: bool) -> Self {
        self.Set_bit(Self::Read_bit, Value)
    }

    pub const fn Set_write(self, Value: bool) -> Self {
        self.Set_bit(Self::Write_bit, Value)
    }

    pub const fn Get_bit(&self, Mask: u8) -> bool {
        self.0 & Mask != 0
    }

    pub const fn Get_read(&self) -> bool {
        self.Get_bit(Self::Read_bit)
    }

    pub const fn Get_write(&self) -> bool {
        self.Get_bit(Self::Write_bit)
    }

    pub const fn From_u8(Value: u8) -> Self {
        Self(Value)
    }

    pub const fn As_u8(&self) -> u8 {
        self.0
    }
}

impl Debug for Mode_type {
    fn fmt(&self, Formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Formatter
            .debug_struct("Mode_type")
            .field("Read", &self.Get_read())
            .field("Write", &self.Get_write())
            .finish()
    }
}

/// The type of opening a file.
///
/// The type is stored in a 8-bit integer, with the following layout:
///
/// | Create | Create exclusive | Truncate | Directory |
/// |--------|------------------|----------|-----------|
/// | 0      | 1                | 2        | 3         |
///
/// # Example
///
/// ```rust
/// use File_system::Open_type;
///
/// let Open = Open_type::New(true, true, false);
///
/// assert_eq!(Open.Get_create(), true);
/// assert_eq!(Open.Get_exclusive(), true);
/// assert_eq!(Open.Get_truncate(), false);
/// ```
#[derive(PartialEq, Eq, Clone, Copy)]
#[repr(transparent)]
pub struct Open_type(u8);

impl Open_type {
    pub const Create_mask: u8 = 1 << 0;
    pub const Exclusive_mask: u8 = 1 << 1;
    pub const Truncate_mask: u8 = 1 << 2;

    pub const Size: u8 = 3;

    pub const None: Self = Self::New(false, false, false);

    pub const Create: Self = Self::New(true, false, false);
    pub const Create_only: Self = Self::New(true, true, false);
    pub const Truncate: Self = Self::New(false, false, true);

    pub const fn New(Create: bool, Create_only: bool, Truncate: bool) -> Self {
        Self(0)
            .Set_create(Create)
            .Set_exclusive(Create_only)
            .Set_truncate(Truncate)
    }

    pub const fn Get_bit(&self, Mask: u8) -> bool {
        self.0 & Mask != 0
    }

    pub const fn Set_bit(mut self, Mask: u8, Value: bool) -> Self {
        if Value {
            self.0 |= Mask;
        } else {
            self.0 &= !Mask;
        }
        self
    }

    pub const fn Get_create(&self) -> bool {
        self.Get_bit(Self::Create_mask)
    }

    pub const fn Set_create(self, Value: bool) -> Self {
        self.Set_bit(Self::Create_mask, Value)
    }

    pub const fn Get_exclusive(&self) -> bool {
        self.Get_bit(Self::Exclusive_mask)
    }

    pub const fn Set_exclusive(self, Value: bool) -> Self {
        self.Set_bit(Self::Exclusive_mask, Value)
    }

    pub const fn Get_truncate(&self) -> bool {
        self.Get_bit(Self::Truncate_mask)
    }

    pub const fn Set_truncate(self, Value: bool) -> Self {
        self.Set_bit(Self::Truncate_mask, Value)
    }

    pub const fn From_u8(Value: u8) -> Self {
        Self(Value)
    }
}

impl Default for Open_type {
    fn default() -> Self {
        Self::None
    }
}

impl Debug for Open_type {
    fn fmt(&self, Formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Formatter
            .debug_struct("Open_type")
            .field("Create", &self.Get_create())
            .field("Create_only", &self.Get_exclusive())
            .field("Truncate", &self.Get_truncate())
            .finish()
    }
}

/// The status of a file.
///
/// The status is stored in a 8-bit integer, with the following layout:
///
/// | Append | Non-blocking | Synchronous | Synchronous data only |
///  -------------------------------------------------------------
/// | 0      | 1            | 2           | 3                     |
///
/// # Example
///
/// ```rust
/// use File_system::Status_type;
///
/// let Status = Status_type::New(true, false, true, false);
///
/// assert_eq!(Status.Get_append(), true);
/// assert_eq!(Status.Get_non_blocking(), false);
/// assert_eq!(Status.Get_synchronous(), true);
/// assert_eq!(Status.Get_synchronous_data_only(), false);
/// ```
#[derive(PartialEq, Eq, Clone, Copy)]
#[repr(transparent)]
pub struct Status_type(u8);

impl Status_type {
    pub const Append_bit: u8 = 1 << 0;
    pub const Non_blocking_bit: u8 = 1 << 1;
    pub const Synchronous_bit: u8 = 1 << 2;
    pub const Synchronous_data_only_bit: u8 = 1 << 3;

    pub const Size: u8 = 4;

    pub const Non_blocking: Self = Self::New(false, true, false, false);

    pub const None: Self = Self::New(false, false, false, false);

    pub const fn New(
        Append: bool,
        Non_blocking: bool,
        Synchronous: bool,
        Synchronous_data_only: bool,
    ) -> Self {
        Self(0)
            .Set_append(Append)
            .Set_non_blocking(Non_blocking)
            .Set_synchronous(Synchronous)
            .Set_synchronous_data_only(Synchronous_data_only)
    }

    const fn Set_bit(mut self, Mask: u8, Value: bool) -> Self {
        if Value {
            self.0 |= Mask;
        } else {
            self.0 &= !Mask;
        }
        self
    }

    const fn Get_bit(&self, Mask: u8) -> bool {
        self.0 & Mask != 0
    }

    pub const fn Set_non_blocking(self, Value: bool) -> Self {
        self.Set_bit(Self::Non_blocking_bit, Value)
    }

    pub fn Get_non_blocking(&self) -> bool {
        self.Get_bit(Self::Non_blocking_bit)
    }

    pub const fn Set_append(self, Value: bool) -> Self {
        self.Set_bit(Self::Append_bit, Value)
    }

    pub const fn Get_append(&self) -> bool {
        self.Get_bit(Self::Append_bit)
    }

    pub const fn Set_synchronous(self, Value: bool) -> Self {
        self.Set_bit(Self::Synchronous_bit, Value)
    }

    pub const fn Get_synchronous(&self) -> bool {
        self.Get_bit(Self::Synchronous_bit)
    }

    pub const fn Set_synchronous_data_only(self, Value: bool) -> Self {
        self.Set_bit(Self::Synchronous_data_only_bit, Value)
    }

    pub const fn Get_synchronous_data_only(&self) -> bool {
        self.Get_bit(Self::Synchronous_data_only_bit)
    }

    pub const fn From_u8(Value: u8) -> Self {
        Self(Value)
    }
}

impl Debug for Status_type {
    fn fmt(&self, Formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Formatter
            .debug_struct("Status_type")
            .field("Append", &self.Get_append())
            .field("Non_blocking", &self.Get_non_blocking())
            .field("Synchronous", &self.Get_bit(Self::Synchronous_bit))
            .field(
                "Synchronous_data_only",
                &self.Get_bit(Self::Synchronous_data_only_bit),
            )
            .finish()
    }
}

impl Default for Status_type {
    fn default() -> Self {
        Self::None
    }
}

/// All the flags that can be set for a file.
///
/// The flags are stored in a 16-bit integer, with the following layout:
///
/// | Mode | Open | Status |
/// |------|------|--------|
/// | 0-1  | 2-5  | 6-9    |
///
/// # Example
///
/// ```rust
/// use File_system::{Flags_type, Mode_type, Open_type, Status_type};
///     
/// let Flags = Flags_type::New(Mode_type::Read_write, Some(Open_type::Create_only), Some(Status_type::Non_blocking));
///
/// assert_eq!(Flags.Get_mode(), Mode_type::Read_write);
/// assert_eq!(Flags.Get_open(), Open_type::Create_only);
/// assert_eq!(Flags.Get_status(), Status_type::Non_blocking);
/// ```
#[derive(PartialEq, Eq, Clone, Copy)]
#[repr(transparent)]
pub struct Flags_type(u16);

impl Debug for Flags_type {
    fn fmt(&self, Formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Formatter
            .debug_struct("Flags_type")
            .field("Mode", &self.Get_mode())
            .field("Open", &self.Get_open())
            .field("Status", &self.Get_status())
            .finish()
    }
}

impl Flags_type {
    const Mode_position: u8 = 0;
    const Open_position: u8 = Mode_type::Size;
    const Status_position: u8 = Open_type::Size + Self::Open_position;

    const Open_mask: u16 = (1 << Open_type::Size) - 1;
    const Status_mask: u16 = (1 << Status_type::Size) - 1;
    const Mode_mask: u16 = (1 << Mode_type::Size) - 1;

    pub const fn New(
        Mode: Mode_type,
        Open: Option<Open_type>,
        Status: Option<Status_type>,
    ) -> Self {
        let Open = if let Some(Open) = Open {
            Open
        } else {
            Open_type::None
        };
        let Status = if let Some(Status) = Status {
            Status
        } else {
            Status_type::None
        };

        let mut Flags: u16 = 0;
        Flags |= (Mode.0 as u16) << Self::Mode_position;
        Flags |= (Open.0 as u16) << Self::Open_position;
        Flags |= (Status.0 as u16) << Self::Status_position;
        Self(Flags)
    }

    pub const fn Get_mode(&self) -> Mode_type {
        Mode_type(((self.0 >> Self::Mode_position) & Self::Mode_mask) as u8)
    }

    pub const fn Get_open(&self) -> Open_type {
        Open_type(((self.0 >> Self::Open_position) & Self::Open_mask) as u8)
    }

    pub const fn Get_status(&self) -> Status_type {
        Status_type(((self.0 >> Self::Status_position) & Self::Status_mask) as u8)
    }

    pub const fn Set_mode(mut self, Mode: Mode_type) -> Self {
        self.0 &= !(Self::Mode_mask << Self::Mode_position);
        self.0 |= (Mode.0 as u16) << Self::Mode_position;
        self
    }

    pub const fn Set_open(mut self, Open: Open_type) -> Self {
        self.0 &= !(Self::Open_mask << Self::Open_position);
        self.0 |= (Open.0 as u16) << Self::Open_position;
        self
    }

    pub const fn Set_status(mut self, Status: Status_type) -> Self {
        self.0 &= !(Self::Status_mask << Self::Status_position);
        self.0 |= (Status.0 as u16) << Self::Status_position;
        self
    }

    pub fn Is_permission_granted(&self, Permission: &Permission_type) -> bool {
        let Mode = self.Get_mode();

        (Permission.Get_read() && Mode.Get_read()) // Read permission
            || (Permission.Get_write() && (Mode.Get_write() || self.Get_status().Get_append()))
        // Write permission
    }
}

impl From<Mode_type> for Flags_type {
    fn from(Mode: Mode_type) -> Self {
        Self::New(Mode, None, None)
    }
}

impl From<Flags_type> for u16 {
    fn from(Flags: Flags_type) -> Self {
        Flags.0
    }
}

#[cfg(test)]
mod Tests {
    use super::*;

    #[test]
    fn Test_mode_type_new() {
        let Read_only = Mode_type::New(true, false);
        assert!(Read_only.Get_read());
        assert!(!Read_only.Get_write());

        let Write_only = Mode_type::New(false, true);
        assert!(!Write_only.Get_read());
        assert!(Write_only.Get_write());

        let Read_write = Mode_type::New(true, true);
        assert!(Read_write.Get_read());
        assert!(Read_write.Get_write());
    }

    #[test]
    fn Test_mode_type_set_get() {
        let mut Mode = Mode_type(0);
        Mode = Mode.Set_read(true);
        assert!(Mode.Get_read());
        assert!(!Mode.Get_write());

        Mode = Mode.Set_write(true);
        assert!(Mode.Get_read());
        assert!(Mode.Get_write());

        Mode = Mode.Set_read(false);
        assert!(!Mode.Get_read());
        assert!(Mode.Get_write());
    }

    #[test]
    fn Test_open_type_new() {
        let Open = Open_type::New(true, false, true);
        assert!(Open.Get_create());
        assert!(!Open.Get_exclusive());
        assert!(Open.Get_truncate());
    }

    #[test]
    fn Test_open_type_set_get() {
        let mut Open = Open_type(0);
        Open = Open.Set_create(true);
        assert!(Open.Get_create());
        assert!(!Open.Get_exclusive());

        Open = Open.Set_exclusive(true);
        assert!(Open.Get_create());
        assert!(Open.Get_exclusive());

        Open = Open.Set_truncate(true);
        assert!(Open.Get_truncate());
    }

    #[test]
    fn Test_status_type_new() {
        let Status = Status_type::New(true, false, true, false);
        assert!(Status.Get_append());
        assert!(!Status.Get_non_blocking());
        assert!(Status.Get_synchronous());
        assert!(!Status.Get_synchronous_data_only());
    }

    #[test]
    fn Test_status_type_set_get() {
        let mut Status = Status_type(0);
        Status = Status.Set_append(true);
        assert!(Status.Get_append());
        assert!(!Status.Get_non_blocking());

        Status = Status.Set_non_blocking(true);
        assert!(Status.Get_non_blocking());

        Status = Status.Set_synchronous(true);
        assert!(Status.Get_synchronous());

        Status = Status.Set_synchronous_data_only(true);
        assert!(Status.Get_synchronous_data_only());
    }

    #[test]
    fn Test_flags_type_new() {
        let Mode = Mode_type::Read_write;
        let Open = Open_type::New(true, false, true);
        let Status = Status_type::New(true, false, true, false);

        let Flags = Flags_type::New(Mode, Some(Open), Some(Status));
        assert_eq!(Flags.Get_mode(), Mode);
        assert_eq!(Flags.Get_open(), Open);
        assert_eq!(Flags.Get_status(), Status);
    }

    #[test]
    fn Test_flags_type_set_get() {
        let Flags = Flags_type::New(Mode_type::Read_only, None, None);

        let New_mode = Mode_type::Write_only;
        let Flags = Flags.Set_mode(New_mode);
        assert_eq!(Flags.Get_mode(), New_mode);

        let New_open = Open_type::New(true, true, false);
        let Flags = Flags.Set_open(New_open);
        assert_eq!(Flags.Get_open(), New_open);

        let New_status = Status_type::New(false, true, false, true);
        let Flags = Flags.Set_status(New_status);
        assert_eq!(Flags.Get_status(), New_status);
    }

    #[test]
    fn Test_flags_type_is_permission_granted() {
        let Mode = Mode_type::Read_write;
        let Status = Status_type::New(true, false, false, false);
        let Flags = Flags_type::New(Mode, None, Some(Status));

        assert!(Flags.Is_permission_granted(&Permission_type::Read_only));
        assert!(Flags.Is_permission_granted(&Permission_type::Write_only));
        assert!(Flags.Is_permission_granted(&Permission_type::Read_write));
    }

    #[test]
    fn Test_flags_type_from_mode_type() {
        let Mode = Mode_type::Read_write;
        let Flags: Flags_type = Mode.into();
        assert_eq!(Flags.Get_mode(), Mode);
    }

    #[test]
    fn Test_flags_type_into_u16() {
        let Mode = Mode_type::Read_write;
        let Flags = Flags_type::New(Mode, None, None);
        let Flags_u16: u16 = Flags.into();
        assert_eq!(Flags_u16, Flags.0);
    }
}