1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
pub mod artifact_assets;
pub mod artifacts_collection;

use std::fmt::Display;

use getset::{CopyGetters, Getters, MutGetters, Setters};
use thiserror::Error;


use crate::resource::{disk_resource::DiskResource, Resource, ResourceError};

use super::dumpable::{DumpConfiguration, DumpError, Dumpable};


#[derive(Error, Debug)]
pub enum ArtifactError {

    #[error("the output path must be an existing directory because artifact can contain more than one file")]
    OutputPathNotDir,

    #[error(transparent)]
    ResourceError(#[from] ResourceError)
}

pub type ArtifactContent = String;

#[derive(Debug, Clone, Getters, MutGetters, CopyGetters, Setters)]
pub struct Artifact {

    #[getset(get = "pub", get_mut = "pub", set = "pub")]
    content: ArtifactContent,
}

impl Artifact {
    pub fn new(content: ArtifactContent) -> Self {

        Self {
            content
        }
    }
}

impl Display for Artifact {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.content)
    }
}

impl Dumpable for Artifact {
    fn dump(&mut self, configuration: &DumpConfiguration) -> Result<(), DumpError> {

        let path = configuration.output_path().clone();

        log::info!("dump artifact in {:?}", path);

        let mut disk_resource = DiskResource::try_from(path)?;

        if configuration.force_dump() {
            disk_resource.create_parents_dir()?;
        }

        disk_resource.write(&self.content)?;

        Ok(())
    }
}

impl Into<String> for Artifact {
    fn into(self) -> String {
        self.content
    }
}