flowey/
lib.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#![expect(missing_docs)]
5#![forbid(unsafe_code)]
6
7//! The user-facing flowey API.
8//!
9//! Relying on `flowey_core` directly is not advised, as many APIs exposed at
10//! that level are only supposed to be used by flowey _infrastructure_ (e.g: in
11//! `flowey_cli`).
12
13pub use flowey_core::shell_cmd;
14
15/// Types and traits for implementing flowey nodes.
16pub mod node {
17    pub mod prelude {
18        // include all user-facing types in the prelude
19        pub use flowey_core::match_arch;
20        pub use flowey_core::node::user_facing::*;
21
22        // ...in addition, export various types/traits that node impls are
23        // almost certainly going to require
24        pub use anyhow;
25        pub use anyhow::Context;
26        pub use fs_err;
27        pub use log;
28        pub use serde::Deserialize;
29        pub use serde::Serialize;
30        pub use std::path::Path;
31        pub use std::path::PathBuf;
32
33        /// Extension trait to streamline working with [`Path`] in flowey.
34        pub trait FloweyPathExt {
35            /// Alias for [`std::path::absolute`]
36            fn absolute(&self) -> std::io::Result<PathBuf>;
37
38            /// Helper to make files executable on unix-like platforms
39            fn make_executable(&self) -> std::io::Result<()>;
40
41            /// Helper to check if a file is executable on unix-like platforms
42            fn is_executable(&self) -> std::io::Result<bool>;
43        }
44
45        impl<T> FloweyPathExt for T
46        where
47            T: AsRef<Path>,
48        {
49            fn absolute(&self) -> std::io::Result<PathBuf> {
50                std::path::absolute(self)
51            }
52
53            fn make_executable(&self) -> std::io::Result<()> {
54                #[cfg(unix)]
55                {
56                    use std::os::unix::fs::PermissionsExt;
57                    let path = self.as_ref();
58                    let old_mode = path.metadata()?.permissions().mode();
59                    fs_err::set_permissions(
60                        path,
61                        std::fs::Permissions::from_mode(old_mode | 0o111),
62                    )?;
63                }
64                Ok(())
65            }
66
67            fn is_executable(&self) -> std::io::Result<bool> {
68                #[cfg(unix)]
69                {
70                    use std::os::unix::fs::PermissionsExt;
71                    let path = self.as_ref();
72                    let mode = path.metadata()?.permissions().mode();
73                    Ok(mode & 0o111 != 0)
74                }
75
76                // TODO: Implement for windows. Tracked by https://github.com/microsoft/openvmm/issues/2622
77                #[cfg(not(unix))]
78                {
79                    Ok(true)
80                }
81            }
82        }
83    }
84}
85
86/// Types and traits for implementing flowey pipelines.
87pub mod pipeline {
88    pub mod prelude {
89        pub use flowey_core::pipeline::user_facing::*;
90    }
91}
92
93/// Types and traits for implementing flowey patch functions.
94pub mod patch {
95    pub use flowey_core::patch::*;
96    pub use flowey_core::register_patch;
97}
98
99/// Utility functions.
100pub mod util {
101    pub use flowey_core::util::*;
102}