Selaa lähdekoodia

Merge branch 'master' into master

GnoCiYeH 1 vuosi sitten
vanhempi
commit
4b892e8b98

+ 7 - 0
.vscode/launch.json

@@ -0,0 +1,7 @@
+{
+    // 使用 IntelliSense 了解相关属性。 
+    // 悬停以查看现有属性的描述。
+    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
+    "version": "0.2.0",
+    "configurations": []
+}

+ 1 - 1
parse_test/test.service

@@ -17,4 +17,4 @@ TimeoutStartSec=10s
 TimeoutStopSec=10s
 
 [Install]
-#WantedBy=multi-user.target
+#WantedBy=multi-user.target

+ 0 - 1
src/error/mod.rs

@@ -25,7 +25,6 @@ pub enum ParseErrorType {
     /// 不是目录
     ENODIR,
 }
-
 /// 错误信息应该包括错误类型ParseErrorType,当前解析的文件名,当前解析的行号
 #[derive(Debug, PartialEq, Eq, Clone)]
 pub struct ParseError(ParseErrorType,String,usize);

+ 0 - 1
src/parse/mod.rs

@@ -5,7 +5,6 @@ use crate::{
     unit::{service::ServiceUnitAttr, BaseUnitAttr, InstallUnitAttr, UnitType},
 };
 
-
 #[cfg(target_os = "dragonos")]
 use drstd as std;
 

+ 165 - 0
src/parse/parse_base_unit/mod.rs

