#Matching enum in enum

7 messages · Page 1 of 1 (latest)

junior summit
#

If I have a enum consisting of enums, how do I match for the inside of the enum?

pub fn column_f64(&self, column: SymbolDataColumn, head: Option<u32>, tail: Option<u32>) -> Vec<f64> {
    let mut c: Vec<f64> = Vec::new();
    match column {
        Name => panic!("Cannot retrieve column with type String!"),
        SymbolDataColumnU32 => panic!("Use column_u32 function instead to retrieve list with types u32"),
        SymbolDataColumnF64::Open => for row in self.df.iter() { c.push(row.open.unwrap()) },
        SymbolDataColumnF64::High => for row in self.df.iter() { c.push(row.high.unwrap()) },
        SymbolDataColumnF64::Low => for row in self.df.iter() { c.push(row.low.unwrap()) },
        SymbolDataColumnF64::Close => for row in self.df.iter() { c.push(row.close.unwrap()) }
    }
    c
}

enum SymbolDataColumnF64 {
    Open,
    High,
    Low,
    Close,
}

enum SymbolDataColumnU32 {
    Time,
    TimeCalculated,
    }

enum SymbolDataColumn {
    Name,
    SymbolDataColumnU32,
    SymbolDataColumnF64
}

I get error: Expected enum 'SymbolDataColumn', found enum 'SymbolDataColumnF64'. But how do I go about doing this then? Do I have to match for just SymbolDataColumnF64 and then inside that match, do another match?

old river
#

This isn't an enum inside an enum, despite appearances

#

These are all totally flat enums. Reusing a type name as an enum variant name is allowed, and the result is just an enum variant

#

You may want

enum SymbolDataColumn {
  Name,
  U32(SymbolDataColumnU32),
  F64(SymbolDataColumnF64)
}
#

Match as

match foo {
  U32(sym_data_col) => ...
  ...
}
junior summit
#

Ah okay. However, that does not seem to work right either.
I tried

pub fn column_f64(&self, column: SymbolDataColumn, head: Option<u32>, tail: Option<u32>) -> Vec<f64> {
    let mut c: Vec<f64> = Vec::new();
    match column {
        Name => panic!("Cannot retrieve column with type String!"),
        U32 => panic!("Use column_u32 function instead to retrieve list with types u32"),
        F64(Open) => for row in self.df.iter() { c.push(row.open.unwrap()) },
        F64(High) => for row in self.df.iter() { c.push(row.high.unwrap()) },
        F64(Low) => for row in self.df.iter() { c.push(row.low.unwrap()) },
        F64(Close) => for row in self.df.iter() { c.push(row.close.unwrap()) },
    }
    c
}
enum SymbolDataColumn {
    Name,
    U32,
    F64
}

But it gives expected struct or tuple variant, F64 not found in scope

old river
#

Enum variants are namespaced to their enum, so you'd have to ude it as SymbolDataColumn::U32(sym_data_col), sorry