Skip to main content

libutils_terminal/
mod.rs

1//^
2//^ HEAD
3//^
4
5//> HEAD -> FEATURES
6#![feature(const_trait_impl)]
7#![feature(const_default)]
8#![feature(transmute_neo)]
9
10//> HEAD -> MODULES
11mod descriptor;
12mod metadata;
13mod problem;
14mod section;
15#[cfg(test)]
16mod tests;
17mod update;
18
19//> HEAD -> STD
20use std::{
21    env::args, 
22    fs::File, 
23    path::PathBuf, 
24    sync::LazyLock, 
25    time::Instant
26};
27
28//> HEAD -> CORE
29use core::fmt::{
30    Debug, 
31    Display
32};
33
34//> HEAD -> CAGELOCK
35use cagelock::Cage;
36
37//> HEAD -> UPDATE
38use update::Update;
39
40//> HEAD -> ISSUE
41use libutils_issue::{
42    Issue,
43    Severity
44};
45
46//> HEAD -> CONSOLE
47use libutils_console::{
48    Console,
49    Argument,
50    Update as UpdateTrait,
51    Descriptor as DescriptorTrait
52};
53
54//> HEAD -> SECTION
55use section::Section;
56
57//> HEAD -> PROBLEM
58use problem::Problem;
59
60//> HEAD -> DESCRIPTOR
61use descriptor::Descriptor;
62
63
64//^
65//^ TERMINAL
66//^
67
68//> TERMINAL -> DATA
69static ARGUMENTS: LazyLock<Vec<Argument>> = LazyLock::new(|| args().map(Argument::from).collect());
70static LAYOUT: Cage<Vec<Section>> = Cage::default();
71static OUTPUT: Cage<String> = Cage::default();
72
73//> TERMINAL -> STRUCT
74pub struct Terminal;
75
76//> TERMINAL -> IMPLEMENTATION
77impl Console for Terminal {
78    fn arguments() -> &'static [Argument] {return ARGUMENTS.as_slice()}
79    fn open(filename: &str) -> Result<impl DescriptorTrait, Issue> {
80        match File::open(PathBuf::from(filename)) {
81            Ok(file) => Ok(Descriptor {
82                file: file
83            }),
84            Err(error) => Err(Issue {
85                name: "Failed to open file",
86                description: Some(error.to_string()),
87                severity: Severity::Error
88            })
89        }
90    }
91    fn problem(issue: Issue, chain: &[&'static str]) -> impl UpdateTrait {
92        LAYOUT.write(|layout| layout.push(Section::Problem(Problem {
93            chain: Vec::from(chain),
94            issue: issue,
95            _at: Instant::now()
96        })));
97        return Update;
98    }
99    fn print(value: impl Display) -> impl UpdateTrait {
100        LAYOUT.write(|layout| layout.push(Section::Display(value.to_string())));
101        return Update;
102    }
103    fn debug(value: impl Debug) -> impl UpdateTrait {
104        LAYOUT.write(|layout| layout.push(Section::Debug(format!("{value:#?}"))));
105        return Update;
106    }
107}