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
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use structopt::StructOpt;

use dsf_core::base::Body;
use dsf_core::page::Page;
use dsf_core::types::*;

use crate::helpers::data_from_str;
use crate::{PageBounds, ServiceIdentifier};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DataInfo {
    pub service: Id,

    pub index: u16,
    pub body: Body,

    pub previous: Option<Signature>,
    pub signature: Signature,
}

impl std::convert::TryFrom<&Page> for DataInfo {
    type Error = std::convert::Infallible;

    fn try_from(page: &Page) -> Result<DataInfo, Self::Error> {
        Ok(DataInfo {
            service: page.id.clone(),
            index: page.header().index,
            body: page.body().clone(),
            previous: page.previous_sig.clone(),
            signature: page.signature.clone().unwrap(),
        })
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, StructOpt)]
pub enum DataCommands {
    #[structopt(name = "list")]
    /// List service data
    List(ListOptions),

    #[structopt(name = "sync")]
    /// Synchronize service data
    Update {},

    #[structopt(name = "query")]
    /// Fetch data from a service
    Query {},

    #[structopt(name = "publish")]
    /// Publish data to a service
    Publish(PublishOptions),

    #[structopt(name = "push")]
    /// Push pre-signed data for a known server
    Push(PushOptions),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, StructOpt)]
pub struct ListOptions {
    #[structopt(flatten)]
    pub service: ServiceIdentifier,

    #[structopt(flatten)]
    pub bounds: PageBounds,
}

pub type Data = Vec<u8>;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, StructOpt)]
pub struct PublishOptions {
    #[structopt(flatten)]
    pub service: ServiceIdentifier,

    #[structopt(short = "k", long = "kind", default_value = "generic")]
    /// Data page kind (defaults to generic)
    pub kind: DataKind,

    #[structopt(short = "d", long = "data", parse(from_str = data_from_str))]
    /// Data body as a string
    pub data: Option<Data>,
}

impl PublishOptions {
    pub fn new(id: Id) -> Self {
        Self {
            service: ServiceIdentifier {
                id: Some(id),
                index: None,
            },
            kind: DataKind::Generic,
            data: None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, StructOpt)]
pub struct PushOptions {
    #[structopt(flatten)]
    pub service: ServiceIdentifier,

    #[structopt(short = "d", long = "data", parse(from_str = data_from_str))]
    /// Base64 encoded (pre-signed) DSF object
    pub data: Data,
}

impl PushOptions {
    pub fn new(id: Id, data: Vec<u8>) -> Self {
        Self {
            service: ServiceIdentifier {
                id: Some(id),
                index: None,
            },
            data,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PublishInfo {
    pub index: u16,
    //pub sig: Signature,
}