flow-types 0.1.0-rc.5

The Flow declaration model with no values: types, declarations and their identities, record and enum shapes, and which types decoding may build.
Documentation
//! Record and enum declarations (`types.md`, *Records* and *Enums*): the
//! shared description a record or enum value holds beside its slots. It is
//! what the text form reads, the type's name, its constructors' names and its
//! fields' names, while the slots are what equality and order read. Two values
//! of one type share one description, so holding it costs a pointer.

use std::sync::Arc;

/// A record declaration: its name and its fields in declaration order.
#[derive(Debug, PartialEq, Eq)]
pub struct RecordShape {
    name: Arc<str>,
    fields: Box<[Arc<str>]>,
}

impl RecordShape {
    /// Returns the shape of the record `name` declaring `fields`, in order.
    pub fn new<'a>(name: &str, fields: impl IntoIterator<Item = &'a str>) -> Arc<Self> {
        Arc::new(RecordShape {
            name: Arc::from(name),
            fields: fields.into_iter().map(Arc::from).collect(),
        })
    }

    /// Returns the type's declared name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the field names, in declaration order.
    pub fn fields(&self) -> impl ExactSizeIterator<Item = &str> {
        self.fields.iter().map(|field| &**field)
    }

    /// Returns the name of the field at `index`, counted from 0 in
    /// declaration order.
    pub fn field(&self, index: usize) -> Option<&str> {
        self.fields.get(index).map(|field| &**field)
    }
}

/// What a constructor carries.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Payload {
    /// Nothing: `TimedOut`.
    None,
    /// This many positional values: `Failed(problem)`.
    Positional(usize),
    /// Named fields, in declaration order: `StepFailed { step, last }`.
    Named(Box<[Arc<str>]>),
}

impl Payload {
    /// Returns how many values the payload holds.
    pub fn arity(&self) -> usize {
        match self {
            Payload::None => 0,
            Payload::Positional(count) => *count,
            Payload::Named(fields) => fields.len(),
        }
    }

    /// Returns named fields, in declaration order.
    pub fn named<'a>(fields: impl IntoIterator<Item = &'a str>) -> Self {
        Payload::Named(fields.into_iter().map(Arc::from).collect())
    }
}

/// One constructor of an enum declaration.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Constructor {
    name: Arc<str>,
    payload: Payload,
}

impl Constructor {
    /// Returns the constructor `name` carrying `payload`.
    pub fn new(name: &str, payload: Payload) -> Self {
        Constructor {
            name: Arc::from(name),
            payload,
        }
    }

    /// Returns the constructor's name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns what it carries.
    pub fn payload(&self) -> &Payload {
        &self.payload
    }
}

/// An enum declaration: its name and its constructors in declaration order.
#[derive(Debug, PartialEq, Eq)]
pub struct EnumShape {
    name: Arc<str>,
    constructors: Box<[Constructor]>,
}

impl EnumShape {
    /// Returns the shape of the enum `name` declaring `constructors`, in
    /// order.
    pub fn new(name: &str, constructors: impl IntoIterator<Item = Constructor>) -> Arc<Self> {
        Arc::new(EnumShape {
            name: Arc::from(name),
            constructors: constructors.into_iter().collect(),
        })
    }

    /// Returns the type's declared name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the constructor at a declaration index.
    pub fn constructor(&self, index: usize) -> Option<&Constructor> {
        self.constructors.get(index)
    }

    /// Returns the constructors, in declaration order.
    pub fn constructors(&self) -> &[Constructor] {
        &self.constructors
    }
}