From c696bc4257355f2e06275900fd1e1c6d4756f9ca Mon Sep 17 00:00:00 2001
From: Xiaoguang Li <lixiaoguang2@huawei.com>
Date: Thu, 25 Aug 2022 20:45:55 +0800
Subject: [PATCH 3/4] if Type item is not configured, set it to default Simple

Signed-off-by: Xiaoguang Li <lixiaoguang2@huawei.com>
---
 components/service/src/service_base.rs   | 20 +++++++++++++++++++-
 components/service/src/service_config.rs | 10 ++++------
 components/service/src/service_mng.rs    |  6 +-----
 3 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/components/service/src/service_base.rs b/components/service/src/service_base.rs
index 529334c..8f79ca6 100755
--- a/components/service/src/service_base.rs
+++ b/components/service/src/service_base.rs
@@ -1,4 +1,5 @@
-use serde::{Deserialize, Serialize};
+use process1::manager::DeserializeWith;
+use serde::{Deserialize, Deserializer, Serialize};
 
 #[derive(PartialEq, EnumString, Display, Debug)]
 pub(super) enum ServiceTimeoutFailureMode {
@@ -73,6 +74,23 @@ impl Default for ServiceType {
     }
 }
 
+impl DeserializeWith for ServiceType {
+    fn deserialize_with<'de, D>(de: D) -> Result<Self, D::Error>
+    where
+        D: Deserializer<'de>,
+    {
+        let s = String::deserialize(de)?;
+
+        match s.as_ref() {
+            "simple" => Ok(ServiceType::Simple),
+            "forking" => Ok(ServiceType::Forking),
+            "oneshot" => Ok(ServiceType::Oneshot),
+            "notify" => Ok(ServiceType::Notify),
+            &_ => Ok(ServiceType::Simple),
+        }
+    }
+}
+
 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy, Clone)]
 pub(super) enum ServiceCommand {
     Condition,
diff --git a/components/service/src/service_config.rs b/components/service/src/service_config.rs
index 9932fe8..0c740e6 100755
--- a/components/service/src/service_config.rs
+++ b/components/service/src/service_config.rs
@@ -44,11 +44,7 @@ impl ServiceConfig {
     }
 
     pub(super) fn service_type(&self) -> ServiceType {
-        self.data
-            .borrow()
-            .Service
-            .Type
-            .map_or(ServiceType::Simple, |e| e)
+        self.data.borrow().Service.Type
     }
 
     pub(super) fn set_notify_access(&self, v: NotifyAccess) {
@@ -84,7 +80,9 @@ impl ServiceConfigData {
 
 #[derive(Config, Default, Debug)]
 pub(super) struct SectionService {
-    pub Type: Option<ServiceType>,
+    #[config(deserialize_with = ServiceType::deserialize_with)]
+    #[config(default = "simple")]
+    pub Type: ServiceType,
     #[config(deserialize_with = Vec::<ExecCommand>::deserialize_with)]
     pub ExecStart: Option<Vec<ExecCommand>>,
     #[config(deserialize_with = Vec::<ExecCommand>::deserialize_with)]
diff --git a/components/service/src/service_mng.rs b/components/service/src/service_mng.rs
index 0a42cfa..b8ac9f2 100755
--- a/components/service/src/service_mng.rs
+++ b/components/service/src/service_mng.rs
@@ -176,11 +176,7 @@ impl ServiceMng {
     }
 
     pub(super) fn current_active_state(&self) -> UnitActiveState {
-        if let Some(service_type) = self.config.config_data().borrow().Service.Type {
-            service_state_to_unit_state(service_type, self.state())
-        } else {
-            UnitActiveState::UnitFailed
-        }
+        service_state_to_unit_state(self.config.service_type(), self.state())
     }
 
     fn enter_contion(&self) {
-- 
2.30.2

