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
#[ allow( clippy ::std_instead_of_alloc, clippy ::std_instead_of_core ) ]
mod private
{
use crate :: *;
use std ::
{
io ::Read,
fmt ::Write,
time ::Duration,
path ::PathBuf,
};
use error :: { untyped ::Context };
use ureq ::Agent;
/// Constructs the expected local path for a packed `.crate` file within a target directory.
///
/// This is a utility function that builds a predictable path without verifying
/// if the file actually exists. It follows the standard Cargo packaging structure.
///
/// # Arguments
///
/// - `name` - The name of the package.
/// - `version` - The version of the package.
/// - `target_dir` - The path to the workspace's `target` directory, inside which
/// the `package/` subdirectory is expected.
///
/// # Returns
///
/// Returns a `Result` containing a `PathBuf` that points to the expected location of the `.crate` file,
/// for example: `< target_dir >/package/my_package-0.1.0.crate`.
///
/// # Errors
///
/// This function is currently infallible as it only performs path joining and string formatting.
/// The `Result` is kept for API consistency.
// qqq: typed error
pub fn local_path< 'a >( name: &'a str, version: &'a str, target_dir: &std ::path ::Path ) -> error ::untyped ::Result< PathBuf >
{
let buf = format!( "package/{name}-{version}.crate" );
let local_package_path = target_dir.join( buf );
error ::untyped ::Result ::Ok( local_package_path )
}
///
/// Get data of remote package from crates.io.
///
/// # Errors
/// qqq: doc
///
/// # Panics
/// qqq: doc
// qqq: typed error
pub fn download< 'a >( name: &'a str, version: &'a str ) -> error ::untyped ::Result< Vec< u8 > >
{
let agent: Agent = ureq ::AgentBuilder ::new()
.timeout_read( Duration ::from_secs( 5 ) )
.timeout_write( Duration ::from_secs( 5 ) )
.build();
let mut buf = String ::new();
write!( &mut buf, "https: //static.crates.io/crates/{name}/{name}-{version}.crate" )?;
let resp = agent.get( &buf[ .. ] ).call().context( "Get data of remote package" )?;
let len: usize = resp.header( "Content-Length" )
.unwrap()
.parse()?;
let mut bytes: Vec< u8 > = Vec ::with_capacity( len );
resp.into_reader()
.take( u64 ::MAX )
.read_to_end( &mut bytes )?;
error ::untyped ::Result ::Ok( bytes )
}
}
//
crate ::mod_interface!
{
own use local_path;
own use download;
}