postrs 0.1.0

A tool for interacting with Postman collections in Rust.
Documentation
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Debug, Serialize, Deserialize)]
pub struct Collection {
    pub info: Info, 

    /// Items are the basic unit for a Postman collection. 
    /// 
    /// You can think of them as corresponding to a single API endpoint. 
    /// 
    /// Each Item has one request and may have multiple API responses associated with it.
    pub item: Vec<ItemOrGroup>,

    pub event: Option<Vec<Event>>,

    pub variable: Option<Vec<Variable>>,

    pub auth: Option<Auth>,

    #[serde(rename = "protocolProfileBehavior")]
    pub protocol_profile_behavior: Option<ProtocolProfileBehavior>,
}

/// Represents authentication helpers provided by Postman
#[derive(Debug, Serialize, Deserialize)]
pub struct Auth {
    #[serde(rename = "auth")]
    pub atype: AuthType,

}

#[derive(Debug, Serialize, Deserialize)]
pub enum AuthType {
    #[serde(rename = "apikey")]
    APIKey,

    #[serde(rename = "awsv4")]
    AWSv4,

    #[serde(rename = "basic")]
    Basic,

    #[serde(rename = "bearer")]
    Bearer,

    #[serde(rename = "digest")]
    Digest,

    #[serde(rename = "edgegrid")]
    EdgeGrid,

    #[serde(rename = "hawk")]
    Hawk,

    #[serde(rename = "noauth")]
    NoAuth,

    #[serde(rename = "oauth1")]
    OAuth1,

    #[serde(rename = "oauth2")]
    OAuth2,

    #[serde(rename = "ntlm")]
    NTLM,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum Description {
    Str(String),
    Obj(DescriptionObj),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DescriptionObj {
    
    /// The content of the description goes here, as a raw string.
    pub content: Option<String>,
    
    /// Holds the mime type of the raw description content. 
    /// E.g: 'text/markdown' or 'text/html'.
    /// 
    /// The type is used to correctly render the description when 
    /// generating documentation, or in the Postman app.
    #[serde(rename = "type")]
    pub mime_type: Option<String>,

    /// Description can have versions associated with it, which should be put in this property.
    pub version: Option<Value>,
}

/// Defines a script associated with an associated event name
#[derive(Debug, Serialize, Deserialize)]
pub struct Event {
    pub id: Option<String>,

    /// Can be set to `test` or `prerequest` for test scripts or 
    /// pre-request scripts respectively.
    pub listen: String,

    pub script: Option<Script>,

    pub disabled: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum Exec {
    Str(String),
    Obj(Vec<String>),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Info {
    /// Name of the collection
    /// 
    /// A collection's friendly name is defined by this field. 
    /// You would want to set this field to a value that would allow you to 
    /// easily identify this collection among a bunch of other collections, 
    /// as such outlining its usage or content.
    pub name: String,


    /// Every collection is identified by the unique value of this field. 
    /// The value of this field is usually easiest to generate using a UID generator function. 
    /// 
    /// If you already have a collection, it is recommended that you maintain the same id since 
    /// changing the id usually implies that is a different collection than it was originally.
    /// 
    /// *Note: This field exists for compatibility reasons with Collection Format V1.*
    #[serde(rename = "_postman_id")]
    pub postman_id: Option<String>,

    pub description: Option<Description>,

    pub version: Option<Version>,

    /// This should ideally hold a link to the Postman schema that is used to validate 
    /// this collection. E.g: https://schema.getpostman.com/collection/v1
    pub schema: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum ItemOrGroup {
    Item(Item),
    Group(ItemGroup),
}

/// Items are entities which contain an actual HTTP request, and sample responses attached to it.
#[derive(Debug, Serialize, Deserialize)]
pub struct Item {
    pub id: Option<String>,

    pub name: Option<String>,

    pub description: Option<Description>,

    pub variable: Option<Vec<Variable>>,

    pub event: Option<Vec<Event>>,

    pub request: Request,

    pub response: Option<Vec<Response>>,
    
    #[serde(rename = "protocolProfileBehavior")]
    pub protocol_profile_behavior: Option<ProtocolProfileBehavior>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ItemGroup {}

#[derive(Debug, Serialize, Deserialize)]
pub struct ProtocolProfileBehavior {}

#[derive(Debug, Serialize, Deserialize)]
pub struct QueryParam {
    pub key: Option<String>,
    pub value: Option<String>,
    pub description: Option<String>,
    pub disabled: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Request {}

#[derive(Debug, Serialize, Deserialize)]
pub struct Response {}

/// A script is a snippet of Javascript code that can be used to to perform 
/// setup or teardown operations on a particular response.
#[derive(Debug, Serialize, Deserialize)]
pub struct Script {
    pub id: Option<String>,

    pub stype: Option<String>,

    pub exec: Option<Exec>,

    pub src: Option<URL>,

    /// Script name
    pub name: Option<String>,
}

/// If object, contains the complete broken-down URL for this request. 
/// If string, contains the literal request URL.
#[derive(Debug, Serialize, Deserialize)]
pub enum URL {
    Str(String),
    Obj(URLObj),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct URLObj {
    /// The string representation of the request URL, including the protocol, 
    /// host, path, hash, query parameter(s) and path variable(s).
    pub raw: Option<String>,

    /// The protocol associated with the request, E.g: 'http'
    pub protocol: Option<String>,

    /// The host for the URL, E.g: api.yourdomain.com. Can be stored as a 
    /// string or as an array of strings.
    pub host: Option<Host>,

    pub path: Option<Path>,

    /// The port number present in this URL. 
    /// 
    /// An empty value implies 80/443 depending on whether the protocol 
    /// field contains http/https.
    pub port: Option<String>,

    /// An array of QueryParams, which is basically the query string part of the URL, 
    /// parsed into separate variables.
    pub query: Option<Vec<QueryParam>>,

    /// Contains the URL fragment (if any). Usually this is not transmitted over the network, 
    /// but it could be useful to store this in some cases.
    pub hash: Option<String>,

    /// Postman supports path variables with the syntax `/path/:variableName/to/somewhere`. 
    /// These variables are stored in this field.
    pub variable: Option<Vec<Variable>>,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum Host {
    Str(String),

    /// The host, split into subdomain strings.
    Parts(Vec<String>),
}

#[derive(Debug, Serialize, Deserialize)]
pub enum Path {
    Str(String),
    Parts(Vec<PathPart>),
}

#[derive(Debug, Serialize, Deserialize)]
pub enum PathPart {
    Str(String),
    Obj(PathPartObj),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PathPartObj {
    pub dtype: Option<String>,
    pub value: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Variable {
    /// A variable ID is a unique user-defined value that identifies the variable 
    /// within a collection. In traditional terms, this would be a variable name.
    pub id: Option<String>,

    /// A variable key is a human friendly value that identifies the variable within 
    /// a collection. In traditional terms, this would be a variable name.
    pub key: Option<String>,

    /// The value that a variable holds in this collection. Ultimately, the variables 
    /// will be replaced by this value, when say running a set of requests from a collection
    pub value: Option<Value>,

    /// A variable may have multiple types. This field specifies the type of the variable.
    #[serde(rename="type")]
    pub dtype: Option<VarDType>,

    /// Variable name
    pub name: Option<String>,

    pub description: Option<Description>,
    
    /// When set to true, indicates that this variable has been set by Postman
    pub system: Option<bool>,
    
    pub disabled: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum VarDType {
    #[serde(rename="string")]
    String,

    #[serde(rename="boolean")]
    Boolean,
    
    #[serde(rename="number")]
    Number,
    
    #[serde(rename="any")]
    Any,
}

/// Collection Version
/// 
/// Postman allows you to version your collections as they grow, 
/// and this field holds the version number. 
/// 
/// While optional, it is recommended that you use this field to its fullest extent!
#[derive(Debug, Serialize, Deserialize)]
pub enum Version {
    Str(String),
    Obj(VersionObj),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct VersionObj {
    pub major: u32,
    pub minor: u32,
    pub patch: u32,
    pub identifier: Option<String>,
}





#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let result = 2 + 2;
        assert_eq!(result, 4);
    }
}