Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add base layer.rs #3

Merged
merged 3 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ semver = { version = "0.11", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
thiserror = "1.0"
toml = "0.5.8"

[dev-dependencies]
anyhow = "1"
tempfile = "3"
11 changes: 11 additions & 0 deletions src/data/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ pub struct Layer {
pub cache: bool,
pub metadata: Table,
}

impl Layer {
pub fn new() -> Self {
Layer {
launch: false,
build: false,
cache: false,
metadata: Table::new(),
}
}
}
8 changes: 7 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(thiserror::Error, Debug, PartialEq)]
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Found `{0}` but value MUST be in form <major>.<minor> or <major>, where <major> is equivalent to <major>.0.")]
InvalidBuildpackApi(String),
Expand All @@ -12,4 +12,10 @@ pub enum Error {
"Found `{0}` but value MUST only contain numbers, letters, and the characters ., /, and -."
)]
InvalidStackId(String),
#[error("could not serialize into TOML")]
TomlSerError(#[from] toml::ser::Error),
#[error("could not deserialize from TOML")]
TomlDeError(#[from] toml::de::Error),
#[error("I/O Error: {0}")]
IoError(#[from] std::io::Error),
}
123 changes: 123 additions & 0 deletions src/layer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use crate::{data::layer::Layer as ContentMetadata, Error};
use std::{
fs,
path::{Path, PathBuf},
};

/// CNB Layer
pub struct Layer {
pub name: String,
path: PathBuf,
content_metadata_path: PathBuf,
content_metadata: ContentMetadata,
}

impl Layer {
/// Layer Constructor that makes a ready to go layer:
/// * create `/<layers_dir>/<layer> if it doesn't exist
/// * `/<layers_dir>/<layer>.toml` will be read and parsed from disk if found. If not found an
/// empty [`crate::data::layer::Layer`] will be constructed.
///
/// # Errors
/// This function will return an error when:
/// * if it can not create the layer dir
/// * if it can not deserialize Layer Content Metadata to [`crate::data::layer::Layer`]
///
/// # Examples
/// ```
/// # use tempfile::tempdir;
/// use libcnb::layer::Layer;
///
/// # fn main() -> Result<(), libcnb::Error> {
/// # let layers_dir = tempdir().unwrap().path().to_owned();
/// let layer = Layer::new("foo", layers_dir)?;
///
/// assert!(layer.as_path().exists());
/// assert_eq!(layer.content_metadata().launch, false);
/// assert_eq!(layer.content_metadata().build, false);
/// assert_eq!(layer.content_metadata().cache, false);
/// assert!(layer.content_metadata().metadata.is_empty());
/// # Ok(())
/// # }
/// ```
pub fn new(name: impl Into<String>, layers_dir: impl AsRef<Path>) -> Result<Self, Error> {
let name = name.into();
let layers_dir = layers_dir.as_ref();
let path = layers_dir.join(&name);

fs::create_dir_all(&path)?;

let content_metadata_path = layers_dir.join(format!("{}.toml", &name));
let content_metadata = if let Ok(contents) = fs::read_to_string(&content_metadata_path) {
toml::from_str(&contents)?
} else {
ContentMetadata::new()
};

Ok(Layer {
name,
path,
content_metadata,
content_metadata_path,
})
}

/// Returns the path to the layer contents `/<layers_dir>/<layer>/`.
pub fn as_path(&self) -> &Path {
self.path.as_path()
}

/// Returns a reference to the [`crate::data::layer::Layer`]
pub fn content_metadata(&self) -> &ContentMetadata {
&self.content_metadata
}

/// Returns a mutable reference to the [`crate::data::layer::Layer`]
pub fn mut_content_metadata(&mut self) -> &mut ContentMetadata {
&mut self.content_metadata
}

/// Write [`crate::data::layer::Layer`] to `<layer>.toml`
pub fn write_content_metadata(&self) -> Result<(), crate::Error> {
fs::write(
&self.content_metadata_path,
toml::to_string(&self.content_metadata)?,
)?;

Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::tempdir;

#[test]
fn new_reads_layer_toml_metadata() -> Result<(), anyhow::Error> {
let layers_dir = tempdir()?.path().to_owned();
fs::create_dir_all(&layers_dir)?;
fs::write(
layers_dir.join("foo.toml"),
r#"
[metadata]
bar = "baz"
"#,
)?;

let layer = Layer::new("foo", &layers_dir)?;
assert_eq!(
layer
.content_metadata()
.metadata
.get::<str>("bar")
.unwrap()
.as_str()
.unwrap(),
"baz"
);

Ok(())
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ pub mod data;
mod error;
pub use error::Error;
pub mod detect;
pub mod layer;
pub mod platform;
pub mod shared;