1 use drstd::std as std; 2 use std::string::String; 3 use std::vec::Vec; 4 use std::boxed::Box; 5 use crate::error::SystemError; 6 7 mod service; 8 mod target; 9 10 use self::target::TargetUnit; 11 12 //所有可解析的Unit都应该实现该trait 13 pub trait Unit { 14 fn parse(path: &str) -> Self where Self: Sized; 15 } 16 17 enum UnitState { 18 Enabled, 19 Disabled, 20 Static, 21 Masked 22 } 23 24 enum UnitType { 25 Automount, 26 Device, 27 Mount, 28 Path, 29 Scope, 30 Service, 31 Slice, 32 Snapshot, 33 Socket, 34 Swap, 35 Target, 36 Timer, 37 } 38 39 //记录unit文件基本信息 40 struct BaseUnit { 41 unit_part: UnitPart, 42 install_part: InstallPart, 43 state: UnitState, 44 unit_type: UnitType 45 } 46 47 // impl Default for BaseUnit { 48 // fn default() -> Self { 49 // // BaseUnit { 50 // // unit_part: UnitPart::default(), 51 // // install_part: InstallPart::default(), 52 // // state: , unit_type: () } 53 // } 54 // } 55 56 struct Url { 57 protocol: String, 58 host: String, 59 port: u16, 60 path: String, 61 query: Option<String>, 62 fragment: Option<String>, 63 } 64 65 impl Url { 66 fn new(url_string: &str) -> Result<Self, SystemError> { 67 // 解析 URL 字符串并初始化结构体字段 68 // ... 69 70 // 验证 URL 合法性 71 // ... 72 73 // 返回 URL 结构体实例 74 // ... 75 Err(SystemError::EINVAL) 76 } 77 78 fn build(&self) -> String { 79 // 构建 URL 字符串 80 // ... 81 String::new() 82 } 83 } 84 85 struct UnitPart { 86 description: String, 87 documentation: Vec<Url>, 88 requires: Vec<Box<dyn Unit>>, 89 wants: Vec<Box<dyn Unit>>, 90 after: Vec<Box<dyn Unit>>, 91 before: Vec<Box<dyn Unit>>, 92 binds_to: Vec<Box<dyn Unit>>, 93 part_of: Vec<Box<dyn Unit>>, 94 on_failure: Vec<Box<dyn Unit>>, 95 conflicts: Vec<Box<dyn Unit>> 96 } 97 98 impl Default for UnitPart { 99 fn default() -> Self { 100 UnitPart { 101 description: String::new(), 102 documentation: Vec::new(), 103 requires: Vec::new(), 104 wants: Vec::new(), 105 after: Vec::new(), 106 before: Vec::new(), 107 binds_to: Vec::new(), 108 part_of: Vec::new(), 109 on_failure: Vec::new(), 110 conflicts: Vec::new() 111 } 112 } 113 } 114 115 struct InstallPart { 116 wanted_by: Vec<TargetUnit>, 117 requires_by: Vec<TargetUnit>, 118 also: Vec<Box<dyn Unit>>, 119 alias: String 120 } 121 122 impl Default for InstallPart { 123 fn default() -> Self { 124 InstallPart { 125 wanted_by: Vec::new(), 126 requires_by: Vec::new(), 127 also: Vec::new(), 128 alias: String::new() 129 } 130 } 131 } 132 133 struct CmdTask{ 134 path: String, 135 cmd: String 136 }