-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::path::PathBuf; | ||
use serde_derive::{Deserialize, Serialize}; | ||
use url::Url; | ||
use std::path::Path; | ||
use crate::package::version::{Version, to_version_string, from_version_string}; | ||
|
||
#[derive(Clone, Deserialize, Serialize)] | ||
enum ProjectSource { | ||
#[serde(rename="git")] | ||
Git(Url), | ||
#[serde(rename="tarball")] | ||
TarBall(Url), | ||
#[serde(rename="path")] | ||
Path(PathBuf) | ||
} | ||
|
||
/// Dependency with source and version | ||
#[derive(Clone, Deserialize, Serialize)] | ||
pub struct DetailedDependency { | ||
#[serde(deserialize_with="from_version_string", serialize_with="to_version_string")] | ||
version: Version, | ||
#[serde(flatten)] | ||
mutual_exclusive: ProjectSource | ||
} | ||
|
||
fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> { | ||
Check warning on line 26 in src/package/management.rs GitHub Actions / Check
Check warning on line 26 in src/package/management.rs GitHub Actions / Clippy Outputfunction `copy_dir_all` is never used
|
||
std::fs::create_dir_all(&dst)?; | ||
for entry in std::fs::read_dir(src)? { | ||
let entry = entry?; | ||
let ty = entry.file_type()?; | ||
if ty.is_dir() { | ||
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?; | ||
} else { | ||
std::fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
|
||
impl DetailedDependency { | ||
fn fetch(&self, library_path: &PathBuf) -> anyhow::Result<()>{ | ||
Check warning on line 42 in src/package/management.rs GitHub Actions / Check
Check warning on line 42 in src/package/management.rs GitHub Actions / Clippy Outputmethod `fetch` is never used
|
||
match &self.mutual_exclusive { | ||
ProjectSource::Path(path_buf) => { | ||
Ok(copy_dir_all(path_buf, library_path)?) | ||
}, | ||
ProjectSource::Git(git_url ) => { | ||
git2::Repository::clone(git_url.as_str(), library_path)?; | ||
Ok(()) | ||
}, | ||
_ => todo!("Not Supported") | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use std::fmt; | ||
use std::fmt::Display; | ||
use std::str::FromStr; | ||
use serde::{Deserializer, Serializer}; | ||
|
||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub struct Version { | ||
pub major : u32, | ||
pub minor : u32, | ||
pub patch : u32 | ||
} | ||
|
||
impl Display for Version { | ||
fn fmt( &self, fmtr : &mut fmt::Formatter ) -> fmt::Result { | ||
write!( fmtr, "{}.{}.{}", self.major, self.minor, self.patch ) | ||
} | ||
} | ||
|
||
impl FromStr for Version { | ||
type Err = String; | ||
|
||
fn from_str( s : &str ) -> Result<Version, Self::Err> { | ||
let parts : Vec<Result<u32, &str>> = | ||
s.split( '.' ) | ||
.map( | elm | elm.parse::<u32>() | ||
.map_err( |_| elm ) ) | ||
.collect(); | ||
|
||
if parts.len() != 3 { | ||
return | ||
Err( format!( "Invalid version format: expected 3 components, got {}." | ||
, parts.len() ) ); | ||
} | ||
|
||
for part in &parts { | ||
match part { | ||
&Err( err ) => | ||
return | ||
Err( format!( "Invalid version format: expected integer, got '{}'." | ||
, err ) ), | ||
_ => {} | ||
} | ||
Check warning on line 42 in src/package/version.rs GitHub Actions / Clippy Outputyou seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
|
||
} | ||
|
||
Ok( Version { | ||
major: parts[0].unwrap(), | ||
minor: parts[1].unwrap(), | ||
patch: parts[2].unwrap() | ||
} ) | ||
} | ||
} | ||
|
||
pub(crate) fn from_version_string<'de, D: Deserializer<'de>>(d: D) -> Result<Version, D::Error> { | ||
struct VersionVisitor; | ||
|
||
impl<'de> serde::de::Visitor<'de> for VersionVisitor { | ||
type Value = Version; | ||
|
||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(formatter, "cannot parse version") | ||
} | ||
|
||
fn visit_str<E: serde::de::Error>(self, s: &str) -> Result<Version, E> { | ||
Ok(Version::from_str(s).map_err(|e| E::invalid_value(serde::de::Unexpected::Str(s), &self))?) | ||
Check warning on line 64 in src/package/version.rs GitHub Actions / Check
Check warning on line 64 in src/package/version.rs GitHub Actions / Clippy Outputquestion mark operator is useless here
Check warning on line 64 in src/package/version.rs GitHub Actions / Clippy Outputunused variable: `e`
|
||
} | ||
} | ||
|
||
d.deserialize_any(VersionVisitor) | ||
} | ||
|
||
pub(crate) fn to_version_string<S>(version: &Version, s: S) -> Result<S::Ok, S::Error> | ||
where S: Serializer | ||
{ | ||
let serialized_string = format!("{}", version); | ||
Ok(s.serialize_str(&serialized_string)?) | ||
Check warning on line 75 in src/package/version.rs GitHub Actions / Clippy Outputquestion mark operator is useless here
|
||
} | ||
|
||
|