mini_builder_rs/builder/
targets.rs1use std::{
3	io::Write,
4	path::{Path, PathBuf},
5};
6
7pub enum Target {
8	Local(String),
9	External(PathBuf),
10}
11
12impl Target {
13	pub fn is_local(&self) {
14		matches!(self, Self::Local(..));
15	}
16
17	pub fn is_external(&self) {
18		matches!(self, Self::External(..));
19	}
20
21	pub fn path_eq<P: AsRef<Path>>(&self, path: P) -> bool {
22		match self {
23			Self::External(p) => p == path.as_ref(),
24			_ => false,
25		}
26	}
27
28	pub fn read(&self) -> String {
29		match &self {
30			Target::Local(s) => s.clone(),
31			Target::External(p) => std::fs::read_to_string(p).unwrap(),
32		}
33	}
34
35	pub fn write(&mut self, content: String) {
36		match self {
37			Target::Local(s) => *s = content,
38			Target::External(p) => {
39				std::fs::create_dir_all(p.parent().unwrap()).unwrap();
40				let mut file = std::fs::File::create(p).unwrap();
41				file.write(content.as_bytes()).unwrap();
42			}
43		}
44	}
45}
46
47pub struct ReadWriteTarget {
48	pub read: Target,
49	pub write: Target,
50}