1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
//! InputDims is a struct that holds the dimensions of the input tensors for the model.
/// InputDims is a struct that holds the dimensions of the input tensors for the model.
///
/// # Fields
/// * `dims` - The dimensions of the input tensors.
#[derive(Debug, PartialEq)]
pub struct InputDims {
pub dims: [i32; 2],
}
impl InputDims {
/// Creates a new `InputDims` struct with all zeros.
///
/// # Returns
/// A new `InputDims` struct with all zeros.
pub fn fresh() -> Self {
InputDims {
dims: [0, 0],
}
}
/// Creates a new `InputDims` struct from a string.
///
/// # Arguments
/// * `data` - The dimensions as a string.
///
/// # Returns
/// A new `InputDims` struct.
pub fn from_string(data: String) -> InputDims {
if data == "".to_string() {
return InputDims::fresh();
}
let dims: Vec<&str> = data.split(",").collect();
let dims: Vec<i32> = dims.iter().map(|x| x.parse::<i32>().unwrap()).collect();
InputDims {
dims: [dims[0], dims[1]],
}
}
/// Translates the struct to a string.
///
/// # Returns
/// * `String` - The struct as a string.
pub fn to_string(&self) -> String {
if self.dims[0] == 0 && self.dims[1] == 0 {
return "".to_string();
}
format!("{},{}", self.dims[0], self.dims[1])
}
}
#[cfg(test)]
pub mod tests {
use super::*;
#[test]
fn test_fresh() {
let input_dims = InputDims::fresh();
assert_eq!(input_dims.dims[0], 0);
assert_eq!(input_dims.dims[1], 0);
}
#[test]
fn test_from_string() {
let input_dims = InputDims::from_string("1,2".to_string());
assert_eq!(input_dims.dims[0], 1);
assert_eq!(input_dims.dims[1], 2);
}
#[test]
fn test_to_string() {
let input_dims = InputDims::from_string("1,2".to_string());
assert_eq!(input_dims.to_string(), "1,2".to_string());
}
}