Skip to content

Commit

Permalink
Use StackId instead of String in contexts (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
Malax authored Nov 19, 2021
1 parent 5de4327 commit a66f1f9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
- New trait design for `LayerLifecycle` which also was renamed to `Layer`.
- Removed low-level layer functions from `BuildContext`. They don't fit well with the design of the library at this
point and are potential footguns. Implementing a `Layer` should work for all use-cases.
- The `stack_id` field in `BuildContext` and `DetectContext` is now of type `StackId` instead of `String`.

## [0.3.0] 2021/09/17
3 changes: 2 additions & 1 deletion libcnb/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use crate::buildpack::Buildpack;
use crate::data::store::Store;
use crate::layer::{HandleLayerErrorOrBuildpackError, Layer, LayerData};

use crate::data::buildpack::StackId;
use crate::data::{buildpack::BuildpackToml, buildpack_plan::BuildpackPlan, launch::Launch};

/// Context for the build phase execution.
pub struct BuildContext<B: Buildpack + ?Sized> {
pub layers_dir: PathBuf,
pub app_dir: PathBuf,
pub buildpack_dir: PathBuf,
pub stack_id: String,
pub stack_id: StackId,
pub platform: B::Platform,
pub buildpack_plan: BuildpackPlan,
pub buildpack_descriptor: BuildpackToml<B::Metadata>,
Expand Down
3 changes: 2 additions & 1 deletion libcnb/src/detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use std::fmt::Debug;
use std::path::PathBuf;

use crate::buildpack::Buildpack;
use crate::data::buildpack::StackId;
use crate::{data::build_plan::BuildPlan, data::buildpack::BuildpackToml};

/// Context for the detect phase execution.
pub struct DetectContext<B: Buildpack + ?Sized> {
pub app_dir: PathBuf,
pub buildpack_dir: PathBuf,
pub stack_id: String,
pub stack_id: StackId,
pub platform: B::Platform,
pub buildpack_descriptor: BuildpackToml<B::Metadata>,
}
Expand Down
4 changes: 4 additions & 0 deletions libcnb/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::data::buildpack::StackIdError;
use crate::data::launch::ProcessTypeError;
use crate::layer::HandleLayerError;
use crate::toml_file::TomlFileError;
Expand All @@ -17,6 +18,9 @@ pub enum Error<E: Debug + Display> {
#[error("Process type error: {0}")]
ProcessTypeError(#[from] ProcessTypeError),

#[error("Stack ID error: {0}")]
StackIdError(#[from] StackIdError),

#[error("Could not determine app directory: {0}")]
CannotDetermineAppDirectory(std::io::Error),

Expand Down
10 changes: 7 additions & 3 deletions libcnb/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::de::DeserializeOwned;

use crate::build::{BuildContext, InnerBuildResult};
use crate::buildpack::Buildpack;
use crate::data::buildpack::BuildpackToml;
use crate::data::buildpack::{BuildpackToml, StackId};
use crate::detect::{DetectContext, InnerDetectResult};
use crate::error::Error;
use crate::platform::Platform;
Expand Down Expand Up @@ -86,7 +86,9 @@ fn libcnb_runtime_detect<B: Buildpack>(buildpack: &B) -> Result<(), B::Error> {

let app_dir = env::current_dir().map_err(Error::CannotDetermineAppDirectory)?;

let stack_id: String = env::var("CNB_STACK_ID").map_err(Error::CannotDetermineStackId)?;
let stack_id: StackId = env::var("CNB_STACK_ID")
.map_err(Error::CannotDetermineStackId)
.and_then(|stack_id_string| stack_id_string.parse().map_err(Error::StackIdError))?;

let platform = B::Platform::from_path(&args.platform_dir_path)
.map_err(Error::CannotCreatePlatformFromPath)?;
Expand Down Expand Up @@ -121,7 +123,9 @@ fn libcnb_runtime_build<B: Buildpack>(buildpack: &B) -> Result<(), B::Error> {

let app_dir = env::current_dir().map_err(Error::CannotDetermineAppDirectory)?;

let stack_id: String = env::var("CNB_STACK_ID").map_err(Error::CannotDetermineStackId)?;
let stack_id: StackId = env::var("CNB_STACK_ID")
.map_err(Error::CannotDetermineStackId)
.and_then(|stack_id_string| stack_id_string.parse().map_err(Error::StackIdError))?;

let platform = B::Platform::from_path(&args.platform_dir_path)
.map_err(Error::CannotCreatePlatformFromPath)?;
Expand Down

0 comments on commit a66f1f9

Please sign in to comment.