arachnid_cli/config/kinds/
scope.rs1use std::path::PathBuf;
6
7fn _default_context() -> Option<String> {
8 Some(".".to_string())
9}
10
11fn _default_workdir() -> String {
12 std::env::current_dir()
13 .map(|path| path.display().to_string())
14 .unwrap_or_else(|_| ".".to_string())
15}
16
17#[derive(
22 Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize,
23)]
24#[serde(default, deny_unknown_fields, rename_all = "snake_case")]
25#[repr(C)]
26pub struct Scope {
27 pub(crate) context: Option<String>,
29 #[serde(default = "_default_workdir")]
31 pub(crate) workdir: String,
32}
33
34impl Scope {
35 pub fn from_workdir<T>(workdir: T) -> Self
36 where
37 T: ToString,
38 {
39 Self {
40 context: None,
41 workdir: workdir.to_string(),
42 }
43 }
44 pub fn context(&self) -> Option<&str> {
46 self.context.as_deref()
47 }
48 pub fn context_mut(&mut self) -> Option<&mut String> {
50 self.context.as_mut()
51 }
52 pub fn workdir(&self) -> &str {
54 &self.workdir
55 }
56 pub fn as_path(&self) -> PathBuf {
58 let mut path = PathBuf::new();
60 self.context().clone().map(|context| path.push(context));
62 path.push(self.workdir());
64 debug_assert!(path.is_dir());
66 path
68 }
69 pub fn as_path_str(&self) -> String {
71 self.as_path().display().to_string()
72 }
73 pub fn contextless(self) -> Self {
75 Self {
76 context: None,
77 ..self
78 }
79 }
80 pub fn set_cwd(&self) {
82 std::env::set_current_dir(self.as_path()).unwrap();
83 }
84 pub fn set_context<T>(&mut self, context: T)
86 where
87 T: ToString,
88 {
89 self.context = Some(context.to_string())
90 }
91 pub fn set_workdir<T>(&mut self, workdir: T)
93 where
94 T: ToString,
95 {
96 self.workdir = workdir.to_string()
97 }
98 pub fn display(&self) -> String {
100 self.as_path().display().to_string()
101 }
102}
103
104impl Default for Scope {
105 fn default() -> Self {
106 Self {
107 context: None,
108 workdir: ".".into(),
109 }
110 }
111}
112
113impl core::fmt::Display for Scope {
114 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
115 write!(f, "{path}", path = self.display())
116 }
117}
118
119impl core::str::FromStr for Scope {
120 type Err = std::io::Error;
121
122 fn from_str(s: &str) -> Result<Self, Self::Err> {
123 Ok(Self::from_workdir(s))
124 }
125}