reproto_core/
flavor.rs

1//! The flavor of RpIR being used.
2
3use errors::Result;
4use std::borrow::Cow;
5use std::cmp;
6use std::fmt;
7use std::hash;
8use {Loc, RpEndpoint, RpEnumType, RpField, RpName, RpPackage, RpType, RpVersionedPackage};
9
10pub trait FlavorField: fmt::Debug + Clone {
11    /// Indicates if the field is discriminating in an untagged context.
12    fn is_discriminating(&self) -> bool;
13}
14
15pub trait AsPackage
16where
17    Self: Sized,
18{
19    /// Attempt to treat the current object as a package.
20    fn try_as_package<'a>(&'a self) -> Result<Cow<'a, RpPackage>>;
21
22    /// Attempt to prefix the package.
23    fn prefix_with(self, prefix: RpPackage) -> Self;
24}
25
26/// The flavor of intermediate representation being used.
27pub trait Flavor: fmt::Debug + Clone + cmp::Eq + hash::Hash {
28    /// The type that this flavor serializes to.
29    type Type: fmt::Debug + Clone + cmp::Eq;
30    /// The local field name.
31    type Name: fmt::Display + fmt::Debug + Clone + cmp::Eq;
32    /// The field that this flavor serializes to.
33    type Field: FlavorField;
34    /// The endpoint that this flavor serializes to.
35    type Endpoint: fmt::Debug + Clone;
36    /// The package type.
37    type Package: fmt::Debug + Clone + cmp::Eq + cmp::Ord + hash::Hash + AsPackage;
38    /// Enum type.
39    type EnumType: fmt::Debug + Clone + cmp::Eq;
40}
41
42/// The first flavor where packages are fully qualified.
43#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Hash)]
44pub struct CoreFlavor;
45
46impl Flavor for CoreFlavor {
47    type Type = RpType<CoreFlavor>;
48    type Name = Loc<RpName<CoreFlavor>>;
49    type Field = RpField<CoreFlavor>;
50    type Endpoint = RpEndpoint<CoreFlavor>;
51    type Package = RpVersionedPackage;
52    type EnumType = RpEnumType;
53}