Skip to main content

rib/
type_parameter.rs

1use crate::type_parameter_parser::type_parameter;
2use combine::stream::position;
3use combine::EasyParser;
4use std::fmt;
5use std::fmt::Display;
6
7// The type parameter which can be part of instance creation or worker function call
8#[derive(Debug, Hash, Clone, Eq, PartialEq, PartialOrd, Ord)]
9pub enum TypeParameter {
10    Interface(InterfaceName),
11    PackageName(PackageName),
12    FullyQualifiedInterface(FullyQualifiedInterfaceName),
13}
14
15impl TypeParameter {
16    pub fn get_package_name(&self) -> Option<PackageName> {
17        match self {
18            TypeParameter::Interface(_) => None,
19            TypeParameter::PackageName(package) => Some(package.clone()),
20            TypeParameter::FullyQualifiedInterface(qualified) => {
21                Some(qualified.package_name.clone())
22            }
23        }
24    }
25
26    pub fn get_interface_name(&self) -> Option<InterfaceName> {
27        match self {
28            TypeParameter::Interface(interface) => Some(interface.clone()),
29            TypeParameter::PackageName(_) => None,
30            TypeParameter::FullyQualifiedInterface(qualified) => {
31                Some(qualified.interface_name.clone())
32            }
33        }
34    }
35
36    pub fn from_text(input: &str) -> Result<TypeParameter, String> {
37        type_parameter()
38            .easy_parse(position::Stream::new(input))
39            .map(|t| t.0)
40            .map_err(|err| format!("Invalid type parameter type {err}"))
41    }
42}
43
44impl Display for TypeParameter {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            TypeParameter::Interface(interface) => write!(f, "{interface}"),
48            TypeParameter::PackageName(package) => write!(f, "{package}"),
49            TypeParameter::FullyQualifiedInterface(qualified) => write!(f, "{qualified}"),
50        }
51    }
52}
53
54// foo@1.0.0
55#[derive(Debug, Hash, Clone, Eq, PartialEq, PartialOrd, Ord)]
56pub struct InterfaceName {
57    pub name: String,
58    pub version: Option<String>,
59}
60
61impl Display for InterfaceName {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        write!(f, "{}", self.name)?;
64        if let Some(version) = &self.version {
65            write!(f, "@{version}")?;
66        }
67        Ok(())
68    }
69}
70
71// ns2:pkg2@1.0.0
72#[derive(Debug, Hash, Clone, Eq, PartialEq, PartialOrd, Ord)]
73pub struct PackageName {
74    pub namespace: String,
75    pub package_name: String,
76    pub version: Option<String>,
77}
78
79impl Display for PackageName {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        write!(f, "{}:{}", self.namespace, self.package_name)?;
82        if let Some(version) = &self.version {
83            write!(f, "@{version}")?;
84        }
85        Ok(())
86    }
87}
88
89// ns2:pkg2/foo@1.0.0
90#[derive(Debug, Hash, Clone, Eq, PartialEq, PartialOrd, Ord)]
91pub struct FullyQualifiedInterfaceName {
92    pub package_name: PackageName,
93    pub interface_name: InterfaceName,
94}
95
96impl Display for FullyQualifiedInterfaceName {
97    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98        write!(f, "{}/{}", self.package_name, self.interface_name)
99    }
100}