MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / parse_memory_limit

Function parse_memory_limit

crates/openshell-driver-docker/src/lib.rs:2719–2762  ·  view source on GitHub ↗
(value: &str)

Source from the content-addressed store, hash-verified

2717
2718#[allow(clippy::cast_possible_truncation)]
2719fn parse_memory_limit(value: &str) -> Result<Option<i64>, Status> {
2720 let value = value.trim();
2721 if value.is_empty() {
2722 return Ok(None);
2723 }
2724
2725 let number_end = value
2726 .find(|ch: char| !(ch.is_ascii_digit() || ch == '.'))
2727 .unwrap_or(value.len());
2728 let (number, suffix) = value.split_at(number_end);
2729 let amount = number.parse::<f64>().map_err(|_| {
2730 Status::failed_precondition(format!(
2731 "invalid docker memory_limit '{value}'; expected a Kubernetes-style quantity",
2732 ))
2733 })?;
2734 if !amount.is_finite() || amount <= 0.0 {
2735 return Err(Status::failed_precondition(
2736 "docker memory_limit must be greater than zero",
2737 ));
2738 }
2739
2740 let multiplier = match suffix {
2741 "" => 1_f64,
2742 "Ki" => 1024_f64,
2743 "Mi" => 1024_f64.powi(2),
2744 "Gi" => 1024_f64.powi(3),
2745 "Ti" => 1024_f64.powi(4),
2746 "Pi" => 1024_f64.powi(5),
2747 "Ei" => 1024_f64.powi(6),
2748 "K" => 1000_f64,
2749 "M" => 1000_f64.powi(2),
2750 "G" => 1000_f64.powi(3),
2751 "T" => 1000_f64.powi(4),
2752 "P" => 1000_f64.powi(5),
2753 "E" => 1000_f64.powi(6),
2754 _ => {
2755 return Err(Status::failed_precondition(format!(
2756 "invalid docker memory_limit suffix '{suffix}'",
2757 )));
2758 }
2759 };
2760
2761 Ok(Some((amount * multiplier).round() as i64))
2762}
2763
2764fn sandbox_from_container_summary(
2765 summary: &ContainerSummary,

Callers 1

docker_resource_limitsFunction · 0.85

Calls 2

lenMethod · 0.80
is_emptyMethod · 0.45

Tested by

no test coverage detected