blanket_rs/resource/
copy.rs1use std::cell::RefCell;
2use std::path::{Path, PathBuf};
3use std::rc::Rc;
4
5use crate::{
6 builder::{Build, Builder, Dependency},
7 resource::Directory,
8};
9
10#[derive(Debug)]
11pub struct CopyFile {
12 source: PathBuf,
13 path: PathBuf,
14}
15
16impl CopyFile {
17 pub fn new<P: AsRef<Path>>(source: P, path: P) -> Self {
18 Self {
19 source: source.as_ref().to_path_buf(),
20 path: path.as_ref().to_path_buf(),
21 }
22 }
23}
24
25impl PartialEq for CopyFile {
26 fn eq(&self, other: &Self) -> bool {
27 self.path == other.path
28 }
29}
30
31impl Build for CopyFile {
32 fn as_any(&self) -> &dyn std::any::Any {
33 self
34 }
35 fn equals(&self, other: Rc<RefCell<dyn Build>>) -> bool {
36 let other = other.borrow();
37 let any = other.as_any();
38 match any.downcast_ref::<Self>() {
39 Some(other) => self == other,
40 None => false,
41 }
42 }
43 fn register(
44 self,
45 builder: &mut Builder,
46 ) -> Result<(Option<PathBuf>, Dependency, Vec<Dependency>), Box<dyn std::error::Error>> {
47 let path = self.path.clone();
48 let parent = match self.path.parent() {
49 Some(parent) => parent,
50 None => return Err("path has no parent".into()),
51 };
52 let dir = builder.require(Directory::new(parent))?;
53 let dependency = builder.make_dependency(self)?;
54 Ok((Some(path), dependency, vec![dir]))
55 }
56 fn generate(&mut self) -> Result<(), Box<dyn std::error::Error>> {
57 let CopyFile { source, path } = self;
58 if source.is_dir() {
59 return Err("source is a directory".into());
60 }
61 std::fs::copy(source, path)?;
62 Ok(())
63 }
64}