Skip to content

Commit 64aca20

Browse files
committed
Adding impl From for Wrapped Enums
Adding Default Signed-off-by: Larry Dewey <[email protected]>
1 parent 44303f5 commit 64aca20

File tree

7 files changed

+74
-28
lines changed

7 files changed

+74
-28
lines changed

src/comid.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ pub enum TagIdTypeChoice<'a> {
149149
Uuid(UuidType),
150150
}
151151

152+
impl<'a> From<&'a str> for TagIdTypeChoice<'a> {
153+
fn from(value: &'a str) -> Self {
154+
TagIdTypeChoice::Tstr(Tstr::from(value))
155+
}
156+
}
157+
152158
/// Information about an entity associated with the tag
153159
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
154160
#[repr(C)]
@@ -205,7 +211,7 @@ pub enum TagRelTypeChoice {
205211
}
206212

207213
/// Collection of different types of triples describing the module characteristics
208-
#[derive(Debug, Serialize, Deserialize, From)]
214+
#[derive(Default, Debug, Serialize, Deserialize, From)]
209215
#[repr(C)]
210216
pub struct TriplesMap<'a> {
211217
/// Optional reference triples that link to external references

src/core.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ pub type Int = i32;
6666
/// Integer is an alias for Int type
6767
pub type Integer = Int;
6868

69+
pub enum StrWrapper<'a, T> {
70+
Tstr(Tstr<'a>),
71+
Other(T),
72+
}
73+
6974
/// ExtensionMap represents the possible types that can be used in extensions
7075
#[derive(Debug, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq, From, TryFrom)]
7176
pub enum ExtensionMap<'a> {
@@ -86,7 +91,7 @@ pub enum ExtensionMap<'a> {
8691
}
8792

8893
/// UUID type representing a 16-byte unique identifier
89-
#[derive(Debug, Serialize, Deserialize, From, AsRef, AsMut, Constructor)]
94+
#[derive(Default, Debug, Serialize, Deserialize, From, AsRef, AsMut, Constructor)]
9095
pub struct UuidType {
9196
#[serde(flatten)]
9297
pub field: [u8; 16],
@@ -209,7 +214,7 @@ pub enum AttributeValue<'a> {
209214
}
210215

211216
/// Represents global attributes with optional language tag and arbitrary attributes
212-
#[derive(Debug, Clone, Default, Serialize, Deserialize, From, Constructor)]
217+
#[derive(Default, Debug, Clone, Serialize, Deserialize, From, Constructor)]
213218
#[repr(C)]
214219
pub struct GlobalAttributes<'a> {
215220
/// Optional language tag (ex. en_US)
@@ -311,7 +316,7 @@ pub struct CoseKey<'a> {
311316
pub extension: Option<ExtensionMap<'a>>,
312317
}
313318

314-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
319+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
315320
#[repr(C)]
316321
/// Raw value data structure with associated mask
317322
pub struct MaskedRawValue {

src/corim.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@
7676
//! and optional fields defined in the standard.
7777
7878
use crate::{
79-
core::Bytes, generate_tagged, Digest, ExtensionMap, Int, OidType, OneOrMany, TaggedBytes,
80-
TaggedConciseMidTag, TaggedConciseSwidTag, TaggedConciseTlTag, Text, Time, Tstr, Uri, UuidType,
79+
comid::ConciseMidTag, core::Bytes, coswid::ConciseSwidTag, cotl::ConciseTlTag, generate_tagged,
80+
Digest, ExtensionMap, Int, OidType, OneOrMany, TaggedBytes, TaggedConciseMidTag,
81+
TaggedConciseSwidTag, TaggedConciseTlTag, Text, Time, Tstr, Uri, UuidType,
8182
};
8283

8384
use derive_more::{Constructor, From, TryFrom};
@@ -153,6 +154,12 @@ pub enum CorimIdTypeChoice<'a> {
153154
Uuid(UuidType),
154155
}
155156

157+
impl<'a> From<&'a str> for CorimIdTypeChoice<'a> {
158+
fn from(s: &'a str) -> Self {
159+
CorimIdTypeChoice::Tstr(s.into())
160+
}
161+
}
162+
156163
/// Types of tags that can be included in a CoRIM
157164
#[repr(C)]
158165
#[derive(Debug, Serialize, Deserialize, From, TryFrom)]
@@ -165,6 +172,22 @@ pub enum ConciseTagTypeChoice<'a> {
165172
Tl(TaggedConciseTlTag<'a>),
166173
}
167174

