Operational fields in struct

I have an api that returns the tree of a file, this api returns the name fields (file/folder name) and if it is a file it returns the content field with the content of the file and if it is a folder it returns children with the exact same structure. ie either there is a children field or there is a content

How to deploy this in a struct? I was thinking of making both fields optional and I did something like:

#[derive(Serialize, Deserialize)]
pub struct TemplateBaseStructure {
  pub name: String,
  pub children: Vec<TemplateBaseStructure>,
  pub content: String,

but that didn't work because the two fields never exist in parallel.

For ensuring two fields don't exist in parallel, it sounds like an enum is appropriate.

pub enum TemplateBaseStructure {
  Folder {
    name: String,
    children: Vec<TemplateBaseStructure>,
  File {
    name: String,
    content: String,

Maybe someone else can chime in with more details, but I've heard that tree structures are discouraged when first exploring the language. I recall something about getting stuck if you want to mutably walk the tree.

edit: fixed compile error by checking on the playground

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.