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

Refine API #51

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use serde::{Deserialize, Serialize};

mod serialization_helpers;
use serialization_helpers::*;
pub use serialization_helpers::{
ByteUnit, CoreUnit, DefaultFactor, DefaultFactorOne, DefaultFactorOneGigabyte,
DefaultFactorOneKilobyte, DefaultFactorOneMegabyte, TimeUnit,
};

mod metadata;
pub use metadata::{Label, Metadata};
Expand Down
4 changes: 2 additions & 2 deletions src/models/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ impl From<gevulot::PinSpec> for PinSpec {
fn from(proto: gevulot::PinSpec) -> Self {
PinSpec {
cid: None,
bytes: (proto.bytes as i64).into(),
time: (proto.time as i64).into(),
bytes: proto.bytes.into(),
time: proto.time.into(),
redundancy: proto.redundancy as i64,
fallback_urls: Some(proto.fallback_urls),
}
Expand Down
42 changes: 21 additions & 21 deletions src/models/serialization_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@ use serde::{Deserialize, Serialize};

/// Trait for specifying default multiplication factors for byte units
pub trait DefaultFactor {
const FACTOR: i64;
const FACTOR: u64;
}

/// Default factor of 1 (no multiplication)
#[derive(Debug)]
pub struct DefaultFactorOne;
impl DefaultFactor for DefaultFactorOne {
const FACTOR: i64 = 1;
const FACTOR: u64 = 1;
}

/// Default factor of 1KB (1024 bytes)
#[derive(Debug)]
pub struct DefaultFactorOneKilobyte;
impl DefaultFactor for DefaultFactorOneKilobyte {
const FACTOR: i64 = 1024;
const FACTOR: u64 = 1024;
}

/// Default factor of 1MB (1024 * 1024 bytes)
#[derive(Debug)]
pub struct DefaultFactorOneMegabyte;
impl DefaultFactor for DefaultFactorOneMegabyte {
const FACTOR: i64 = 1024 * 1024;
const FACTOR: u64 = 1024 * 1024;
}

/// Default factor of 1GB (1024 * 1024 * 1024 bytes)
#[derive(Debug)]
pub struct DefaultFactorOneGigabyte;
impl DefaultFactor for DefaultFactorOneGigabyte {
const FACTOR: i64 = 1024 * 1024 * 1024;
const FACTOR: u64 = 1024 * 1024 * 1024;
}

/// Type for handling byte sizes with configurable default factors
Expand All @@ -66,22 +66,22 @@ impl DefaultFactor for DefaultFactorOneGigabyte {
#[derive(Debug, Serialize, Deserialize, Eq)]
#[serde(untagged)]
pub enum ByteUnit<D: DefaultFactor = DefaultFactorOne> {
Number(i64),
Number(u64),
String(String),
#[serde(skip)]
Factor(std::marker::PhantomData<D>),
}

impl<D: DefaultFactor> ByteUnit<D> {
/// Convert to number of bytes
pub fn bytes(&self) -> Result<i64, String> {
pub fn bytes(&self) -> Result<u64, String> {
match self {
ByteUnit::Number(n) => Ok(*n * D::FACTOR),
ByteUnit::String(s) => {
if s.chars().all(|c| c.is_ascii_digit()) {
return Ok(s.parse::<i64>().map_err(|e| e.to_string())? * D::FACTOR);
return Ok(s.parse::<u64>().map_err(|e| e.to_string())? * D::FACTOR);
}
s.parse::<ByteSize>().map(|b| b.0 as i64)
s.parse::<ByteSize>().map(|b| b.as_u64())
}
ByteUnit::Factor(_) => Ok(D::FACTOR),
}
Expand All @@ -103,8 +103,8 @@ impl<D: DefaultFactor> FromStr for ByteUnit<D> {
}
}

impl<D: DefaultFactor> From<i64> for ByteUnit<D> {
fn from(n: i64) -> Self {
impl<D: DefaultFactor> From<u64> for ByteUnit<D> {
fn from(n: u64) -> Self {
ByteUnit::Number(n)
}
}
Expand Down Expand Up @@ -133,21 +133,21 @@ impl<D: DefaultFactor> From<i64> for ByteUnit<D> {
#[derive(Debug, Serialize, Deserialize, Eq)]
#[serde(untagged)]
pub enum CoreUnit {
Number(i64),
Number(u64),
String(String),
}

impl CoreUnit {
/// Convert to millicores (1 core = 1000 millicores)
pub fn millicores(&self) -> Result<i64, String> {
pub fn millicores(&self) -> Result<u64, String> {
match self {
CoreUnit::Number(n) => Ok(*n * 1000), // Default factor without unit is 1000
CoreUnit::String(s) => {
// Extract numeric part
let numeric: String = s.chars().take_while(|c| c.is_digit(10)).collect();
// Extract and normalize unit part
let unit = s[numeric.len()..].to_lowercase().replace(" ", "");
let base: i64 = numeric
let base: u64 = numeric
.parse()
.map_err(|e| format!("Invalid number: {}", e))?;

Expand Down Expand Up @@ -185,8 +185,8 @@ impl FromStr for CoreUnit {
}
}

impl From<i64> for CoreUnit {
fn from(n: i64) -> Self {
impl From<u64> for CoreUnit {
fn from(n: u64) -> Self {
CoreUnit::Number(n)
}
}
Expand All @@ -213,19 +213,19 @@ impl From<i64> for CoreUnit {
#[derive(Debug, Serialize, Deserialize, Eq)]
#[serde(untagged)]
pub enum TimeUnit {
Number(i64),
Number(u64),
String(String),
}

impl TimeUnit {
/// Convert to seconds
pub fn seconds(&self) -> Result<i64, String> {
pub fn seconds(&self) -> Result<u64, String> {
match self {
TimeUnit::Number(n) => Ok(*n),
TimeUnit::String(s) => {
let duration =
humantime::parse_duration(s).map_err(|e| format!("Invalid duration: {}", e))?;
Ok(duration.as_secs() as i64)
Ok(duration.as_secs() as u64)
}
}
}
Expand All @@ -248,8 +248,8 @@ impl FromStr for TimeUnit {
}
}

impl From<i64> for TimeUnit {
fn from(n: i64) -> Self {
impl From<u64> for TimeUnit {
fn from(n: u64) -> Self {
TimeUnit::Number(n)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/models/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ impl From<gevulot::TaskSpec> for TaskSpec {
})
.collect(),
resources: TaskResources {
cpus: (proto.cpus as i64).into(),
gpus: (proto.gpus as i64).into(),
memory: (proto.memory as i64).into(),
time: (proto.time as i64).into(),
cpus: proto.cpus.into(),
gpus: proto.gpus.into(),
memory: proto.memory.into(),
time: proto.time.into(),
},
store_stdout: proto.store_stdout,
store_stderr: proto.store_stderr,
Expand Down
16 changes: 8 additions & 8 deletions src/models/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ impl From<gevulot::WorkerSpec> for WorkerSpec {
fn from(proto: gevulot::WorkerSpec) -> Self {
// Convert protobuf spec to internal spec
WorkerSpec {
cpus: (proto.cpus as i64).into(),
gpus: (proto.gpus as i64).into(),
memory: (proto.memory as i64).into(),
disk: (proto.disk as i64).into(),
cpus: proto.cpus.into(),
gpus: proto.gpus.into(),
memory: proto.memory.into(),
disk: proto.disk.into(),
}
}
}
Expand Down Expand Up @@ -160,10 +160,10 @@ impl From<gevulot::WorkerStatus> for WorkerStatus {
fn from(proto: gevulot::WorkerStatus) -> Self {
// Convert protobuf status to internal status
WorkerStatus {
cpus_used: (proto.cpus_used as i64).into(),
gpus_used: (proto.gpus_used as i64).into(),
memory_used: (proto.memory_used as i64).into(),
disk_used: (proto.disk_used as i64).into(),
cpus_used: proto.cpus_used.into(),
gpus_used: proto.gpus_used.into(),
memory_used: proto.memory_used.into(),
disk_used: proto.disk_used.into(),
exit_announced_at: proto.exit_announced_at as i64,
}
}
Expand Down