175+
impl<'a> From<ConciseSwidTag<'a>> for ConciseTagTypeChoice<'a> {
176+
fn from(value: ConciseSwidTag<'a>) -> Self {
177+
Self::Swid(value.into())
178+
}
179+
}
180+
impl<'a> From<ConciseMidTag<'a>> for ConciseTagTypeChoice<'a> {
181+
fn from(value: ConciseMidTag<'a>) -> Self {
182+
Self::Mid(value.into())
183+
}
184+
}
185+
impl<'a> From<ConciseTlTag<'a>> for ConciseTagTypeChoice<'a> {
186+
fn from(value: ConciseTlTag<'a>) -> Self {
187+
Self::Tl(value.into())
188+
}
189+
}
190+
168191
/// Location and optional thumbprint of a dependent CoRIM
169192
#[repr(C)]
170193
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
@@ -188,7 +211,7 @@ pub enum ProfileTypeChoice<'a> {
188211

189212
/// Defines the validity period for a CoRIM or signature
190213
#[repr(C)]
191-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
214+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
192215
pub struct ValidityMap {
193216
/// Optional start time of the validity period
194217
#[serde(rename = "not-before")]
@@ -249,7 +272,7 @@ pub struct COSESign1Corim<'a> {
249272
}
250273

251274
/// Protected header for a signed CoRIM
252-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
275+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
253276
#[repr(C)]
254277
pub struct ProtectedCorimHeaderMap<'a> {
255278
/// Algorithm identifier for the signature
@@ -269,7 +292,7 @@ pub struct ProtectedCorimHeaderMap<'a> {
269292
}
270293

271294
/// Metadata about the CoRIM signing operation
272-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
295+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
273296
#[repr(C)]
274297
pub struct CorimMetaMap<'a> {
275298
/// Information about the signer
@@ -280,7 +303,7 @@ pub struct CorimMetaMap<'a> {
280303
}
281304

282305
/// Information about the entity that signed the CoRIM
283-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
306+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
284307
#[repr(C)]
285308
pub struct CorimSignerMap<'a> {
286309
/// Name of the signing entity

src/coswid.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub struct ConciseSwidTag<'a> {
140140
/// useful for identification, deployment, or management purposes. All fields
141141
/// are optional except for global attributes.
142142
#[repr(C)]
143-
#[derive(Debug, Serialize, Deserialize, From)]
143+
#[derive(Default, Debug, Serialize, Deserialize, From)]
144144
pub struct SoftwareMetaEntry<'a> {
145145
/// Current activation status of the software (e.g., "trial", "full", "deleted")
146146
#[serde(skip_serializing_if = "Option::is_none")]
@@ -338,15 +338,15 @@ pub enum PayloadOrEvidence<'a> {
338338
}
339339

340340
/// Container for payload information
341-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
341+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
342342
#[repr(C)]
343343
pub struct Payload<'a> {
344344
/// The payload entry containing resource information
345345
payload: PayloadEntry<'a>,
346346
}
347347

348348
/// Detailed payload information about software resources
349-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
349+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
350350
#[repr(C)]
351351
pub struct PayloadEntry<'a> {
352352
/// Collection of resources in the software
@@ -367,7 +367,7 @@ pub struct PayloadEntry<'a> {
367367
/// This structure groups together all the resources that are part of the
368368
/// software, including files, directories, processes, and other resource types.
369369
/// It forms the core content description of what comprises the software.
370-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
370+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
371371
#[repr(C)]
372372
pub struct ResourceCollection<'a> {
373373
/// Group of filesystem path elements
@@ -387,7 +387,7 @@ pub struct ResourceCollection<'a> {
387387
}
388388

389389
/// Group of filesystem path elements in a resource collection
390-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
390+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
391391
#[repr(C)]
392392
pub struct PathElementsGroup<'a> {
393393
/// Optional list of directory entries
@@ -399,7 +399,7 @@ pub struct PathElementsGroup<'a> {
399399
}
400400

401401
/// Information about a directory in the filesystem
402-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
402+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
403403
#[repr(C)]
404404
pub struct DirectoryEntry<'a> {
405405
/// Basic filesystem item information
@@ -426,7 +426,7 @@ pub struct DirectoryEntry<'a> {
426426
}
427427

428428
/// Basic information about a filesystem item (file or directory)
429-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
429+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
430430
#[repr(C)]
431431
pub struct FileSystemItem<'a> {
432432
/// Indicates if this is a key/critical filesystem item
@@ -444,7 +444,7 @@ pub struct FileSystemItem<'a> {
444444
}
445445

446446
/// Information about a file in the filesystem
447-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
447+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
448448
#[repr(C)]
449449
pub struct FileEntry<'a> {
450450
/// Basic filesystem item information
@@ -471,7 +471,7 @@ pub struct FileEntry<'a> {
471471
}
472472

473473
/// Information about a running process
474-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
474+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
475475
#[repr(C)]
476476
pub struct ProcessEntry<'a> {
477477
/// Name of the process
@@ -490,7 +490,7 @@ pub struct ProcessEntry<'a> {
490490
}
491491

492492
/// Information about a general resource
493-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
493+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
494494
#[repr(C)]
495495
pub struct ResourceEntry<'a> {
496496
/// Type identifier for the resource
@@ -505,15 +505,15 @@ pub struct ResourceEntry<'a> {
505505
}
506506

507507
/// Container for evidence information about observed software state
508-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
508+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
509509
#[repr(C)]
510510
pub struct Evidence<'a> {
511511
/// The evidence entry containing observed resource information
512512
pub evidence: EvidenceEntry<'a>,
513513
}
514514

515515
/// Detailed evidence information about observed software state
516-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
516+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
517517
#[repr(C)]
518518
pub struct EvidenceEntry<'a> {
519519
/// Collection of observed resources

src/fixed_bytes.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ impl<const N: usize> std::fmt::Debug for FixedBytes<N> {
113113
}
114114
}
115115

116+
impl<const N: usize> Default for FixedBytes<N> {
117+
fn default() -> Self {
118+
Self([0; N])
119+
}
120+
}
121+
116122
#[cfg(test)]
117123
mod tests {
118124
use super::*;

src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
/// use corim_rs::generate_tagged;
4040
///
4141
/// // Define a simple type to wrap
42-
/// #[derive(Debug, PartialEq, Serialize, Deserialize)]
42+
/// #[derive(Default, Debug, PartialEq, Serialize, Deserialize)]
4343
/// pub struct MyType(u32);
4444
///
4545
/// // Generate the tagged wrapper

src/triples.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ pub struct EnvironmentMap<'a> {
126126
}
127127

128128
/// Classification information for an environment
129-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
129+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
130130
#[repr(C)]
131131
pub struct ClassMap<'a> {
132132
/// Optional class identifier
@@ -263,7 +263,7 @@ pub enum GroupIdTypeChoice {
263263
}
264264

265265
/// Map containing measurement values and metadata
266-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
266+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
267267
#[repr(C)]
268268
pub struct MeasurementMap<'a> {
269269
/// Optional measurement key identifier
@@ -290,8 +290,14 @@ pub enum MeasuredElementTypeChoice<'a> {
290290
Tstr(Tstr<'a>),
291291
}
292292

293+
impl<'a> From<&'a str> for MeasuredElementTypeChoice<'a> {
294+
fn from(value: &'a str) -> Self {
295+
Self::Tstr(value.into())
296+
}
297+
}
298+
293299
/// Collection of measurement values and attributes
294-
#[derive(Debug, Default, Serialize, Deserialize, From)]
300+
#[derive(Default, Debug, Serialize, Deserialize, From)]
295301
#[repr(C)]
296302
pub struct MeasurementValuesMap<'a> {
297303
/// Optional version information
@@ -341,7 +347,7 @@ pub struct MeasurementValuesMap<'a> {
341347
}
342348

343349
/// Version information with optional versioning scheme
344-
#[derive(Debug, Serialize, Deserialize, From, Constructor)]
350+
#[derive(Default, Debug, Serialize, Deserialize, From, Constructor)]
345351
#[repr(C)]
346352
pub struct VersionMap<'a> {
347353
/// Version identifier string
@@ -367,7 +373,7 @@ pub enum SvnTypeChoice {
367373
pub type DigestType<'a> = OneOrMany<Digest<'a>>;
368374

369375
/// Status flags indicating various security and configuration states
370-
#[derive(Debug, Serialize, Deserialize, From)]
376+
#[derive(Default, Debug, Serialize, Deserialize, From)]
371377
#[repr(C)]
372378
pub struct FlagsMap<'a> {
373379
/// Whether the environment is configured

0 commit comments

Comments
 (0)