nmd-core 1.0.1

Official NMD compiler core
Documentation
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 From<&str> for Artifact {
    fn from(value: &str) -> Self {
        Self::new(String::from(value))
    }
}

impl From<&String> for Artifact {
    fn from(value: &String) -> Self {
        Self::new(value.clone())
    }
}

impl From<String> for Artifact {
    fn from(value: String) -> Self {
        Self::new(value)
    }
}

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
    }
}