ethercat_driver_ros2 安装 EtherLab
2026/4/27 21:18:25
这是一个 Rust 时间库中的组件范围错误类型,用于表示时间组件(如年、月、日、时、分、秒等)值超出允许范围的情况。
pubstructComponentRange{pub(crate)name:&'staticstr,// 组件名称pub(crate)minimum:i64,// 最小值(包含)pub(crate)maximum:i64,// 最大值(包含)pub(crate)value:i64,// 提供的值pub(crate)conditional_message:Option<&'staticstr>,// 条件性错误信息}设计选择:使用i64作为数值类型,因为它是最窄的、能满足所有用例的整数类型,避免了泛型参数的需要。
// 获取组件名称leterr=ComponentRange{name:"hour",minimum:0,maximum:23,value:25,conditional_message:None};assert_eq!(err.name(),"hour");// 检查是否为条件性错误letconditional_err=ComponentRange{name:"day",minimum:1,maximum:28,value:30,conditional_message:Some("in February")};assert!(conditional_err.is_conditional());implPartialEqforComponentRange{fneq(&self,other:&Self)->bool{self.name==other.name&&self.minimum==other.minimum&&self.maximum==other.maximum&&self.value==other.value// 注意:比较时只检查是否有条件信息,不比较具体内容&&self.conditional_message.is_some()==other.conditional_message.is_some()}}implhash::HashforComponentRange{fnhash<H:hash::Hasher>(&self,state:&mutH){// 同样的逻辑:条件信息只检查是否存在}}重要设计:比较和哈希时只检查conditional_message是否存在,不比较其内容。这是因为条件信息只是为了更好的错误提示,不影响错误的本质。
implfmt::DisplayforComponentRange{fnfmt(&self,f:&mutfmt::Formatter<'_>)->fmt::Result{// 基本格式:hour must be in the range 0..=23write!(f,"{} must be in the range {}..={}",self.name,self.minimum,self.maximum)?;// 如果有条件信息,追加:in FebruaryifletSome(message)=self.conditional_message{write!(f," {message}")?;}Ok(())}}示例输出:
hour must be in the range 0..=23day must be in the range 1..=28 in February// ComponentRange -> crate::ErrorimplFrom<ComponentRange>forcrate::Error{fnfrom(original:ComponentRange)->Self{Self::ComponentRange(original)}}// crate::Error -> ComponentRange (可能失败)implTryFrom<crate::Error>forComponentRange{typeError=error::DifferentVariant;fntry_from(err:crate::Error)->Result<Self,Self::Error>{matcherr{crate::Error::ComponentRange(err)=>Ok(err),_=>Err(error::DifferentVariant),// 如果不是 ComponentRange 变体}}}#[cfg(feature ="serde")]implserde_core::de::ExpectedforComponentRange{fnfmt(&self,f:&mutfmt::Formatter<'_>)->fmt::Result{write!(f,"a value in the range {}..={}",self.minimum,self.maximum)}}注意:文档指出这个实现是废弃的,将在未来的破坏性更新中移除。
#[cfg(feature ="serde")]implComponentRange{pub(crate)fninto_de_error<E:serde_core::de::Error>(self)->E{E::invalid_value(serde_core::de::Unexpected::Signed(self.value),&self)}}implcore::error::ErrorforComponentRange{}这使得ComponentRange可以作为标准错误类型使用,可以向上传播。
fncreate_time(hour:u8,minute:u8,second:u8)->Result<Time,ComponentRange>{ifhour>23{returnErr(ComponentRange{name:"hour",minimum:0,maximum:23,value:hourasi64,conditional_message:None,});}// ... 其他验证Ok(Time::from_hms(hour,minute,second)?)}fnvalidate_day(month:Month,day:u8)->Result<(),ComponentRange>{letmax_days=days_in_month(month);ifday>max_days{returnErr(ComponentRange{name:"day",minimum:1,maximum:max_daysasi64,value:dayasi64,conditional_message:Some(format!("in {}",month.name()).leak()),});}Ok(())}crate::Error转换时需要显式处理matchTime::from_hms(25,0,0){Ok(time)=>{/* 使用时间 */}Err(ComponentRange{name:"hour",minimum,maximum,value,..})=>{println!("小时值 {} 超出范围 {}..{}",value,minimum,maximum);}Err(other_error)=>{/* 处理其他错误 */}}这个错误类型是时间库中验证逻辑的核心部分,确保所有时间组件都在有效范围内,并提供清晰的错误信息以便调试。