refactor(snix/glue): reduce string allocations in the flake ref parser

Change-Id: Id6818976b651b6d6ffa51cf7e2066cce0cb01055
Reviewed-on: https://cl.snix.dev/c/snix/+/30203
Reviewed-by: Florian Klink <flokli@flokli.de>
Autosubmit: Sander Melnikov <hey@sandydoo.me>
Tested-by: besadii
This commit is contained in:
Sander 2025-03-20 19:40:14 +00:00 committed by clbot
parent 7aef14c57f
commit be949fb122
2 changed files with 16 additions and 13 deletions

View file

@ -633,6 +633,11 @@ impl NixString {
unsafe { NixStringInner::data_slice(self.0) } unsafe { NixStringInner::data_slice(self.0) }
} }
/// Returns a &str if the NixString is a valid UTF-8 string.
pub fn as_str(&self) -> Result<&str, std::str::Utf8Error> {
std::str::from_utf8(self.as_bytes())
}
pub fn into_bstring(self) -> BString { pub fn into_bstring(self) -> BString {
self.as_bstr().to_owned() self.as_bstr().to_owned()
} }

View file

@ -202,20 +202,18 @@ pub(crate) mod fetcher_builtins {
co: GenCo, co: GenCo,
value: Value, value: Value,
) -> Result<Value, ErrorKind> { ) -> Result<Value, ErrorKind> {
let flake_ref_str = value.to_str()?.into_bstring().as_bstr().to_string(); let flake_ref = value.to_str()?;
let flake_ref_str = flake_ref.to_str()?;
let fetch_args = match flake_ref_str.parse() { let fetch_args = flake_ref_str
Ok(args) => args, .parse()
Err(err) => { .map_err(|err| ErrorKind::SnixError(Rc::new(err)))?;
return Err(ErrorKind::SnixError(Rc::new(err)));
}
};
// Convert the FlakeRef to our Value format // Convert the FlakeRef to our Value format
let mut attrs = BTreeMap::new(); let mut attrs = BTreeMap::new();
// Extract type and url based on the variant // Extract type and url based on the variant
match &fetch_args { match fetch_args {
flakeref::FlakeRef::Git { url, .. } => { flakeref::FlakeRef::Git { url, .. } => {
attrs.insert("type".into(), Value::from("git")); attrs.insert("type".into(), Value::from("git"));
attrs.insert("url".into(), Value::from(url.to_string())); attrs.insert("url".into(), Value::from(url.to_string()));
@ -224,16 +222,16 @@ pub(crate) mod fetcher_builtins {
owner, repo, r#ref, .. owner, repo, r#ref, ..
} => { } => {
attrs.insert("type".into(), Value::from("github")); attrs.insert("type".into(), Value::from("github"));
attrs.insert("owner".into(), Value::from(owner.clone())); attrs.insert("owner".into(), Value::from(owner));
attrs.insert("repo".into(), Value::from(repo.clone())); attrs.insert("repo".into(), Value::from(repo));
if let Some(ref_name) = r#ref { if let Some(ref_name) = r#ref {
attrs.insert("ref".into(), Value::from(ref_name.clone())); attrs.insert("ref".into(), Value::from(ref_name));
} }
} }
flakeref::FlakeRef::GitLab { owner, repo, .. } => { flakeref::FlakeRef::GitLab { owner, repo, .. } => {
attrs.insert("type".into(), Value::from("gitlab")); attrs.insert("type".into(), Value::from("gitlab"));
attrs.insert("owner".into(), Value::from(owner.clone())); attrs.insert("owner".into(), Value::from(owner));
attrs.insert("repo".into(), Value::from(repo.clone())); attrs.insert("repo".into(), Value::from(repo));
} }
flakeref::FlakeRef::File { url, .. } => { flakeref::FlakeRef::File { url, .. } => {
attrs.insert("type".into(), Value::from("file")); attrs.insert("type".into(), Value::from("file"));