Mountain/Environment/LanguageFeatureProvider/
Registration.rs

1//! Provider registration and unregistration logic.
2
3use CommonLibrary::{Error::CommonError::CommonError, LanguageFeature::DTO::ProviderType::ProviderType};
4use log::warn;
5use serde_json::Value;
6
7use crate::{
8	ApplicationState::DTO::ProviderRegistrationDTO::ProviderRegistrationDTO,
9	Environment::Utility::ErrorMapping::MapApplicationStateLockErrorToCommonError,
10};
11
12pub(super) async fn register_provider(
13	environment:&crate::Environment::MountainEnvironment::MountainEnvironment,
14	side_car_identifier:String,
15	provider_type:ProviderType,
16	selector_dto:Value,
17	extension_identifier_dto:Value,
18	options_dto:Option<Value>,
19) -> Result<u32, CommonError> {
20	let handle = environment.ApplicationState.GetNextProviderHandle();
21	let new_registration = ProviderRegistrationDTO {
22		Handle:handle,
23		ProviderType:provider_type,
24		Selector:selector_dto,
25		SideCarIdentifier:side_car_identifier,
26		ExtensionIdentifier:extension_identifier_dto,
27		Options:options_dto,
28	};
29	environment
30		.ApplicationState
31		.Extension
32		.ProviderRegistration
33		.LanguageProviders
34		.lock()
35		.map_err(MapApplicationStateLockErrorToCommonError)?
36		.insert(handle, new_registration);
37	Ok(handle)
38}
39
40pub(super) async fn unregister_provider(
41	environment:&crate::Environment::MountainEnvironment::MountainEnvironment,
42	handle:u32,
43) -> Result<(), CommonError> {
44	let mut providers = environment
45		.ApplicationState
46		.Extension
47		.ProviderRegistration
48		.LanguageProviders
49		.lock()
50		.map_err(MapApplicationStateLockErrorToCommonError)?;
51	if providers.remove(&handle).is_none() {
52		warn!("Attempted to unregister non-existent provider handle: {}", handle);
53	}
54	Ok(())
55}