• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

facet-rs / facet / 15143556258

20 May 2025 05:04PM UTC coverage: 57.431% (+0.02%) from 57.413%
15143556258

Pull #653

github

web-flow
Merge ee4dc3ab9 into 165766df4
Pull Request #653: refactor(args): `ArgType` enum, various field helpers

182 of 205 new or added lines in 4 files covered. (88.78%)

1 existing line in 1 file now uncovered.

9398 of 16364 relevant lines covered (57.43%)

136.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.54
/facet-args/src/fields.rs
1
use alloc::string::ToString;
2
use facet_core::{FieldAttribute, Shape, Type, UserType};
3
use facet_deserialize::{DeserErrorKind, Scalar};
4
use facet_reflect::Wip;
5

6
pub(crate) fn validate_field<'facet, 'shape>(
20✔
7
    field_name: &str,
20✔
8
    shape: &'shape Shape<'shape>,
20✔
9
    wip: &Wip<'facet, 'shape>,
20✔
10
) -> Result<(), DeserErrorKind<'shape>> {
20✔
11
    if let Type::User(UserType::Struct(_)) = &shape.ty {
20✔
12
        if wip.field_index(field_name).is_none() {
20✔
13
            return Err(DeserErrorKind::UnknownField {
2✔
14
                field_name: field_name.to_string(),
2✔
15
                shape,
2✔
16
            });
2✔
17
        }
18✔
NEW
18
    }
×
19
    Ok(())
18✔
20
}
20✔
21

22
// Find a positional field
23
pub(crate) fn find_positional_field<'facet, 'shape>(
9✔
24
    shape: &'shape Shape<'shape>,
9✔
25
    wip: &Wip<'facet, 'shape>,
9✔
26
) -> Result<&'shape str, DeserErrorKind<'shape>> {
9✔
27
    if let Type::User(UserType::Struct(st)) = &shape.ty {
9✔
28
        for (idx, field) in st.fields.iter().enumerate() {
11✔
29
            for attr in field.attributes.iter() {
12✔
30
                if let FieldAttribute::Arbitrary(a) = attr {
12✔
31
                    if a.contains("positional") {
12✔
32
                        // Check if this field is already set
33
                        let is_set = wip.is_field_set(idx).unwrap_or(false);
7✔
34
                        if !is_set {
7✔
35
                            return Ok(field.name);
6✔
36
                        }
1✔
37
                    }
5✔
NEW
38
                }
×
39
            }
40
        }
NEW
41
    }
×
42
    Err(DeserErrorKind::UnknownField {
3✔
43
        field_name: "positional argument".to_string(),
3✔
44
        shape,
3✔
45
    })
3✔
46
}
9✔
47

48
// Find an unset boolean field for implicit false handling
49
pub(crate) fn find_unset_bool_field<'facet, 'shape>(
11✔
50
    shape: &'shape Shape<'shape>,
11✔
51
    wip: &Wip<'facet, 'shape>,
11✔
52
) -> Option<&'shape str> {
11✔
53
    if let Type::User(UserType::Struct(st)) = &shape.ty {
11✔
54
        for (idx, field) in st.fields.iter().enumerate() {
27✔
55
            if !wip.is_field_set(idx).unwrap_or(false) && field.shape().is_type::<bool>() {
27✔
56
                return Some(field.name);
1✔
57
            }
26✔
58
        }
NEW
59
    }
×
60
    None
10✔
61
}
11✔
62

63
pub(crate) fn find_field_by_short_flag<'shape>(
13✔
64
    key: &str,
13✔
65
    shape: &'shape Shape<'shape>,
13✔
66
) -> Result<&'shape str, DeserErrorKind<'shape>> {
13✔
67
    match &shape.ty {
13✔
68
        Type::User(UserType::Struct(st)) => st
13✔
69
            .fields
13✔
70
            .iter()
13✔
71
            .find(|field| {
26✔
72
                field.attributes.iter().any(|attr| {
46✔
73
                    matches!(attr, FieldAttribute::Arbitrary(a) if a.contains("short") &&
46✔
74
                            (a.contains(key) || (key.len() == 1 && field.name == key)))
20✔
75
                })
46✔
76
            })
26✔
77
            .map(|field| field.name)
13✔
78
            .ok_or_else(|| DeserErrorKind::UnknownField {
13✔
NEW
79
                field_name: key.to_string(),
×
NEW
80
                shape,
×
NEW
81
            }),
×
NEW
82
        _ => Err(DeserErrorKind::UnsupportedType {
×
NEW
83
            got: shape,
×
NEW
84
            wanted: "struct",
×
NEW
85
        }),
×
86
    }
87
}
13✔
88

89
// Create a missing value error
90
pub(crate) fn create_missing_value_error<'shape>(field: &str) -> DeserErrorKind<'shape> {
4✔
91
    DeserErrorKind::MissingValue {
4✔
92
        expected: "argument value",
4✔
93
        field: field.to_string(),
4✔
94
    }
4✔
95
}
4✔
96

97
// Handle boolean value parsing
98
pub(crate) fn handle_bool_value<'shape>(
5✔
99
    args_available: bool,
5✔
100
) -> Result<Scalar<'static>, DeserErrorKind<'shape>> {
5✔
101
    Ok(Scalar::Bool(args_available))
5✔
102
}
5✔
103

104
// Check if a value is available and valid (not a flag)
105
pub(crate) fn validate_value_available<'shape, 'input>(
33✔
106
    arg_idx: usize,
33✔
107
    args: &[&'input str],
33✔
108
) -> Result<&'input str, DeserErrorKind<'shape>> {
33✔
109
    if arg_idx >= args.len() {
33✔
110
        return Err(create_missing_value_error(args[arg_idx.saturating_sub(1)]));
2✔
111
    }
31✔
112

113
    let arg = args[arg_idx];
31✔
114
    if arg.starts_with('-') {
31✔
115
        return Err(create_missing_value_error(args[arg_idx.saturating_sub(1)]));
2✔
116
    }
29✔
117

118
    Ok(arg)
29✔
119
}
33✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc