Skip to main content

appstruct_codegen/
artifact.rs

1use std::error::Error;
2use std::fmt;
3use std::path::PathBuf;
4
5/// A generated file planned in memory before any filesystem write.
6#[derive(Clone, Debug, PartialEq, Eq)]
7pub struct Artifact {
8    pub relative_path: PathBuf,
9    pub content: Vec<u8>,
10    pub executable: bool,
11    pub kind: ArtifactKind,
12}
13
14impl Artifact {
15    pub(crate) fn text(
16        relative_path: impl Into<PathBuf>,
17        content: impl Into<String>,
18        kind: ArtifactKind,
19    ) -> Self {
20        Self {
21            relative_path: relative_path.into(),
22            content: content.into().into_bytes(),
23            executable: false,
24            kind,
25        }
26    }
27}
28
29#[derive(Clone, Copy, Debug, PartialEq, Eq)]
30pub enum ArtifactKind {
31    CanonicalIr,
32    DatabaseSchema,
33    Migration,
34    RustManifest,
35    RustSource,
36    OpenApi,
37    TypeScript,
38    Web,
39    Module,
40}
41
42impl ArtifactKind {
43    #[must_use]
44    pub fn as_str(self) -> &'static str {
45        match self {
46            Self::CanonicalIr => "canonical_ir",
47            Self::DatabaseSchema => "database_schema",
48            Self::Migration => "migration",
49            Self::RustManifest => "rust_manifest",
50            Self::RustSource => "rust_source",
51            Self::OpenApi => "openapi",
52            Self::TypeScript => "typescript",
53            Self::Web => "web",
54            Self::Module => "module",
55        }
56    }
57}
58
59#[derive(Clone, Debug, PartialEq, Eq)]
60pub struct CodegenError {
61    message: String,
62}
63
64impl CodegenError {
65    pub(crate) fn new(message: impl Into<String>) -> Self {
66        Self {
67            message: message.into(),
68        }
69    }
70}
71
72impl fmt::Display for CodegenError {
73    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
74        self.message.fmt(formatter)
75    }
76}
77
78impl Error for CodegenError {}
79
80impl From<serde_json::Error> for CodegenError {
81    fn from(error: serde_json::Error) -> Self {
82        Self::new(format!("JSON generation failed: {error}"))
83    }
84}
85
86impl From<appstruct_ir::IrValidationErrors> for CodegenError {
87    fn from(error: appstruct_ir::IrValidationErrors) -> Self {
88        Self::new(error.to_string())
89    }
90}