-
Notifications
You must be signed in to change notification settings - Fork 201
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
Зубков Андрей #164
base: master
Are you sure you want to change the base?
Зубков Андрей #164
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
using System.Drawing; | ||
using ResultOf; | ||
|
||
namespace TagCloud.Drawer; | ||
|
||
public interface IDrawer | ||
{ | ||
Bitmap DrawTagCloud(IEnumerable<(string word, int rank)> words); | ||
Result<Bitmap> DrawTagCloud(IEnumerable<(string word, int rank)> words); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,36 @@ | ||
using ResultOf; | ||
using Spire.Doc; | ||
|
||
namespace TagCloud.FileReader; | ||
|
||
public class DocReader : IFileReader | ||
{ | ||
public IEnumerable<string> ReadLines(string inputPath) | ||
public IList<string> GetAvailableExtensions() => new List<string>() { "doc", "docx" }; | ||
|
||
public Result<IEnumerable<string>> ReadLines(string inputPath) | ||
{ | ||
if (!File.Exists(inputPath)) | ||
throw new ArgumentException("Source file doesn't exist"); | ||
return FileExists(inputPath, out var error) | ||
? Result.Ok(ReadFile(inputPath)) | ||
: Result.Fail<IEnumerable<string>>(error); | ||
} | ||
|
||
var document = new Document(inputPath, FileFormat.Auto); | ||
private IEnumerable<string> ReadFile(string inputPath) | ||
{ | ||
using var document = new Document(inputPath, FileFormat.Auto); | ||
var text = document.GetText(); | ||
|
||
return text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Skip(1); | ||
} | ||
|
||
public IList<string> GetAvailableExtensions() => new List<string>() { "doc", "docx" }; | ||
private bool FileExists(string inputPath, out string error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. С Тут, например, можно было использовать все тот же паттерн |
||
{ | ||
if (!File.Exists(inputPath)) | ||
{ | ||
error = $"File {inputPath} doesn't exist"; | ||
return false; | ||
} | ||
|
||
error = string.Empty; | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
using ResultOf; | ||
|
||
namespace TagCloud.FileReader; | ||
|
||
public class FileReaderProvider : IFileReaderProvider | ||
|
@@ -9,12 +11,12 @@ public FileReaderProvider(IEnumerable<IFileReader> readers) | |
this.readers = ArrangeReaders(readers); | ||
} | ||
|
||
public IFileReader CreateReader(string inputPath) | ||
public Result<IFileReader> CreateReader(string inputPath) | ||
{ | ||
var extension = inputPath.Split(".").Last(); | ||
if (readers.ContainsKey(extension)) | ||
return readers[extension]; | ||
throw new ArgumentException($"{extension} file type is not supported"); | ||
return readers.ContainsKey(extension) | ||
? Result.Ok(readers[extension]) | ||
: Result.Fail<IFileReader>($"Reading of file {inputPath} with extension {extension} is not supported"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я думаю принцип ты понял) |
||
} | ||
|
||
private Dictionary<string, IFileReader> ArrangeReaders(IEnumerable<IFileReader> readers) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
using ResultOf; | ||
|
||
namespace TagCloud.FileReader; | ||
|
||
public interface IFileReader | ||
{ | ||
IEnumerable<string> ReadLines(string inputPath); | ||
Result<IEnumerable<string>> ReadLines(string inputPath); | ||
|
||
IList<string> GetAvailableExtensions(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
using ResultOf; | ||
|
||
namespace TagCloud.FileReader; | ||
|
||
public interface IFileReaderProvider | ||
{ | ||
IFileReader CreateReader(string inputPath); | ||
Result<IFileReader> CreateReader(string inputPath); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,29 @@ | ||
using System.Xml.XPath; | ||
using ResultOf; | ||
using Spire.Doc; | ||
|
||
namespace TagCloud.FileReader; | ||
|
||
public class TxtReader : IFileReader | ||
{ | ||
private List<string> extensions = new() { "txt" }; | ||
public IList<string> GetAvailableExtensions() => new List<string>() { "txt" }; | ||
|
||
public IEnumerable<string> ReadLines(string inputPath) | ||
public Result<IEnumerable<string>> ReadLines(string inputPath) | ||
{ | ||
if (!File.Exists(inputPath)) | ||
throw new ArgumentException("Source file doesn't exist"); | ||
|
||
return File.ReadLines(inputPath); | ||
return FileExists(inputPath, out var error) | ||
? Result.Ok(File.ReadLines(inputPath)) | ||
: Result.Fail<IEnumerable<string>>(error); | ||
} | ||
|
||
public IList<string> GetAvailableExtensions() | ||
private bool FileExists(string inputPath, out string error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Хм, точно такой же метод находится в классе |
||
{ | ||
return extensions; | ||
if (!File.Exists(inputPath)) | ||
{ | ||
error = $"File {inputPath} doesn't exist"; | ||
return false; | ||
} | ||
|
||
error = string.Empty; | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
using ResultOf; | ||
|
||
namespace TagCloud.PointGenerator; | ||
|
||
public interface IPointGeneratorProvider | ||
{ | ||
IPointGenerator CreateGenerator(string generatorName); | ||
Result<IPointGenerator> CreateGenerator(string generatorName); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
using ResultOf; | ||
|
||
namespace TagCloud.PointGenerator; | ||
|
||
public class PointGeneratorProvider : IPointGeneratorProvider | ||
|
@@ -9,11 +11,11 @@ public PointGeneratorProvider(IEnumerable<IPointGenerator> generators) | |
registeredGenerators = ArrangeLayouters(generators); | ||
} | ||
|
||
public IPointGenerator CreateGenerator(string generatorName) | ||
public Result<IPointGenerator> CreateGenerator(string generatorName) | ||
{ | ||
if (registeredGenerators.ContainsKey(generatorName)) | ||
return registeredGenerators[generatorName]; | ||
throw new ArgumentException($"{generatorName} layouter is not supported"); | ||
return registeredGenerators.ContainsKey(generatorName) | ||
? Result.Ok(registeredGenerators[generatorName]) | ||
: Result.Fail<IPointGenerator>($"{generatorName} layouter is not supported"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут было бы прикольно ещё дать пользователю сразу подсказку, а какие тогда поддерживаются? |
||
} | ||
|
||
private Dictionary<string, IPointGenerator> ArrangeLayouters(IEnumerable<IPointGenerator> generators) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Аналогичный момент, а что нужно сделать пользователю, чтобы это исправить? Уменьшить / увеличить размеры? Может быть сразу подсказать, какой размер изображения получился сейчас?