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
use super::{
    ast::{AstSerdeOptions, ModuleAst},
    ByteReader, ByteWriter, Deserializable, DeserializationError, LibraryError, PathError,
    Serializable, MAX_LABEL_LEN, NAMESPACE_LABEL_PARSER,
};
use crate::utils::string::*;
use core::{cmp::Ordering, fmt, ops::Deref, str::from_utf8};

mod masl;
pub use masl::MaslLibrary;

mod path;
pub use path::LibraryPath;

#[cfg(test)]
mod tests;

/// Maximum number of modules in a library.
const MAX_MODULES: usize = u16::MAX as usize;

/// Maximum number of dependencies in a library.
const MAX_DEPENDENCIES: usize = u16::MAX as usize;

// LIBRARY
// ================================================================================================

/// A library definition that provides AST modules for the compilation process.
pub trait Library {
    /// The concrete type used to iterate the modules of the library.
    type ModuleIterator<'a>: Iterator<Item = &'a Module>
    where
        Self: 'a;

    /// Returns the root namespace of this library.
    fn root_ns(&self) -> &LibraryNamespace;

    /// Returns the version number of this library.
    fn version(&self) -> &Version;

    /// Iterate the modules available in the library.
    fn modules(&self) -> Self::ModuleIterator<'_>;

    /// Returns the dependency libraries of this library.
    fn dependencies(&self) -> &[LibraryNamespace];

    /// Returns the AST of the module stored at the provided path.
    fn get_module_ast(&self, path: &LibraryPath) -> Option<&ModuleAst> {
        self.modules().find(|&module| module.path == *path).map(|module| &module.ast)
    }
}

impl<T> Library for &T
where
    T: Library,
{
    type ModuleIterator<'a> = T::ModuleIterator<'a>
    where
        Self: 'a;

    fn root_ns(&self) -> &LibraryNamespace {
        T::root_ns(self)
    }

    fn version(&self) -> &Version {
        T::version(self)
    }

    fn modules(&self) -> Self::ModuleIterator<'_> {
        T::modules(self)
    }

    fn dependencies(&self) -> &[LibraryNamespace] {
        T::dependencies(self)
    }

    fn get_module_ast(&self, path: &LibraryPath) -> Option<&ModuleAst> {
        T::get_module_ast(self, path)
    }
}

// MODULE
// ================================================================================================

/// A module containing its absolute path and parsed AST.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Module {
    /// Absolute path of the module.
    pub path: LibraryPath,
    /// Parsed AST of the module.
    pub ast: ModuleAst,
}

impl Module {
    // CONSTRUCTORS
    // --------------------------------------------------------------------------------------------

    /// Create a new module from a path and ast.
    pub const fn new(path: LibraryPath, ast: ModuleAst) -> Self {
        Self { path, ast }
    }

    /// Create a new kernel module from a AST using the constant [LibraryPath::kernel_path].
    pub fn kernel(ast: ModuleAst) -> Self {
        Self {
            path: LibraryPath::kernel_path(),
            ast,
        }
    }

    // VALIDATIONS
    // --------------------------------------------------------------------------------------------

    /// Validate if the module belongs to the provided namespace.
    pub fn check_namespace(&self, namespace: &LibraryNamespace) -> Result<(), LibraryError> {
        (self.path.first() == namespace.as_str()).then_some(()).ok_or_else(|| {
            LibraryError::inconsistent_namespace(self.path.first(), namespace.as_str())
        })
    }

    // STATE MUTATORS
    // --------------------------------------------------------------------------------------------

    /// Clears the source locations from this module.
    pub fn clear_locations(&mut self) {
        self.ast.clear_locations()
    }

    // SERIALIZATION / DESERIALIZATION
    // --------------------------------------------------------------------------------------------

    /// Loads the [SourceLocation] of the procedures via [ModuleAst::load_source_locations].
    pub fn load_source_locations<R: ByteReader>(
        &mut self,
        source: &mut R,
    ) -> Result<(), DeserializationError> {
        self.ast.load_source_locations(source)
    }

    /// Writes the [SourceLocation] of the procedures via [ModuleAst::write_source_locations].
    pub fn write_source_locations<W: ByteWriter>(&self, target: &mut W) {
        self.ast.write_source_locations(target)
    }

    /// Serialization of [Module] via [LibraryPath::write_into] and
    /// [ModuleAst::write_into]. [AstSerdeOptions] are used to direct serialization of [ModuleAst].
    pub fn write_into<W: ByteWriter>(&self, target: &mut W, options: AstSerdeOptions) {
        self.path.write_into(target);
        self.ast.write_into(target, options);
    }

    /// Deserialization of [Module] via [LibraryPath::read_from] and
    /// [ModuleAst::read_from]. [AstSerdeOptions] are used to direct deserialization of [ModuleAst].
    pub fn read_from<R: ByteReader>(
        source: &mut R,
        options: AstSerdeOptions,
    ) -> Result<Self, DeserializationError> {
        let path = LibraryPath::read_from(source)?;
        let ast = ModuleAst::read_from(source, options)?;
        Ok(Self { path, ast })
    }
}

impl PartialOrd for Module {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Module {
    fn cmp(&self, other: &Self) -> Ordering {
        self.path.cmp(&other.path)
    }
}

// VERSION
// ================================================================================================

#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Version {
    pub major: u16,
    pub minor: u16,
    pub patch: u16,
}

