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