-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: introduce
PackageName
for luarocks package names
- Loading branch information
Showing
3 changed files
with
78 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,69 @@ | ||
use semver::{Error, Version}; | ||
use eyre::Result; | ||
use semver::Version; | ||
use serde::Deserialize; | ||
use std::fmt::Display; | ||
|
||
mod version; | ||
|
||
pub use version::{parse_version, parse_version_req}; | ||
|
||
// TODO: We probably need a better name for this | ||
pub struct LuaRock { | ||
pub name: String, | ||
pub version: Version, | ||
name: PackageName, | ||
version: Version, | ||
} | ||
|
||
impl LuaRock { | ||
pub fn new(name: String, version: String) -> Result<Self, Error> { | ||
pub fn new(name: String, version: String) -> Result<Self> { | ||
Ok(Self { | ||
name, | ||
name: PackageName::new(name), | ||
version: parse_version(&version)?, | ||
}) | ||
} | ||
pub fn name(&self) -> &PackageName { | ||
&self.name | ||
} | ||
pub fn version(&self) -> &Version { | ||
&self.version | ||
} | ||
} | ||
|
||
/// A luarocks package name, which is always lowercase | ||
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)] | ||
pub struct PackageName { | ||
name: String, | ||
} | ||
|
||
impl PackageName { | ||
pub fn new(name: String) -> Self { | ||
Self { | ||
// TODO: validations? | ||
name: name.to_lowercase(), | ||
} | ||
} | ||
} | ||
|
||
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.name.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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters