From e2736f3e10107f97105947aab3b80a77debbb29d Mon Sep 17 00:00:00 2001 From: Adam McKee Date: Thu, 9 Jan 2025 13:37:38 -0600 Subject: [PATCH] splitting shared parse and build apis in result and spec modules to build and parse modules --- fn_build/src/{spec.rs => build.rs} | 40 +++++-- fn_build/src/build_test.rs | 2 +- fn_build/src/lib.rs | 8 +- fn_build/src/{result.rs => parse.rs} | 152 +++++++++++---------------- 4 files changed, 103 insertions(+), 99 deletions(-) rename fn_build/src/{spec.rs => build.rs} (66%) rename fn_build/src/{result.rs => parse.rs} (74%) diff --git a/fn_build/src/spec.rs b/fn_build/src/build.rs similarity index 66% rename from fn_build/src/spec.rs rename to fn_build/src/build.rs index ca26051..ce58dae 100644 --- a/fn_build/src/spec.rs +++ b/fn_build/src/build.rs @@ -1,5 +1,9 @@ +use crate::checksum::Checksum; use crate::runtime::Runtime; +use crate::{FnDependencies, FnHandler, FnParseError, FnParseSpec, FnSource}; use serde::Deserialize; +use std::collections::HashMap; +use std::io; use std::path::PathBuf; use std::sync::Arc; @@ -9,12 +13,6 @@ pub enum BuildMode { Release, } -pub struct FnParseSpec { - pub entrypoint: PathBuf, - pub project_dir: Arc, - pub runtime: Runtime, -} - /// Output config for the build. #[derive(Clone, Deserialize)] pub struct FnOutputConfig { @@ -69,3 +67,33 @@ impl FnBuildSpec { } } } + +#[derive(Clone, Deserialize)] +pub struct FnBuildOutput { + pub archive_file: Option, + /// PathBuf to the build's output directory partitioned by the build's BuildMode. + pub build_dir: PathBuf, +} + +#[derive(Clone, Deserialize)] +pub struct FnBuildManifest { + /// Checksums of the original sources of the function's build. + pub checksums: HashMap, + pub dependencies: FnDependencies, + pub entrypoint: PathBuf, + pub handler: FnHandler, + pub output: FnBuildOutput, + pub sources: Vec, +} + +#[derive(thiserror::Error, Debug)] +pub enum FnBuildError { + #[error("{0}")] + IoError(#[from] io::Error), + #[error("error parsing function: {0}")] + ParseError(#[from] FnParseError), + #[error("build task error: {0}")] + KnownError(#[from] anyhow::Error), +} + +pub type FnBuildResult = Result; diff --git a/fn_build/src/build_test.rs b/fn_build/src/build_test.rs index 36ad8b4..aa1ff3c 100644 --- a/fn_build/src/build_test.rs +++ b/fn_build/src/build_test.rs @@ -59,7 +59,7 @@ async fn build_fn_errors_for_missing_handler() { assert!(result.is_err()); assert_eq!( result.err().unwrap().to_string(), - "entrypoint routes/data/lambda.js does not have a handler fn someWackyFunctionName" + "error parsing function: entrypoint routes/data/lambda.js does not have a handler fn someWackyFunctionName" ); } diff --git a/fn_build/src/lib.rs b/fn_build/src/lib.rs index ae4ae42..caf4898 100644 --- a/fn_build/src/lib.rs +++ b/fn_build/src/lib.rs @@ -1,10 +1,10 @@ mod archive; +mod build; mod checksum; +mod parse; mod paths; -mod result; mod routing; pub mod runtime; -mod spec; mod swc; #[cfg(test)] @@ -25,9 +25,9 @@ mod testing; #[cfg(test)] mod routing_test; -pub use crate::result::*; +pub use crate::build::*; +pub use crate::parse::*; pub use crate::routing::*; -pub use crate::spec::*; /// Builds a lambda function. pub async fn build_fn(build_spec: FnBuildSpec) -> FnBuildResult { diff --git a/fn_build/src/result.rs b/fn_build/src/parse.rs similarity index 74% rename from fn_build/src/result.rs rename to fn_build/src/parse.rs index e5d8dd0..d82ff8a 100644 --- a/fn_build/src/result.rs +++ b/fn_build/src/parse.rs @@ -1,27 +1,14 @@ -use crate::checksum::Checksum; +use crate::runtime::Runtime; use crate::{FnRouting, HttpMethod}; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; use std::io; use std::path::{Path, PathBuf}; +use std::sync::Arc; -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -#[serde(rename_all = "camelCase")] -pub enum ModuleImport { - PackageDependency { - package: String, - subpath: Option, - }, - /// A source file import specified by relative path from the project's root. - RelativeSource(PathBuf), - /// An unresolvable import specifier. - Unknown(String), -} - -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -pub struct FnSource { - pub imports: Vec, - pub path: PathBuf, +pub struct FnParseSpec { + pub entrypoint: PathBuf, + pub project_dir: Arc, + pub runtime: Runtime, } #[derive(Clone, Deserialize, Serialize)] @@ -40,75 +27,6 @@ pub struct FnHandler { pub routing: FnRouting, } -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -pub struct FnEntrypoint { - pub handlers: Vec, - pub path: PathBuf, -} - -#[derive(Clone, Deserialize, Serialize)] -pub struct FnParseManifest { - pub dependencies: FnDependencies, - pub entrypoint: FnEntrypoint, - pub sources: Vec, -} - -#[derive(thiserror::Error, Debug)] -pub enum FnParseError { - #[error("entrypoint file type is unsupported")] - InvalidFileType, - #[error("{0}")] - IoError(#[from] io::Error), - #[error("syntax error")] - SyntaxError, -} - -pub type FnParseResult = Result; - -#[derive(Clone, Deserialize)] -pub struct FnBuildOutput { - pub archive_file: Option, - /// PathBuf to the build's output directory partitioned by the build's BuildMode. - pub build_dir: PathBuf, -} - -#[derive(Clone, Deserialize)] -pub struct FnBuildManifest { - /// Checksums of the original sources of the function's build. - pub checksums: HashMap, - pub dependencies: FnDependencies, - pub entrypoint: PathBuf, - pub handler: FnHandler, - pub output: FnBuildOutput, - pub sources: Vec, -} - -#[derive(thiserror::Error, Debug)] -pub enum FnBuildError { - #[error("{0}")] - IoError(#[from] io::Error), - #[error("error parsing function: {0}")] - ParseError(#[from] FnParseError), - #[error("entrypoint {0} does not have a handler fn {1}")] - MissingHandler(PathBuf, String), - #[error("build task error: {0}")] - KnownError(#[from] anyhow::Error), -} - -pub type FnBuildResult = Result; - -impl FnEntrypoint { - pub fn to_fn_identifier(&self, handler_fn_name: &str) -> FnBuildResult { - self.handlers - .iter() - .find(|h| h.fn_name == handler_fn_name) - .map(FnHandler::to_fn_identifier) - .ok_or_else(|| { - FnBuildError::MissingHandler(self.path.clone(), handler_fn_name.to_string()) - }) - } -} - impl FnHandler { pub fn from_handler_fn(path: &Path, fn_name: String) -> Self { let routing = FnRouting::from_handler_fn(path, fn_name.as_str()); @@ -135,8 +53,66 @@ impl FnHandler { } } +#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] +pub struct FnEntrypoint { + pub handlers: Vec, + pub path: PathBuf, +} + +impl FnEntrypoint { + pub fn to_fn_identifier(&self, handler_fn_name: &str) -> FnParseResult { + self.handlers + .iter() + .find(|h| h.fn_name == handler_fn_name) + .map(FnHandler::to_fn_identifier) + .ok_or_else(|| { + FnParseError::MissingHandler(self.path.clone(), handler_fn_name.to_string()) + }) + } +} + +#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] +#[serde(rename_all = "camelCase")] +pub enum ModuleImport { + PackageDependency { + package: String, + subpath: Option, + }, + /// A source file import specified by relative path from the project's root. + RelativeSource(PathBuf), + /// An unresolvable import specifier. + Unknown(String), +} + +#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] +pub struct FnSource { + pub imports: Vec, + pub path: PathBuf, +} + +#[derive(Clone, Deserialize, Serialize)] +pub struct FnParseManifest { + pub dependencies: FnDependencies, + pub entrypoint: FnEntrypoint, + pub sources: Vec, +} + impl FnParseManifest { pub fn source_paths(&self) -> Vec { self.sources.iter().map(|s| s.path.clone()).collect() } } + +#[derive(thiserror::Error, Debug)] +pub enum FnParseError { + #[error("entrypoint file type is unsupported")] + InvalidFileType, + #[error("{0}")] + IoError(#[from] io::Error), + #[error("entrypoint {0} does not have a handler fn {1}")] + MissingHandler(PathBuf, String), + #[error("syntax error")] + SyntaxError, +} + +pub type FnParseResult = Result;