@@ -13,31 +13,44 @@ pub enum DevMode {
1313}
1414
1515impl DevMode {
16- /// Determine the [`DevMode`] policy from the command-line arguments.
17- pub fn from_args ( dev : bool , no_dev : bool , only_dev : bool ) -> Self {
18- if only_dev {
19- Self :: Only
20- } else if no_dev {
21- Self :: Exclude
22- } else if dev {
23- Self :: Include
24- } else {
25- Self :: default ( )
16+ /// Iterate over the group names to include.
17+ pub fn iter ( & self ) -> impl Iterator < Item = & GroupName > {
18+ match self {
19+ Self :: Exclude => Either :: Left ( std:: iter:: empty ( ) ) ,
20+ Self :: Include | Self :: Only => Either :: Right ( std:: iter:: once ( & * DEV_DEPENDENCIES ) ) ,
2621 }
2722 }
23+
24+ /// Returns `true` if the specification allows for production dependencies.
25+ pub fn prod ( & self ) -> bool {
26+ matches ! ( self , Self :: Exclude | Self :: Include )
27+ }
2828}
2929
3030#[ derive( Debug , Clone ) ]
31- pub enum DevSpecification {
32- /// Include dev dependencies from the specified group.
31+ pub struct DevGroupsSpecification {
32+ /// Legacy option for `dependency-group.dev` and `tool.uv.dev-dependencies`.
33+ ///
34+ /// Requested via the `--dev`, `--no-dev`, and `--only-dev` flags.
35+ dev : Option < DevMode > ,
36+
37+ /// The groups to include.
38+ ///
39+ /// Requested via the `--group` and `--only-group` options.
40+ groups : GroupsSpecification ,
41+ }
42+
43+ #[ derive( Debug , Clone ) ]
44+ pub enum GroupsSpecification {
45+ /// Include dependencies from the specified groups.
3346 Include ( Vec < GroupName > ) ,
34- /// Do not include dev dependencies.
47+ /// Do not include dependencies from groups .
3548 Exclude ,
36- /// Include dev dependencies from the specified groups, and exclude all non-dev dependencies.
49+ /// Only include dependencies from the specified groups, exclude all other dependencies.
3750 Only ( Vec < GroupName > ) ,
3851}
3952
40- impl DevSpecification {
53+ impl GroupsSpecification {
4154 /// Returns an [`Iterator`] over the group names to include.
4255 pub fn iter ( & self ) -> impl Iterator < Item = & GroupName > {
4356 match self {
@@ -52,48 +65,92 @@ impl DevSpecification {
5265 }
5366}
5467
55- impl From < DevMode > for DevSpecification {
56- fn from ( mode : DevMode ) -> Self {
57- match mode {
58- DevMode :: Include => Self :: Include ( vec ! [ DEV_DEPENDENCIES . clone ( ) ] ) ,
59- DevMode :: Exclude => Self :: Exclude ,
60- DevMode :: Only => Self :: Only ( vec ! [ DEV_DEPENDENCIES . clone ( ) ] ) ,
68+ impl DevGroupsSpecification {
69+ /// Returns an [`Iterator`] over the group names to include.
70+ pub fn iter ( & self ) -> impl Iterator < Item = & GroupName > {
71+ match self . dev {
72+ None => Either :: Left ( self . groups . iter ( ) ) ,
73+ Some ( ref dev_mode ) => Either :: Right ( self . groups . iter ( ) . chain ( dev_mode . iter ( ) ) ) ,
6174 }
6275 }
63- }
6476
65- impl DevSpecification {
66- /// Determine the [`DevSpecification`] policy from the command-line arguments.
77+ /// Determine the [`DevGroupsSpecification`] policy from the command-line arguments.
6778 pub fn from_args (
6879 dev : bool ,
6980 no_dev : bool ,
7081 only_dev : bool ,
7182 group : Vec < GroupName > ,
7283 only_group : Vec < GroupName > ,
7384 ) -> Self {
74- let from_mode = DevSpecification :: from ( DevMode :: from_args ( dev, no_dev, only_dev) ) ;
75- if !group. is_empty ( ) {
76- match from_mode {
77- DevSpecification :: Exclude => Self :: Include ( group) ,
78- DevSpecification :: Include ( dev) => {
79- Self :: Include ( group. into_iter ( ) . chain ( dev) . collect ( ) )
80- }
81- DevSpecification :: Only ( _) => {
82- unreachable ! ( "cannot specify both `--only-dev` and `--group`" )
83- }
84- }
85+ let dev_mode = if only_dev {
86+ Some ( DevMode :: Only )
87+ } else if no_dev {
88+ Some ( DevMode :: Exclude )
89+ } else if dev {
90+ Some ( DevMode :: Include )
91+ } else {
92+ None
93+ } ;
94+
95+ let groups = if !group. is_empty ( ) {
96+ if matches ! ( dev_mode, Some ( DevMode :: Only ) ) {
97+ unreachable ! ( "cannot specify both `--only-dev` and `--group`" )
98+ } ;
99+ GroupsSpecification :: Include ( group)
85100 } else if !only_group. is_empty ( ) {
86- match from_mode {
87- DevSpecification :: Exclude => Self :: Only ( only_group) ,
88- DevSpecification :: Only ( dev) => {
89- Self :: Only ( only_group. into_iter ( ) . chain ( dev) . collect ( ) )
90- }
91- // TODO(zanieb): `dev` defaults to true we can't tell if `--dev` was provided in
92- // conflict with `--only-group` here
93- DevSpecification :: Include ( _) => Self :: Only ( only_group) ,
94- }
101+ if matches ! ( dev_mode, Some ( DevMode :: Include ) ) {
102+ unreachable ! ( "cannot specify both `--dev` and `--only-group`" )
103+ } ;
104+ GroupsSpecification :: Only ( only_group)
95105 } else {
96- from_mode
106+ GroupsSpecification :: Exclude
107+ } ;
108+
109+ Self {
110+ dev : dev_mode,
111+ groups,
112+ }
113+ }
114+
115+ /// Return a new [`DevGroupsSpecification`] with development dependencies included by default.
116+ ///
117+ /// This is appropriate in projects, where the `dev` group is synced by default.
118+ #[ must_use]
119+ pub fn with_default_dev ( self ) -> Self {
120+ match self . dev {
121+ Some ( _) => self ,
122+ None => match self . groups {
123+ // Only include the default `dev` group if `--only-group` wasn't used
124+ GroupsSpecification :: Only ( _) => self ,
125+ GroupsSpecification :: Exclude | GroupsSpecification :: Include ( _) => Self {
126+ dev : Some ( DevMode :: Include ) ,
127+ ..self
128+ } ,
129+ } ,
97130 }
98131 }
132+
133+ /// Returns `true` if the specification allows for production dependencies.
134+ pub fn prod ( & self ) -> bool {
135+ ( self . dev . is_none ( ) || self . dev . as_ref ( ) . is_some_and ( DevMode :: prod) ) && self . groups . prod ( )
136+ }
137+
138+ pub fn dev_mode ( & self ) -> Option < & DevMode > {
139+ self . dev . as_ref ( )
140+ }
141+ }
142+
143+ impl From < DevMode > for DevGroupsSpecification {
144+ fn from ( dev : DevMode ) -> Self {
145+ Self {
146+ dev : Some ( dev) ,
147+ groups : GroupsSpecification :: Exclude ,
148+ }
149+ }
150+ }
151+
152+ impl From < GroupsSpecification > for DevGroupsSpecification {
153+ fn from ( groups : GroupsSpecification ) -> Self {
154+ Self { dev : None , groups }
155+ }
99156}
0 commit comments