lux-lib 0.12.0

Library for the lux package manager for Lua
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
use itertools::Itertools;
use mlua::{ExternalResult, FromLua, IntoLua, LuaSerdeExt};
use serde::{de, Deserialize, Deserializer, Serialize};
use std::{cmp::Ordering, fmt::Display, str::FromStr};
use thiserror::Error;

mod outdated;
mod version;

pub use outdated::*;
pub use version::{
    PackageVersion, PackageVersionParseError, PackageVersionReq, PackageVersionReqError,
    VersionReqToVersionError,
};

use crate::{
    lockfile::RemotePackageSourceUrl,
    lua_rockspec::{DisplayAsLuaKV, DisplayLuaKV, DisplayLuaValue},
    remote_package_source::RemotePackageSource,
    rockspec::lua_dependency::LuaDependencySpec,
    variables::HasVariables,
};

#[derive(Debug, Error)]
pub enum PackageSpecFromPackageReqError {
    #[error("invalid version for rock {rock}: {err}")]
    Version {
        rock: PackageName,
        err: VersionReqToVersionError,
    },
}

#[derive(Clone, Debug)]
#[cfg_attr(feature = "clap", derive(clap::Args))]
pub struct PackageSpec {
    name: PackageName,
    version: PackageVersion,
}

impl PackageSpec {
    pub fn new(name: PackageName, version: PackageVersion) -> Self {
        Self { name, version }
    }
    pub fn parse(name: String, version: String) -> Result<Self, PackageVersionParseError> {
        Ok(Self::new(
            PackageName::new(name),
            PackageVersion::parse(&version)?,
        ))
    }
    pub fn name(&self) -> &PackageName {
        &self.name
    }
    pub fn version(&self) -> &PackageVersion {
        &self.version
    }
    pub fn into_package_req(self) -> PackageReq {
        PackageReq {
            name: self.name,
            version_req: self.version.into_version_req(),
        }
    }
}

impl TryFrom<PackageReq> for PackageSpec {
    type Error = PackageSpecFromPackageReqError;

    fn try_from(value: PackageReq) -> Result<Self, Self::Error> {
        let name = value.name;
        let version = value.version_req.try_into().map_err(|err| {
            PackageSpecFromPackageReqError::Version {
                rock: name.clone(),
                err,
            }
        })?;
        Ok(Self { name, version })
    }
}

impl FromLua for PackageSpec {
    fn from_lua(
        value: mlua::prelude::LuaValue,
        lua: &mlua::prelude::Lua,
    ) -> mlua::prelude::LuaResult<Self> {
        let (name, version) = lua.from_value(value)?;

        Self::parse(name, version).into_lua_err()
    }
}

impl mlua::UserData for PackageSpec {
    fn add_fields<F: mlua::UserDataFields<Self>>(fields: &mut F) {
        fields.add_field_method_get("name", |_, this| Ok(this.name.to_string()));
        fields.add_field_method_get("version", |_, this| Ok(this.version.to_string()));
    }

    fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
        methods.add_method("to_package_req", |_, this, ()| {
            Ok(this.clone().into_package_req())
        })
    }
}

impl HasVariables for PackageSpec {
    fn get_variable(&self, input: &str) -> Option<String> {
        match input {
            "PACKAGE" => Some(self.name.to_string()),
            "VERSION" => Some(self.version.to_string()),
            _ => None,
        }
    }
}

#[derive(Clone, Debug)]
pub(crate) struct RemotePackage {
    pub package: PackageSpec,
    pub source: RemotePackageSource,
    /// `Some` if present in a lockfile
    pub source_url: Option<RemotePackageSourceUrl>,
}

impl RemotePackage {
    pub fn new(
        package: PackageSpec,
        source: RemotePackageSource,
        source_url: Option<RemotePackageSourceUrl>,
    ) -> Self {
        Self {
            package,
            source,
            source_url,
        }
    }
}

#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub(crate) enum RemotePackageType {
    Rockspec,
    Src,
    Binary,
}

impl Ord for RemotePackageType {
    fn cmp(&self, other: &Self) -> Ordering {
        // Priority: binary > rockspec > src
        match (self, other) {
            (Self::Binary, Self::Binary)
            | (Self::Rockspec, Self::Rockspec)
            | (Self::Src, Self::Src) => Ordering::Equal,

            (Self::Binary, _) => Ordering::Greater,
            (_, Self::Binary) => Ordering::Less,
            (Self::Rockspec, Self::Src) => Ordering::Greater,
            (Self::Src, Self::Rockspec) => Ordering::Less,
        }
    }
}

