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
//! The flavor of RpIR being used.

use errors::Result;
use std::cmp;
use std::fmt;
use std::hash;
use {RpEndpoint, RpField, RpName, RpPackage, RpType, RpVersionedPackage};

pub trait FlavorField: fmt::Debug + Clone {
    /// Indicates if the field is discriminating in an untagged context.
    fn is_discriminating(&self) -> bool;
}

pub trait AsPackage
where
    Self: Sized,
{
    /// Attempt to treat the current object as a package.
    fn try_as_package(&self) -> Result<&RpPackage>;

    /// Attempt to prefix the package.
    fn prefix_with(self, prefix: RpPackage) -> Self;
}

/// The flavor of intermediate representation being used.
pub trait Flavor: fmt::Debug + Clone + cmp::Eq + hash::Hash {
    /// The type that this flavor serializes to.
    type Type: fmt::Debug + Clone + cmp::Eq;
    /// The local field name.
    type Name: fmt::Display + fmt::Debug + Clone + cmp::Eq;
    /// The field that this flavor serializes to.
    type Field: FlavorField;
    /// The endpoint that this flavor serializes to.
    type Endpoint: fmt::Debug + Clone;
    /// The package type.
    type Package: fmt::Debug + Clone + cmp::Eq + cmp::Ord + hash::Hash + AsPackage;
}

/// The first flavor where packages are fully qualified.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Hash)]
pub struct CoreFlavor;

impl Flavor for CoreFlavor {
    type Type = RpType<CoreFlavor>;
    type Name = RpName<CoreFlavor>;
    type Field = RpField<CoreFlavor>;
    type Endpoint = RpEndpoint<CoreFlavor>;
    type Package = RpVersionedPackage;
}