Skip to main content

rib/
type_parameter.rs

1// Copyright 2024-2025 Golem Cloud
2//
3// Licensed under the Golem Source License v1.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://license.golem.cloud/LICENSE
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::type_parameter_parser::type_parameter;
16use combine::stream::position;
17use combine::EasyParser;
18use std::fmt;
19use std::fmt::Display;
20
21// The type parameter which can be part of instance creation or worker function call
22#[derive(Debug, Hash, Clone, Eq, PartialEq, PartialOrd, Ord)]
23pub enum TypeParameter {
24    Interface(InterfaceName),
25    PackageName(PackageName),
26    FullyQualifiedInterface(FullyQualifiedInterfaceName),
27}
28
29impl TypeParameter {
30    pub fn get_package_name(&self) -> Option<PackageName> {
31        match self {
32            TypeParameter::Interface(_) => None,
33            TypeParameter::PackageName(package) => Some(package.clone()),
34            TypeParameter::FullyQualifiedInterface(qualified) => {
35                Some(qualified.package_name.clone())
36            }
37        }
38    }
39
40    pub fn get_interface_name(&self) -> Option<InterfaceName> {
41        match self {
42            TypeParameter::Interface(interface) => Some(interface.clone()),
43            TypeParameter::PackageName(_) => None,
44            TypeParameter::FullyQualifiedInterface(qualified) => {
45                Some(qualified.interface_name.clone())
46            }
47        }
48    }
49
50    pub fn from_text(input: &str) -> Result<TypeParameter, String> {
51        type_parameter()
52            .easy_parse(position::Stream::new(input))
53            .map(|t| t.0)
54            .map_err(|err| format!("Invalid type parameter type {err}"))
55    }
56}
57
58impl Display for TypeParameter {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        match self {
61            TypeParameter::Interface(interface) => write!(f, "{interface}"),
62            TypeParameter::PackageName(package) => write!(f, "{package}"),
63            TypeParameter::FullyQualifiedInterface(qualified) => write!(f, "{qualified}"),
64        }
65    }
66}
67
68// foo@1.0.0
69#[derive(Debug, Hash, Clone, Eq, PartialEq, PartialOrd, Ord)]
70pub struct InterfaceName {
71    pub name: String,
72    pub version: Option<String>,
73}
74
75impl Display for InterfaceName {
76    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77        write!(f, "{}", self.name)?;
78        if let Some(version) = &self.version {
79            write!(f, "@{version}")?;
80        }
81        Ok(())
82    }
83}
84
85// ns2:pkg2@1.0.0
86#[derive(Debug, Hash, Clone, Eq, PartialEq, PartialOrd, Ord)]
87pub struct PackageName {
88    pub namespace: String,
89    pub package_name: String,
90    pub version: Option<String>,
91}
92
93impl Display for PackageName {
94    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95        write!(f, "{}:{}", self.namespace, self.package_name)?;
96        if let Some(version) = &self.version {
97            write!(f, "@{version}")?;
98        }
99        Ok(())
100    }
101}
102
103// ns2:pkg2/foo@1.0.0
104#[derive(Debug, Hash, Clone, Eq, PartialEq, PartialOrd, Ord)]
105pub struct FullyQualifiedInterfaceName {
106    pub package_name: PackageName,
107    pub interface_name: InterfaceName,
108}
109
110impl Display for FullyQualifiedInterfaceName {
111    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112        write!(f, "{}/{}", self.package_name, self.interface_name)
113    }
114}