impl PartialOrd for RemotePackageType {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

#[derive(Clone)]
pub struct RemotePackageTypeFilterSpec {
    /// Include Rockspec
    pub rockspec: bool,
    /// Include Src
    pub src: bool,
    /// Include Binary
    pub binary: bool,
}

impl Default for RemotePackageTypeFilterSpec {
    fn default() -> Self {
        Self {
            rockspec: true,
            src: true,
            binary: true,
        }
    }
}

#[derive(Error, Debug)]
pub enum ParseRemotePackageError {
    #[error("unable to parse package {0}. expected format: `name@version`")]
    InvalidInput(String),
    #[error("unable to parse package {package_str}: {error}")]
    InvalidPackageVersion {
        #[source]
        error: PackageVersionParseError,
        package_str: String,
    },
}

impl FromStr for PackageSpec {
    type Err = ParseRemotePackageError;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        let (name, version) = s
            .split_once('@')
            .ok_or_else(|| ParseRemotePackageError::InvalidInput(s.to_string()))?;

        Self::parse(name.to_string(), version.to_string()).map_err(|error| {
            ParseRemotePackageError::InvalidPackageVersion {
                error,
                package_str: s.to_string(),
            }
        })
    }
}

/// A lua package requirement with a name and an optional version requirement.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "clap", derive(clap::Args))]
pub struct PackageReq {
    /// The name of the package.
    pub(crate) name: PackageName,
    /// The version requirement, for example "1.0.0" or ">=1.0.0".
    pub(crate) version_req: PackageVersionReq,
}

impl PackageReq {
    pub fn new(name: String, version: Option<String>) -> Result<Self, PackageVersionReqError> {
        let version_req = match version {
            Some(version_req_str) => PackageVersionReq::parse(version_req_str.as_str())?,
            None => PackageVersionReq::any(),
        };
        Ok(Self {
            name: PackageName::new(name),
            version_req,
        })
    }
    pub fn parse(pkg_constraints: &str) -> Result<Self, PackageReqParseError> {
        Self::from_str(pkg_constraints)
    }
    pub fn name(&self) -> &PackageName {
        &self.name
    }
    pub fn version_req(&self) -> &PackageVersionReq {
        &self.version_req
    }
    /// Evaluate whether the given package satisfies the package requirement
    /// given by `self`.
    pub fn matches(&self, package: &PackageSpec) -> bool {
        self.name == package.name && self.version_req.matches(&package.version)
    }
}

impl Display for PackageReq {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.version_req.is_any() {
            self.name.fmt(f)
        } else {
            f.write_str(format!("{}{}", self.name, self.version_req).as_str())
        }
    }
}

impl From<PackageSpec> for PackageReq {
    fn from(value: PackageSpec) -> Self {
        value.into_package_req()
    }
}

impl From<PackageName> for PackageReq {
    fn from(name: PackageName) -> Self {
        Self {
            name,
            version_req: PackageVersionReq::any(),
        }
    }
}

impl FromLua for PackageReq {
    fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
        let str: String = lua.from_value(value)?;
        Self::parse(&str).into_lua_err()
    }
}

impl mlua::UserData for PackageReq {
    fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
        methods.add_method("name", |_, this, ()| Ok(this.name.to_string()));
        methods.add_method("version_req", |_, this, ()| {
            Ok(this.version_req.to_string())
        });
        methods.add_method("matches", |_, this, package: PackageSpec| {
            Ok(this.matches(&package))
        });
    }
}

/// Wrapper structs for proper serialization of various dependency types.
pub(crate) struct Dependencies<'a>(pub(crate) &'a Vec<LuaDependencySpec>);
pub(crate) struct BuildDependencies<'a>(pub(crate) &'a Vec<LuaDependencySpec>);
pub(crate) struct TestDependencies<'a>(pub(crate) &'a Vec<LuaDependencySpec>);

impl DisplayAsLuaKV for Dependencies<'_> {
    fn display_lua(&self) -> DisplayLuaKV {
        DisplayLuaKV {
            key: "dependencies".to_string(),
            value: DisplayLuaValue::List(
                self.0
                    .iter()
                    .map(|req| DisplayLuaValue::String(req.to_string()))
                    .collect(),
            ),
        }
    }
}

impl DisplayAsLuaKV for BuildDependencies<'_> {
    fn display_lua(&self) -> DisplayLuaKV {
        DisplayLuaKV {
            key: "build_dependencies".to_string(),
            value: DisplayLuaValue::List(
                self.0
                    .iter()
                    .map(|req| DisplayLuaValue::String(req.to_string()))
                    .collect(),
            ),
        }
    }
}

impl DisplayAsLuaKV for TestDependencies<'_> {
    fn display_lua(&self) -> DisplayLuaKV {
        DisplayLuaKV {
            key: "test_dependencies".to_string(),
            value: DisplayLuaValue::List(
                self.0
                    .iter()
                    .map(|req| DisplayLuaValue::String(req.to_string()))
                    .collect(),
            ),
        }
    }
}

#[derive(Error, Debug)]
pub enum PackageReqParseError {
    #[error("could not parse dependency name from {0}")]
    InvalidDependencyName(String),
    #[error("could not parse version requirement in '{str}': {error}")]
    InvalidPackageVersionReq {
        #[source]
        error: PackageVersionReqError,
        str: String,
    },
}

impl FromStr for PackageReq {
    type Err = PackageReqParseError;

    fn from_str(str: &str) -> Result<Self, PackageReqParseError> {
        let rock_name_str = str
            .chars()
            .peeking_take_while(|t| t.is_alphanumeric() || matches!(t, '-' | '_' | '.'))
            .collect::<String>();

        if rock_name_str.is_empty() {
            return Err(PackageReqParseError::InvalidDependencyName(str.to_string()));
        }

        let constraints = str.trim_start_matches(&rock_name_str).trim();
        let version_req = match constraints {
            "" => PackageVersionReq::any(),
            constraints => PackageVersionReq::parse(constraints.trim_start()).map_err(|error| {
                PackageReqParseError::InvalidPackageVersionReq {
                    error,
                    str: str.to_string(),
                }
            })?,
        };
        Ok(Self {
            name: PackageName::new(rock_name_str),
            version_req,
        })
    }
}

impl<'de> Deserialize<'de> for PackageReq {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        Self::from_str(&s).map_err(de::Error::custom)
    }
}

/// A luarocks package name, which is always lowercase
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub struct PackageName(String);

impl IntoLua for PackageName {
    fn into_lua(self, lua: &mlua::Lua) -> mlua::Result<mlua::Value> {
        self.0.into_lua(lua)
    }
}

impl PackageName {
    pub fn new(name: String) -> Self {
        Self(name.to_lowercase())
    }
}

impl<'de> Deserialize<'de> for PackageName {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        Ok(PackageName::new(String::deserialize(deserializer)?))
    }
}

impl FromLua for PackageName {
    fn from_lua(
        value: mlua::prelude::LuaValue,
        lua: &mlua::prelude::Lua,
    ) -> mlua::prelude::LuaResult<Self> {
        Ok(Self::new(String::from_lua(value, lua)?))
    }
}

impl Serialize for PackageName {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.0.serialize(serializer)
    }
}

impl From<&str> for PackageName {
    fn from(value: &str) -> Self {
        Self::new(value.into())
    }
}

impl Display for PackageName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.0.as_str())
    }
}

#[derive(Debug)]
pub struct PackageNameList(Vec<PackageName>);

impl PackageNameList {
    pub(crate) fn new(package_names: Vec<PackageName>) -> Self {
        Self(package_names)
    }
}

impl Display for PackageNameList {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.0.iter().map(|pkg| pkg.to_string()).join(", ").as_str())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn parse_name() {
        let mut package_name: PackageName = "neorg".into();
        assert_eq!(package_name.to_string(), "neorg");
        package_name = "LuaFileSystem".into();
        assert_eq!(package_name.to_string(), "luafilesystem");
    }

    #[tokio::test]
    async fn parse_lua_package() {
        let neorg = PackageSpec::parse("neorg".into(), "1.0.0".into()).unwrap();
        let expected_version = PackageVersion::parse("1.0.0").unwrap();
        assert_eq!(neorg.name().to_string(), "neorg");
        assert!(matches!(
            neorg.version().cmp(&expected_version),
            std::cmp::Ordering::Equal
        ));
        let neorg = PackageSpec::parse("neorg".into(), "1.0".into()).unwrap();
        assert!(matches!(
            neorg.version().cmp(&expected_version),
            std::cmp::Ordering::Equal
        ));
        let neorg = PackageSpec::parse("neorg".into(), "1".into()).unwrap();
        assert!(matches!(
            neorg.version().cmp(&expected_version),
            std::cmp::Ordering::Equal
        ));
    }

    #[tokio::test]
    async fn parse_lua_package_req() {
        let mut package_req = PackageReq::new("foo".into(), Some("1.0.0".into())).unwrap();
        assert!(package_req.matches(&PackageSpec::parse("foo".into(), "1.0.0".into()).unwrap()));
        assert!(!package_req.matches(&PackageSpec::parse("bar".into(), "1.0.0".into()).unwrap()));
        assert!(!package_req.matches(&PackageSpec::parse("foo".into(), "2.0.0".into()).unwrap()));
        package_req = PackageReq::new("foo".into(), Some(">= 1.0.0".into())).unwrap();
        assert!(package_req.matches(&PackageSpec::parse("foo".into(), "2.0.0".into()).unwrap()));
        let package_req: PackageReq = "lua >= 5.1".parse().unwrap();
        assert_eq!(package_req.name.to_string(), "lua");
        let package_req: PackageReq = "lua>=5.1".parse().unwrap();
        assert_eq!(package_req.name.to_string(), "lua");
        let package_req: PackageReq = "toml-edit >= 0.1.0".parse().unwrap();
        assert_eq!(package_req.name.to_string(), "toml-edit");
        let package_req: PackageReq = "plugin.nvim >= 0.1.0".parse().unwrap();
        assert_eq!(package_req.name.to_string(), "plugin.nvim");
        let package_req: PackageReq = "lfs".parse().unwrap();
        assert_eq!(package_req.name.to_string(), "lfs");
        let package_req: PackageReq = "neorg 1.0.0".parse().unwrap();
        assert_eq!(package_req.name.to_string(), "neorg");
        let neorg = PackageSpec::parse("neorg".into(), "1.0.0".into()).unwrap();
        assert!(package_req.matches(&neorg));
        let neorg = PackageSpec::parse("neorg".into(), "2.0.0".into()).unwrap();
        assert!(!package_req.matches(&neorg));
        let package_req: PackageReq = "neorg 2.0.0".parse().unwrap();
        assert!(package_req.matches(&neorg));
        let package_req: PackageReq = "neorg = 2.0.0".parse().unwrap();
        assert!(package_req.matches(&neorg));
        let package_req: PackageReq = "neorg == 2.0.0".parse().unwrap();
        assert!(package_req.matches(&neorg));
        let package_req: PackageReq = "neorg &equals; 2.0.0".parse().unwrap();
        assert!(package_req.matches(&neorg));
        let package_req: PackageReq = "neorg >= 1.0, &lt; 2.0".parse().unwrap();
        let neorg = PackageSpec::parse("neorg".into(), "1.5".into()).unwrap();
        assert!(package_req.matches(&neorg));
        let package_req: PackageReq = "neorg &gt; 1.0, &lt; 2.0".parse().unwrap();
        let neorg = PackageSpec::parse("neorg".into(), "1.11.0".into()).unwrap();
        assert!(package_req.matches(&neorg));
        let neorg = PackageSpec::parse("neorg".into(), "3.0.0".into()).unwrap();
        assert!(!package_req.matches(&neorg));
        let neorg = PackageSpec::parse("neorg".into(), "0.5".into()).unwrap();
        assert!(!package_req.matches(&neorg));
        let package_req: PackageReq = "neorg ~> 1".parse().unwrap();
        assert!(!package_req.matches(&neorg));
        let neorg = PackageSpec::parse("neorg".into(), "3".into()).unwrap();
        assert!(!package_req.matches(&neorg));
        let neorg = PackageSpec::parse("neorg".into(), "1.5".into()).unwrap();
        assert!(package_req.matches(&neorg));
        let package_req: PackageReq = "neorg ~> 1.4".parse().unwrap();
        let neorg = PackageSpec::parse("neorg".into(), "1.3".into()).unwrap();
        assert!(!package_req.matches(&neorg));
        let neorg = PackageSpec::parse("neorg".into(), "1.5".into()).unwrap();
        assert!(!package_req.matches(&neorg));
        let neorg = PackageSpec::parse("neorg".into(), "1.4.10".into()).unwrap();
        assert!(package_req.matches(&neorg));
        let neorg = PackageSpec::parse("neorg".into(), "1.4".into()).unwrap();
        assert!(package_req.matches(&neorg));
        let package_req: PackageReq = "neorg ~> 1.0.5".parse().unwrap();
        let neorg = PackageSpec::parse("neorg".into(), "1.0.4".into()).unwrap();
        assert!(!package_req.matches(&neorg));
        let neorg = PackageSpec::parse("neorg".into(), "1.0.5".into()).unwrap();
        assert!(package_req.matches(&neorg));
        let neorg = PackageSpec::parse("neorg".into(), "1.0.6".into()).unwrap();
        assert!(!package_req.matches(&neorg));
        // Testing incomplete version constraints
        let package_req: PackageReq = "lua-utils.nvim ~> 1.1-1".parse().unwrap();
        let lua_utils = PackageSpec::parse("lua-utils.nvim".into(), "1.1.4".into()).unwrap();
        assert!(package_req.matches(&lua_utils));
        let lua_utils = PackageSpec::parse("lua-utils.nvim".into(), "1.1.5".into()).unwrap();
        assert!(package_req.matches(&lua_utils));
        let lua_utils = PackageSpec::parse("lua-utils.nvim".into(), "1.2-1".into()).unwrap();
        assert!(!package_req.matches(&lua_utils));
    }

    #[tokio::test]
    pub async fn remote_package_type_priorities() {
        let rock_types = vec![
            RemotePackageType::Binary,
            RemotePackageType::Src,
            RemotePackageType::Rockspec,
        ];
        assert_eq!(
            rock_types.into_iter().sorted().collect_vec(),
            vec![
                RemotePackageType::Src,
                RemotePackageType::Rockspec,
                RemotePackageType::Binary,
            ]
        );
    }
}