@@ -0,0 +1,165 @@
+use crate::{
+    error::ParseError,
+    unit::{target::TargetUnit, BaseUnitAttr, InstallPart, UnitPart},
+};
+//use drstd as std;
+use std::rc::Rc;
+use std::string::String;
+use std::vec::Vec;
+
+use super::{parse_util::UnitParseUtil, InstallUnitAttr};
+
+//Unit同有部分的解析器
+pub struct BaseUnitParser;
+
+impl BaseUnitParser {
+    /// @brief 为Unit解析Unit段属性并添加
+    ///
+    /// 为Unit解析Unit段属性并添加
+    ///
+    /// @param unit_part UnitPart(Unit段结构体),解析结果将存在该结构体
+    /// 
+    /// @param attr BaseUnitAttr,Unit段的属性抽象
+    /// 
+    /// @param val 属性值
+    ///
+    /// @return 成功则返回Ok(()),否则返回Err
+    pub fn parse_and_set_base_unit_attribute(
+        unit_part: &mut UnitPart,
+        attr: &BaseUnitAttr,
+        val: &str,
+    ) -> Result<(), ParseError> {
+        match attr {
+            BaseUnitAttr::None => {
+                return Err(ParseError::ESyntaxError);
+            }
+            BaseUnitAttr::Description => unit_part.description = String::from(val),
+            BaseUnitAttr::Documentation => unit_part
+                .documentation
+                .extend(UnitParseUtil::parse_url(val)?),
+            BaseUnitAttr::Requires => {
+                let units = val.split_whitespace().collect::<Vec<&str>>();
+                //TODO:目前先加入requires列表,可能会出现循环依赖问题,后续应解决循环依赖问题
+                for unit_path in units {
+                    unit_part
+                        .requires
+                        .push(UnitParseUtil::parse_unit_no_type(unit_path)?);
+                }
+            }
+            BaseUnitAttr::Wants => {
+                let units = val.split_whitespace().collect::<Vec<&str>>();
+                //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
+                for unit_path in units {
+                    unit_part
+                        .wants
+                        .push(UnitParseUtil::parse_unit_no_type(unit_path)?);
+                }
+            }
+            BaseUnitAttr::After => {
+                let units = val.split_whitespace().collect::<Vec<&str>>();
+                //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
+                for unit_path in units {
+                    unit_part
+                        .after
+                        .push(UnitParseUtil::parse_unit_no_type(unit_path)?);
+                }
+            }
+            BaseUnitAttr::Before => {
+                let units = val.split_whitespace().collect::<Vec<&str>>();
+                //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
+                for unit_path in units {
+                    unit_part
+                        .before
+                        .push(UnitParseUtil::parse_unit_no_type(unit_path)?);
+                }
+            }
+            BaseUnitAttr::BindsTo => {
+                let units = val.split_whitespace().collect::<Vec<&str>>();
+                //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
+                for unit_path in units {
+                    unit_part
+                        .binds_to
+                        .push(UnitParseUtil::parse_unit_no_type(unit_path)?);
+                }
+            }
+            BaseUnitAttr::PartOf => {
+                let units = val.split_whitespace().collect::<Vec<&str>>();
+                //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
+                for unit_path in units {
+                    unit_part
+                        .part_of
+                        .push(UnitParseUtil::parse_unit_no_type(unit_path)?);
+                }
+            }
+            BaseUnitAttr::OnFailure => {
+                let units = val.split_whitespace().collect::<Vec<&str>>();
+                //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
+                for unit_path in units {
+                    unit_part
+                        .on_failure
+                        .push(UnitParseUtil::parse_unit_no_type(unit_path)?);
+                }
+            }
+            BaseUnitAttr::Conflicts => {
+                let units = val.split_whitespace().collect::<Vec<&str>>();
+                //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
+                for unit_path in units {
+                    let unit = UnitParseUtil::parse_unit_no_type(unit_path)?;
+                    unit_part.conflicts.push(unit);
+                }
+            }
+        }
+        return Ok(());
+    }
+
+    /// @brief 为Unit解析Install段属性并添加
+    ///
+    /// 为Unit解析Install段属性并添加
+    ///
+    /// @param install_part InstallPart(Install段结构体),解析结果将存在该结构体
+    /// 
+    /// @param attr BaseUnitAttr,Unit段的属性抽象
+    /// 
+    /// @param val 属性值
+    ///
+    /// @return 成功则返回Ok(()),否则返回Err
+    pub fn parse_and_set_base_install_attribute(
+        install_part: &mut InstallPart,
+        attr: &InstallUnitAttr,
+        val: &str,
+    ) -> Result<(), ParseError> {
+        match attr {
+            InstallUnitAttr::RequiredBy => {
+                let units = val.split_whitespace().collect::<Vec<&str>>();
+                //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
+                for unit_path in units {
+                    let unit = UnitParseUtil::parse_unit::<TargetUnit>(unit_path)?;
+                    install_part.requires_by.push(unit);
+                }
+            }
+            InstallUnitAttr::Also => {
+                let units = val.split_whitespace().collect::<Vec<&str>>();
+                //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
+                for unit_path in units {
+                    let unit = UnitParseUtil::parse_unit_no_type(unit_path)?;
+                    install_part.also.push(unit);
+                }
+            }
+            InstallUnitAttr::WantedBy => {
+                let units = val.split_whitespace().collect::<Vec<&str>>();
+                //TODO:目前先加入列表,可能会出现循环依赖问题,后续应解决循环依赖问题
+                for unit_path in units {
+                    let unit = UnitParseUtil::parse_unit::<TargetUnit>(unit_path)?;
+                    install_part.wanted_by.push(unit);
+                }
+            }
+            InstallUnitAttr::Alias => {
+                install_part.alias = String::from(val);
+            }
+            InstallUnitAttr::None => {
+                return Err(ParseError::EINVAL);
+            }
+        }
+        return Ok(());
+    }
+}

+ 0 - 1
src/parse/parse_util/mod.rs

@@ -673,7 +673,6 @@ impl UnitParseUtil {
         //计算ns
         return Ok(integer * factor + (frac * factor) / (10u64.pow(frac.to_string().len() as u32)));
     }
-
     /// @brief 判断对应路径是否为目录
     ///
     /// @param path 路径

+ 1 - 1
src/unit/mod.rs

@@ -136,6 +136,7 @@ impl BaseUnit {
     pub fn parse_and_set_attribute(&self) -> Result<(), ParseError> {
         return Ok(());
     }
+}
 
     pub fn unit_part(&self) -> &UnitPart {
         &self.unit_part
@@ -386,7 +387,6 @@ impl InstallPart {
         &self.alias
     }
 }
-
 //对应Unit文件的各种属性
 pub enum BaseUnitAttr {
     None,

+ 0 - 1
src/unit/service/mod.rs

@@ -7,7 +7,6 @@ use crate::task::cmdtask::CmdTask;
 
 #[cfg(target_os = "dragonos")]
 use drstd as std;
-
 use std::rc::Rc;
 use std::string::String;
 use std::vec::Vec;