impl Version {
    // CONSTANTS
    // --------------------------------------------------------------------------------------------

    /// Minimal version
    pub const MIN: Self = Self {
        major: 0,
        minor: 1,
        patch: 0,
    };

    // COMPARISON HELPERS
    // --------------------------------------------------------------------------------------------

    /// Compare two versions considering only the major value.
    pub fn cmp_major(&self, other: &Self) -> Ordering {
        self.major.cmp(&other.major)
    }

    /// Compare two versions considering only the major and minor values.
    pub fn cmp_minor(&self, other: &Self) -> Ordering {
        match self.cmp_major(other) {
            Ordering::Less => Ordering::Less,
            Ordering::Equal => self.minor.cmp(&other.minor),
            Ordering::Greater => Ordering::Greater,
        }
    }

    /// Compare two versions considering the major, minor, and patch values.
    pub fn cmp_patch(&self, other: &Self) -> Ordering {
        match self.cmp_minor(other) {
            Ordering::Less => Ordering::Less,
            Ordering::Equal => self.patch.cmp(&other.patch),
            Ordering::Greater => Ordering::Greater,
        }
    }

    // INCREMENT HELPERS
    // --------------------------------------------------------------------------------------------

    /// Increment the major version value.
    pub const fn inc_major(self) -> Self {
        Self {
            major: self.major + 1,
            minor: 0,
            patch: 0,
        }
    }

    /// Increment the minor version value.
    pub const fn inc_minor(self) -> Self {
        Self {
            major: self.major,
            minor: self.minor + 1,
            patch: 0,
        }
    }

    /// Increment the patch version value.
    pub const fn inc_patch(self) -> Self {
        Self {
            major: self.major,
            minor: self.minor,
            patch: self.patch + 1,
        }
    }
}

impl Default for Version {
    fn default() -> Self {
        Self::MIN
    }
}

impl Serializable for Version {
    fn write_into<W: ByteWriter>(&self, target: &mut W) {
        target.write_u16(self.major);
        target.write_u16(self.minor);
        target.write_u16(self.patch);
    }
}

impl Deserializable for Version {
    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
        let major = source.read_u16()?;
        let minor = source.read_u16()?;
        let patch = source.read_u16()?;
        Ok(Self {
            major,
            minor,
            patch,
        })
    }
}

impl fmt::Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
    }
}

impl TryFrom<&str> for Version {
    type Error = LibraryError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        let mut components = value.split('.');

        let major = components
            .next()
            .ok_or(LibraryError::missing_version_component(value, "major"))?
            .parse::<u16>()
            .map_err(|err| LibraryError::invalid_version_number(value, err.to_string()))?;
        let minor = components
            .next()
            .ok_or(LibraryError::missing_version_component(value, "minor"))?
            .parse::<u16>()
            .map_err(|err| LibraryError::invalid_version_number(value, err.to_string()))?;
        let patch = components
            .next()
            .ok_or(LibraryError::missing_version_component(value, "patch"))?
            .parse::<u16>()
            .map_err(|err| LibraryError::invalid_version_number(value, err.to_string()))?;

        if components.next().is_some() {
            Err(LibraryError::too_many_version_components(value))
        } else {
            Ok(Self {
                major,
                minor,
                patch,
            })
        }
    }
}

// LIBRARY NAMESPACE
// ================================================================================================

/// Library namespace.
///
/// Will be `std` in the absolute procedure name `std::foo::bar::baz`.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LibraryNamespace {
    name: String,
}

impl LibraryNamespace {
    /// Returns an new [LibraryNamespace] instantiated from the provided source.
    ///
    /// # Errors
    /// Returns an error if:
    /// - The source string is empty or requires more than 255 bytes to serialize.
    /// - Does not start with a ASCII letter.
    /// - Contains characters other than ASCII letters, numbers, and underscores.
    pub fn new<S>(source: S) -> Result<Self, LibraryError>
    where
        S: AsRef<str>,
    {
        let name = NAMESPACE_LABEL_PARSER
            .parse_label(source.as_ref())
            .map_err(LibraryError::invalid_namespace)?;
        Ok(Self {
            name: name.to_string(),
        })
    }
}

impl TryFrom<String> for LibraryNamespace {
    type Error = LibraryError;

    fn try_from(name: String) -> Result<Self, Self::Error> {
        Self::new(name)
    }
}

impl Deref for LibraryNamespace {
    type Target = String;

    fn deref(&self) -> &Self::Target {
        &self.name
    }
}

impl AsRef<str> for LibraryNamespace {
    fn as_ref(&self) -> &str {
        &self.name
    }
}

impl Serializable for LibraryNamespace {
    fn write_into<W: ByteWriter>(&self, target: &mut W) {
        // this assertion should pass because library namespace constructor enforces max allowed
        // length at 255 bytes
        debug_assert!(self.name.len() <= u8::MAX as usize, "namespace too long");
        target.write_u8(self.name.len() as u8);
        target.write_bytes(self.name.as_bytes());
    }
}

impl Deserializable for LibraryNamespace {
    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
        let num_bytes = source.read_u8()? as usize;
        let name = source.read_vec(num_bytes)?;
        let name =
            from_utf8(&name).map_err(|e| DeserializationError::InvalidValue(e.to_string()))?;
        Self::new(name).map_err(|e| DeserializationError::InvalidValue(e.to_string()))
    }
}