Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change DebugSession::source_by_path() to return SourceCode enum #758

Merged
merged 19 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: review changes
  • Loading branch information
vaind committed Feb 17, 2023
commit adf4cfe872d30228ce1914f804572d23feb4f875
8 changes: 3 additions & 5 deletions symbolic-ppdb/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl<'data> PortablePdb<'data> {
us_stream: None,
blob_stream: None,
guid_stream: None,
source_link_mappings: SourceLinkMappings::empty(),
source_link_mappings: SourceLinkMappings::default(),
};

let mut metadata_stream = None;
Expand Down Expand Up @@ -297,12 +297,10 @@ impl<'data> PortablePdb<'data> {
let cdi = cdi?;
// Note: only handle module #1 (do we actually handle multiple modules in any way??)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe there is only ever one module as defined in the specification.

if let (metadata::CustomDebugInformationTag::Module, 1) = (cdi.tag, cdi.value) {
let json = String::from_utf8_lossy(result.get_blob(cdi.blob)?);
source_link_mappings.push(json);
source_link_mappings.push(result.get_blob(cdi.blob)?);
}
}
result.source_link_mappings =
SourceLinkMappings::new(source_link_mappings.iter().map(|v| v.as_ref()))?;
result.source_link_mappings = SourceLinkMappings::new(source_link_mappings)?;

Ok(result)
}
Expand Down
48 changes: 27 additions & 21 deletions symbolic-ppdb/src/format/sourcelinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::cmp::Ordering;
use crate::{FormatError, FormatErrorKind};

/// See [Source Link PPDB docs](https://github.com/dotnet/designs/blob/main/accepted/2020/diagnostics/source-link.md#source-link-json-schema).
#[derive(Clone)]
#[derive(Default, Clone)]
pub(crate) struct SourceLinkMappings {
rules: Vec<Rule>,
}
Expand All @@ -21,14 +21,7 @@ enum Pattern {
}

impl SourceLinkMappings {
pub fn empty() -> Self {
SourceLinkMappings { rules: Vec::new() }
}

pub fn new<'a, I>(jsons: I) -> Result<Self, FormatError>
where
I: Iterator<Item = &'a str>,
{
pub fn new(jsons: Vec<&[u8]>) -> Result<Self, FormatError> {
let mut result = Self { rules: Vec::new() };
for json in jsons {
result.add_mappings(json)?;
Expand All @@ -37,9 +30,9 @@ impl SourceLinkMappings {
Ok(result)
}

fn add_mappings(&mut self, json: &str) -> Result<(), FormatError> {
fn add_mappings(&mut self, json: &[u8]) -> Result<(), FormatError> {
use serde_json::*;
let json: Value = serde_json::from_str(json)
let json: Value = serde_json::from_slice(json)
.map_err(|e| FormatError::new(FormatErrorKind::InvalidSourceLinkJson, e))?;

let docs = json
Expand All @@ -48,7 +41,7 @@ impl SourceLinkMappings {
.ok_or_else(Self::err)?;

self.rules.reserve(docs.len());
for doc in docs.iter() {
for (key, url) in docs.iter() {
/*
Each document is defined by a file path and a URL. Original source file paths are compared
case-insensitively to documents and the resulting URL is used to download source. The document
Expand All @@ -59,8 +52,8 @@ impl SourceLinkMappings {
3. If the file path contains a *, it must be the final character.
4. If the URL contains a *, it may be anywhere in the URL.
*/
let key = doc.0.to_lowercase();
let url = doc.1.as_str().ok_or_else(Self::err)?.into();
let key = key.to_lowercase();
let url = url.as_str().ok_or_else(Self::err)?.into();
let pattern = if let Some(prefix) = key.strip_suffix('*') {
Pattern::Prefix(prefix.into())
} else {
Expand Down Expand Up @@ -124,12 +117,14 @@ mod tests {

#[test]
fn test_invalid_json() {
let mut mappings = SourceLinkMappings::empty();
assert!(mappings.add_mappings("").is_err());
assert!(mappings.add_mappings("foo").is_err());
assert!(mappings.add_mappings("{\"docs\": {\"k\": \"v\"}}").is_err());
let mut mappings = SourceLinkMappings::default();
assert!(mappings.add_mappings("".as_bytes()).is_err());
assert!(mappings.add_mappings("foo".as_bytes()).is_err());
assert!(mappings
.add_mappings("{\"documents\": [\"k\", \"v\"]}")
.add_mappings("{\"docs\": {\"k\": \"v\"}}".as_bytes())
.is_err());
assert!(mappings
.add_mappings("{\"documents\": [\"k\", \"v\"]}".as_bytes())
vaind marked this conversation as resolved.
Show resolved Hide resolved
.is_err());
assert_eq!(mappings.rules.len(), 0);
}
Expand All @@ -152,16 +147,23 @@ mod tests {
"C:\\src\\file.txt": "https://example.com/file.txt"
vaind marked this conversation as resolved.
Show resolved Hide resolved
}
}
"#].iter().copied()
"#, r#"
{
"documents": {
"/home/user/src/*": "https://linux.com/*"
}
}
"#].map(|v| v.as_bytes()).to_vec()
).unwrap();

assert_eq!(mappings.rules.len(), 5);
assert_eq!(mappings.rules.len(), 6);

// In this example:
// All files under directory bar will map to a relative URL beginning with http://MyBarDomain.com/src/.
// All files under directory foo will map to a relative URL beginning with http://MyFooDomain.com/src/ EXCEPT foo/specific.txt which will map to http://MySpecificFoodDomain.com/src/specific.txt.
// All other files anywhere under the src directory will map to a relative url beginning with http://MyDefaultDomain.com/src/.
assert!(mappings.resolve("c:\\other\\path").is_none());
assert!(mappings.resolve("/home/path").is_none());
assert_eq!(
mappings.resolve("c:\\src\\bAr\\foo\\FiLe.txt").unwrap(),
"http://MyBarDomain.com/src/foo/FiLe.txt"
Expand All @@ -182,5 +184,9 @@ mod tests {
mappings.resolve("c:\\src\\other\\path").unwrap(),
"http://MyDefaultDomain.com/src/other/path"
);
assert_eq!(
mappings.resolve("/home/user/src/Path/TO/file.txt").unwrap(),
"https://linux.com/Path/TO/file.txt"
);
}
}