Skip to content
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
2 changes: 1 addition & 1 deletion crates/cargo-lambda-metadata/src/cargo/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ pub struct FunctionDeployConfig {
pub disable_function_url: bool,

/// Memory allocated for the function
#[arg(long, alias = "memory-size")]
#[arg(long, alias = "memory-size", value_parser = clap::value_parser!(i32).range(128..=10240))]
#[serde(default)]
pub memory: Option<Memory>,

Expand Down
12 changes: 6 additions & 6 deletions crates/cargo-lambda-metadata/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ mod tests {
);

assert_eq!(config.env.get("FOO"), Some(&"BAR".to_string()));
assert_eq!(config.deploy.function_config.memory, Some(Memory::Mb512));
assert_eq!(config.deploy.function_config.memory, Some(Memory(512)));
assert_eq!(config.deploy.function_config.timeout, Some(60.into()));

assert_eq!(
Expand Down Expand Up @@ -340,7 +340,7 @@ mod tests {

let metadata = load_metadata(manifest).unwrap();
let config = load_config_without_cli_flags(&metadata, &options).unwrap();
assert_eq!(config.deploy.function_config.memory, Some(Memory::Mb1024));
assert_eq!(config.deploy.function_config.memory, Some(Memory(1024)));

let options = ConfigOptions {
context: Some("development".to_string()),
Expand All @@ -349,15 +349,15 @@ mod tests {
};

let config = load_config_without_cli_flags(&metadata, &options).unwrap();
assert_eq!(config.deploy.function_config.memory, Some(Memory::Mb512));
assert_eq!(config.deploy.function_config.memory, Some(Memory(512)));

let options = ConfigOptions {
global: Some(global),
..Default::default()
};

let config = load_config_without_cli_flags(&metadata, &options).unwrap();
assert_eq!(config.deploy.function_config.memory, Some(Memory::Mb256));
assert_eq!(config.deploy.function_config.memory, Some(Memory(256)));
}

#[test]
Expand All @@ -372,7 +372,7 @@ mod tests {
};

let mut deploy = Deploy::default();
deploy.function_config.memory = Some(Memory::Mb2048);
deploy.function_config.memory = Some(Memory(2048));

let args_config = Config {
deploy,
Expand All @@ -381,6 +381,6 @@ mod tests {

let metadata = load_metadata(manifest).unwrap();
let config = load_config(&args_config, &metadata, &options).unwrap();
assert_eq!(config.deploy.function_config.memory, Some(Memory::Mb2048));
assert_eq!(config.deploy.function_config.memory, Some(Memory(2048)));
}
}
73 changes: 6 additions & 67 deletions crates/cargo-lambda-metadata/src/lambda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,39 +58,8 @@ impl From<i32> for Timeout {
}
}

#[derive(Clone, Debug, Display, EnumString, Eq, PartialEq)]
pub enum Memory {
#[strum(to_string = "128")]
Mb128,
#[strum(to_string = "256")]
Mb256,
#[strum(to_string = "512")]
Mb512,
#[strum(to_string = "640")]
Mb640,
#[strum(to_string = "1024")]
Mb1024,
#[strum(to_string = "1536")]
Mb1536,
#[strum(to_string = "2048")]
Mb2048,
#[strum(to_string = "3072")]
Mb3072,
#[strum(to_string = "4096")]
Mb4096,
#[strum(to_string = "5120")]
Mb5120,
#[strum(to_string = "6144")]
Mb6144,
#[strum(to_string = "7168")]
Mb7168,
#[strum(to_string = "8192")]
Mb8192,
#[strum(to_string = "9216")]
Mb9216,
#[strum(to_string = "10240")]
Mb10240,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Memory(pub i32);

impl From<Memory> for i32 {
fn from(m: Memory) -> i32 {
Expand All @@ -100,48 +69,18 @@ impl From<Memory> for i32 {

impl From<&Memory> for i32 {
fn from(m: &Memory) -> i32 {
match m {
Memory::Mb128 => 128,
Memory::Mb256 => 256,
Memory::Mb512 => 512,
Memory::Mb640 => 640,
Memory::Mb1024 => 1024,
Memory::Mb1536 => 1536,
Memory::Mb2048 => 2048,
Memory::Mb3072 => 3072,
Memory::Mb4096 => 4096,
Memory::Mb5120 => 5120,
Memory::Mb6144 => 6144,
Memory::Mb7168 => 7168,
Memory::Mb8192 => 8192,
Memory::Mb9216 => 9216,
Memory::Mb10240 => 10240,
}
m.0
}
}

impl TryFrom<i32> for Memory {
type Error = MetadataError;

fn try_from(m: i32) -> Result<Memory, Self::Error> {
match m {
128 => Ok(Memory::Mb128),
256 => Ok(Memory::Mb256),
512 => Ok(Memory::Mb512),
640 => Ok(Memory::Mb640),
1024 => Ok(Memory::Mb1024),
1536 => Ok(Memory::Mb1536),
2048 => Ok(Memory::Mb2048),
3072 => Ok(Memory::Mb3072),
4096 => Ok(Memory::Mb4096),
5120 => Ok(Memory::Mb5120),
6144 => Ok(Memory::Mb6144),
7168 => Ok(Memory::Mb7168),
8192 => Ok(Memory::Mb8192),
9216 => Ok(Memory::Mb9216),
10240 => Ok(Memory::Mb10240),
other => Err(MetadataError::InvalidMemory(other)),
if !(128..=10240).contains(&m) {
return Err(MetadataError::InvalidMemory(m));
}
Ok(Memory(m))
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/cargo-lambda-metadata/tests/jail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn test_env() {
let config = load_config_without_cli_flags(&metadata, &ConfigOptions::default()).unwrap();

assert!(config.build.cargo_opts.release);
assert_eq!(config.deploy.function_config.memory, Some(Memory::Mb1024));
assert_eq!(config.deploy.function_config.memory, Some(Memory(1024)));
assert_eq!(config.deploy.function_config.timeout, Some(60.into()));

Ok(())
Expand Down Expand Up @@ -65,7 +65,7 @@ fn test_env_with_context() {
let metadata = load_metadata("Cargo.toml").unwrap();
let config = load_config_without_cli_flags(&metadata, &options).unwrap();

assert_eq!(config.deploy.function_config.memory, Some(Memory::Mb1024));
assert_eq!(config.deploy.function_config.memory, Some(Memory(1024)));

Ok(())
});
Expand Down