neotron_api/file.rs
1//! File related types
2
3// ============================================================================
4// Imports
5// ============================================================================
6
7use bitflags::bitflags;
8
9// ============================================================================
10// Constants
11// ============================================================================
12
13// None
14
15// ============================================================================
16// Types
17// ============================================================================
18
19/// Represents an open file
20#[repr(C)]
21#[derive(Copy, Clone, Debug, PartialEq, Eq)]
22pub struct Handle(u8);
23
24impl Handle {
25 /// The magic file ID for Standard Input
26 const STDIN: u8 = 0;
27
28 /// The magic file ID for Standard Output
29 const STDOUT: u8 = 1;
30
31 /// The magic file ID for Standard Error
32 const STDERR: u8 = 2;
33
34 /// Construct a new `Handle` from an integer.
35 ///
36 /// Only the OS should call this - applications should not be constructing
37 /// their own file handles! But if you do, you probably can't harm anything
38 /// and it's no worse that C just using `int`.
39 pub const fn new(value: u8) -> Handle {
40 Handle(value)
41 }
42
43 /// Create a file handle for Standard Input
44 pub const fn new_stdin() -> Handle {
45 Handle(Self::STDIN)
46 }
47
48 /// Create a file handle for Standard Output
49 pub const fn new_stdout() -> Handle {
50 Handle(Self::STDOUT)
51 }
52
53 /// Create a file handle for Standard Error
54 pub const fn new_stderr() -> Handle {
55 Handle(Self::STDERR)
56 }
57
58 /// Get the numeric value of this File Handle
59 pub const fn value(&self) -> u8 {
60 self.0
61 }
62}
63
64/// Describes a file on disk.
65///
66/// This is set up for 8.3 filenames on MS-DOS FAT32 partitions currently.
67#[repr(C)]
68#[derive(Clone, Debug, PartialEq, Eq)]
69pub struct Stat {
70 /// How big is this file
71 pub file_size: u64,
72 /// When was the file created
73 pub ctime: Time,
74 /// When was the last modified
75 pub mtime: Time,
76 /// File attributes (Directory, Volume, etc)
77 pub attr: Attributes,
78}
79
80bitflags! {
81 #[repr(C)]
82 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
83 /// Describes the attributes of a file.
84 pub struct Flags: u8 {
85 /// Enable write support for this file.
86 const WRITE = 0x01;
87 /// Create the file if it doesn't exist.
88 const CREATE = 0x02;
89 /// Truncate the file to zero length upon opening.
90 const TRUNCATE = 0x04;
91 }
92}
93
94bitflags! {
95 #[repr(C)]
96 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
97 /// The attributes a file on disk can have.alloc
98 ///
99 /// Based on that supported by the FAT32 file system.
100 pub struct Attributes: u8 {
101 /// File is read-only
102 const READ_ONLY = 0x01;
103 /// File should not appear in directory listing
104 const HIDDEN = 0x02;
105 /// File should not be moved on disk
106 const SYSTEM = 0x04;
107 /// File is a volume label
108 const VOLUME = 0x08;
109 /// File is a directory
110 const DIRECTORY = 0x10;
111 /// File has not been backed up
112 const ARCHIVE = 0x20;
113 /// File is actually a device
114 const DEVICE = 0x40;
115 }
116}
117
118/// Represents an instant in time, in the local time zone.
119#[repr(C)]
120#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
121pub struct Time {
122 /// Add 1970 to this file to get the calendar year
123 pub year_since_1970: u8,
124 /// Add one to this value to get the calendar month
125 pub zero_indexed_month: u8,
126 /// Add one to this value to get the calendar day
127 pub zero_indexed_day: u8,
128 /// The number of hours past midnight
129 pub hours: u8,
130 /// The number of minutes past the hour
131 pub minutes: u8,
132 /// The number of seconds past the minute
133 pub seconds: u8,
134}
135
136// ============================================================================
137// Functions
138// ============================================================================
139
140// None
141
142// ============================================================================
143// Tests
144// ============================================================================
145
146// None
147
148// ============================================================================
149// End of File
150// ============================================================================